CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
docs: add image generation guide and finish 0.7 docs prep
xf gong0481bfce3e03
diff --git a/.playwright-cli/console-2026-06-05T01-46-09-613Z.log b/.playwright-cli/console-2026-06-05T01-46-09-613Z.lognew file mode 100644index 0000000..9c60081--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-46-09-613Z.log@@ -0,0 +1 @@+[ 624ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-46-41-854Z.log b/.playwright-cli/console-2026-06-05T01-46-41-854Z.lognew file mode 100644index 0000000..1f3c691--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-46-41-854Z.log@@ -0,0 +1,17 @@+[ 172ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 322ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-46-44-306Z.log b/.playwright-cli/console-2026-06-05T01-46-44-306Z.lognew file mode 100644index 0000000..edfb2e7--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-46-44-306Z.log@@ -0,0 +1 @@+[ 152ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-47-44-930Z.log b/.playwright-cli/console-2026-06-05T01-47-44-930Z.lognew file mode 100644index 0000000..5a86314--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-47-44-930Z.log@@ -0,0 +1 @@+[ 194ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-47-56-694Z.log b/.playwright-cli/console-2026-06-05T01-47-56-694Z.lognew file mode 100644index 0000000..1b0a4f1--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-47-56-694Z.log@@ -0,0 +1,17 @@+[ 244ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 518ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-48-48-391Z.log b/.playwright-cli/console-2026-06-05T01-48-48-391Z.lognew file mode 100644index 0000000..80f349b--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-48-391Z.log@@ -0,0 +1,17 @@+[ 147ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 299ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-48-50-818Z.log b/.playwright-cli/console-2026-06-05T01-48-50-818Z.lognew file mode 100644index 0000000..0b0f287--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-50-818Z.log@@ -0,0 +1,17 @@+[ 147ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 270ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-48-52-243Z.log b/.playwright-cli/console-2026-06-05T01-48-52-243Z.lognew file mode 100644index 0000000..16d58d0--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-52-243Z.log@@ -0,0 +1,17 @@+[ 144ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 284ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-48-54-676Z.log b/.playwright-cli/console-2026-06-05T01-48-54-676Z.lognew file mode 100644index 0000000..d64e682--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-54-676Z.log@@ -0,0 +1,17 @@+[ 149ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 256ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408diff --git a/.playwright-cli/console-2026-06-05T01-48-56-078Z.log b/.playwright-cli/console-2026-06-05T01-48-56-078Z.lognew file mode 100644index 0000000..bed30ad--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-56-078Z.log@@ -0,0 +1 @@+[ 151ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-48-57-486Z.log b/.playwright-cli/console-2026-06-05T01-48-57-486Z.lognew file mode 100644index 0000000..b90760b--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-48-57-486Z.log@@ -0,0 +1 @@+[ 149ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-49-20-243Z.log b/.playwright-cli/console-2026-06-05T01-49-20-243Z.lognew file mode 100644index 0000000..93b078d--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-49-20-243Z.log@@ -0,0 +1,21 @@+[ 246ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 482ms] [ERROR] %o++%s++%s+ TypeError: Cannot read properties of undefined (reading 'id')+ at DocItem (webpack-internal:///./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@sw_b13344762f2126093fd1f2df394b82c0/node_modules/@docusaurus/theme-classic/lib/theme/DocItem/index.js:17:89)+ at Object.react_stack_bottom_frame (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:25903:20)+ at renderWithHooks (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:7661:22)+ at updateFunctionComponent (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:10165:19)+ at beginWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:11777:18)+ at runWithFiberInDEV (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:871:30)+ at performUnitOfWork (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17640:22)+ at workLoopSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17468:41)+ at renderRootSync (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:17449:11)+ at performWorkOnRoot (webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:16582:35) The above error occurred in the <DocItem> component. React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:9408+[ 42759ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 42761ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607+[ 44859ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 44859ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607diff --git a/.playwright-cli/console-2026-06-05T01-50-45-859Z.log b/.playwright-cli/console-2026-06-05T01-50-45-859Z.lognew file mode 100644index 0000000..1282322--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-50-45-859Z.log@@ -0,0 +1 @@+[ 291ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002diff --git a/.playwright-cli/console-2026-06-05T01-50-48-910Z.log b/.playwright-cli/console-2026-06-05T01-50-48-910Z.lognew file mode 100644index 0000000..9295679--- /dev/null+++ b/.playwright-cli/console-2026-06-05T01-50-48-910Z.log@@ -0,0 +1,11 @@+[ 177ms] [INFO] %cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools font-weight:bold @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-client.development.js:28002+[ 16335ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 16337ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607+[ 18389ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 18390ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607+[ 22408ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error during WebSocket handshake: Unexpected response code: 404 @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 22408ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607+[ 30437ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error during WebSocket handshake: Unexpected response code: 404 @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 30438ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607+[ 46518ms] [ERROR] WebSocket connection to 'ws://localhost:3002/ws' failed: Error during WebSocket handshake: Unexpected response code: 404 @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/clients/WebSocketClient.js:24+[ 46519ms] [ERROR] [webpack-dev-server] Event @ webpack-internal:///./node_modules/.pnpm/[email protected][email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/webpack-dev-server/client/modules/logger/index.js:607diff --git a/.playwright-cli/page-2026-06-05T01-46-10-841Z.yml b/.playwright-cli/page-2026-06-05T01-46-10-841Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-46-42-131Z.yml b/.playwright-cli/page-2026-06-05T01-46-42-131Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-46-44-535Z.yml b/.playwright-cli/page-2026-06-05T01-46-44-535Z.ymlnew file mode 100644index 0000000..597c256--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-46-44-535Z.yml@@ -0,0 +1,35 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [disabled] [ref=e26]:+ - img [ref=e27]+ - main [ref=e30]:+ - generic [ref=e32]:+ - heading "找不到页面" [level=1] [ref=e33]+ - paragraph [ref=e34]: 我们找不到您要找的页面。+ - paragraph [ref=e35]: 请联系原始链接来源网站的所有者,并告知他们链接已损坏。\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-47-45-236Z.yml b/.playwright-cli/page-2026-06-05T01-47-45-236Z.ymlnew file mode 100644index 0000000..597c256--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-47-45-236Z.yml@@ -0,0 +1,35 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [disabled] [ref=e26]:+ - img [ref=e27]+ - main [ref=e30]:+ - generic [ref=e32]:+ - heading "找不到页面" [level=1] [ref=e33]+ - paragraph [ref=e34]: 我们找不到您要找的页面。+ - paragraph [ref=e35]: 请联系原始链接来源网站的所有者,并告知他们链接已损坏。\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-47-57-101Z.yml b/.playwright-cli/page-2026-06-05T01-47-57-101Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-48-48-636Z.yml b/.playwright-cli/page-2026-06-05T01-48-48-636Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-48-51-059Z.yml b/.playwright-cli/page-2026-06-05T01-48-51-059Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-48-52-478Z.yml b/.playwright-cli/page-2026-06-05T01-48-52-478Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-48-54-942Z.yml b/.playwright-cli/page-2026-06-05T01-48-54-942Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-48-56-308Z.yml b/.playwright-cli/page-2026-06-05T01-48-56-308Z.ymlnew file mode 100644index 0000000..597c256--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-48-56-308Z.yml@@ -0,0 +1,35 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [disabled] [ref=e26]:+ - img [ref=e27]+ - main [ref=e30]:+ - generic [ref=e32]:+ - heading "找不到页面" [level=1] [ref=e33]+ - paragraph [ref=e34]: 我们找不到您要找的页面。+ - paragraph [ref=e35]: 请联系原始链接来源网站的所有者,并告知他们链接已损坏。\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-48-57-733Z.yml b/.playwright-cli/page-2026-06-05T01-48-57-733Z.ymlnew file mode 100644index 0000000..597c256--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-48-57-733Z.yml@@ -0,0 +1,35 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [disabled] [ref=e26]:+ - img [ref=e27]+ - main [ref=e30]:+ - generic [ref=e32]:+ - heading "找不到页面" [level=1] [ref=e33]+ - paragraph [ref=e34]: 我们找不到您要找的页面。+ - paragraph [ref=e35]: 请联系原始链接来源网站的所有者,并告知他们链接已损坏。\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-49-20-655Z.yml b/.playwright-cli/page-2026-06-05T01-49-20-655Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-50-46-359Z.yml b/.playwright-cli/page-2026-06-05T01-50-46-359Z.ymlnew file mode 100644index 0000000..e69de29diff --git a/.playwright-cli/page-2026-06-05T01-50-49-183Z.yml b/.playwright-cli/page-2026-06-05T01-50-49-183Z.ymlnew file mode 100644index 0000000..17935ae--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-50-49-183Z.yml@@ -0,0 +1,35 @@+- generic [ref=e2]:+ - region "Skip to main content":+ - link "Skip to main content" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "Main" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "Docs" [ref=e11] [cursor=pointer]:+ - /url: /docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /docs/api/+ - link "Recipes" [ref=e13] [cursor=pointer]:+ - /url: /docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /changelog+ - generic [ref=e15]:+ - button "0.7 (latest)" [ref=e17] [cursor=pointer]+ - button "English" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: English+ - link "GitHub(opens in new tab)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(opens in new tab)" [ref=e23]+ - button "Switch between dark and light mode (currently system mode)" [disabled] [ref=e26]:+ - img [ref=e27]+ - main [ref=e30]:+ - generic [ref=e32]:+ - heading "Page Not Found" [level=1] [ref=e33]+ - paragraph [ref=e34]: We could not find what you were looking for.+ - paragraph [ref=e35]: Please contact the owner of the site that linked you to the original URL and let them know their link is broken.\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-51-50-873Z.yml b/.playwright-cli/page-2026-06-05T01-51-50-873Z.ymlnew file mode 100644index 0000000..d361329--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-51-50-873Z.yml@@ -0,0 +1,269 @@+- generic [ref=e2]:+ - region "Skip to main content":+ - link "Skip to main content" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "Main" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "Docs" [ref=e11] [cursor=pointer]:+ - /url: /docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /docs/api/+ - link "Recipes" [ref=e13] [cursor=pointer]:+ - /url: /docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /changelog+ - generic [ref=e15]:+ - button "0.7 (latest)" [ref=e17] [cursor=pointer]+ - button "English" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: English+ - link "GitHub(opens in new tab)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(opens in new tab)" [ref=e23]+ - button "Switch between dark and light mode (currently system mode)" [ref=e26] [cursor=pointer]:+ - img [ref=e27]+ - generic [ref=e31]:+ - complementary [ref=e32]:+ - navigation "Docs sidebar" [ref=e35]:+ - list [ref=e36]:+ - listitem [ref=e37]:+ - link "CubePi — Pythonic async-native agent framework" [ref=e38] [cursor=pointer]:+ - /url: /docs/+ - generic "CubePi — Pythonic async-native agent framework" [ref=e39]+ - listitem [ref=e40]:+ - button "Getting Started" [expanded] [ref=e42] [cursor=pointer]:+ - generic "Getting Started" [ref=e43]+ - list [ref=e44]:+ - listitem [ref=e45]:+ - link "Installation" [ref=e46] [cursor=pointer]:+ - /url: /docs/getting-started/installation+ - generic "Installation" [ref=e47]+ - listitem [ref=e48]:+ - link "Quick Start" [ref=e49] [cursor=pointer]:+ - /url: /docs/getting-started/quick-start+ - generic "Quick Start" [ref=e50]+ - listitem [ref=e51]:+ - link "Core Concepts" [ref=e52] [cursor=pointer]:+ - /url: /docs/getting-started/core-concepts+ - generic "Core Concepts" [ref=e53]+ - listitem [ref=e54]:+ - button "Guides" [ref=e56] [cursor=pointer]:+ - generic "Guides" [ref=e57]+ - listitem [ref=e58]:+ - generic [ref=e59]:+ - link "API Reference" [ref=e60] [cursor=pointer]:+ - /url: /docs/api/+ - generic "API Reference" [ref=e61]+ - button "Expand sidebar category 'API Reference'" [ref=e62] [cursor=pointer]+ - listitem [ref=e63]:+ - button "Recipes" [ref=e65] [cursor=pointer]:+ - generic "Recipes" [ref=e66]+ - listitem [ref=e67]:+ - button "Migration" [ref=e69] [cursor=pointer]:+ - generic "Migration" [ref=e70]+ - main [ref=e71]:+ - generic [ref=e73]:+ - generic [ref=e75]:+ - article [ref=e76]:+ - navigation "Breadcrumbs" [ref=e77]:+ - list [ref=e78]:+ - listitem [ref=e79]:+ - link "Home page" [ref=e80] [cursor=pointer]:+ - /url: /+ - img [ref=e81]+ - listitem [ref=e83]:+ - generic [ref=e84]: CubePi — Pythonic async-native agent framework+ - generic [ref=e85]: "Version: 0.7 (latest)"+ - generic [ref=e86]:+ - heading "CubePi" [level=1] [ref=e88]+ - paragraph [ref=e89]:+ - strong [ref=e90]: CubePi+ - text: is a Pythonic, async-native agent framework designed for high performance, readability, and production-grade persistence. It provides a leaner alternative to graph-based agent runtimes by modeling agent logic as a linear+ - code [ref=e91]: while+ - text: loop that developers can easily trace and debug.+ - generic [ref=e93]:+ - code [ref=e95]:+ - generic [ref=e96]: pip install cubepi+ - button "Copy code to clipboard" [ref=e98] [cursor=pointer]:+ - generic [ref=e99]:+ - img [ref=e100]+ - img [ref=e102]+ - paragraph [ref=e104]:+ - text: Then jump to the+ - link "Quick Start" [ref=e105] [cursor=pointer]:+ - /url: /docs/getting-started/quick-start+ - text: to ship a working tool-using agent in under five minutes.+ - heading "What you getDirect link to What you get" [level=2] [ref=e106]:+ - text: What you get+ - link "Direct link to What you get" [ref=e107] [cursor=pointer]:+ - /url: "#what-you-get"+ - text: "#"+ - list [ref=e108]:+ - listitem [ref=e109]:+ - strong [ref=e110]: Plain async functions, not graph nodes.+ - text: The agent loop is a+ - code [ref=e111]: while+ - text: loop over message turns. You build tools as+ - code [ref=e112]: async def+ - text: functions; the framework routes calls and executes them in parallel.+ - listitem [ref=e113]:+ - strong [ref=e114]: One streaming pattern.+ - text: Every provider yields+ - code [ref=e115]: StreamEvent+ - text: s through a+ - code [ref=e116]: MessageStream+ - text: . You iterate it with+ - code [ref=e117]: async for+ - text: . No callback registries, no separate handler types for text vs. tools.+ - listitem [ref=e118]:+ - strong [ref=e119]: Append-only checkpointing.+ - text: Persistence writes the new messages on each turn, not the full transcript. O(1) DB I/O regardless of conversation length — SQLite for laptops, Postgres for production.+ - listitem [ref=e120]:+ - strong [ref=e121]: Native multi-provider.+ - text: Anthropic and OpenAI ship in the box, through a+ - code [ref=e122]: Provider+ - text: Protocol. Add a new provider in one class.+ - listitem [ref=e123]:+ - strong [ref=e124]: Eight-hook middleware.+ - code [ref=e125]: transform_context+ - text: ","+ - code [ref=e126]: convert_to_llm+ - text: ","+ - code [ref=e127]: transform_system_prompt+ - text: ","+ - code [ref=e128]: before_tool_call+ - text: ","+ - code [ref=e129]: after_tool_call+ - text: ","+ - code [ref=e130]: should_stop_after_turn+ - text: ","+ - code [ref=e131]: after_model_response+ - text: ","+ - code [ref=e132]: on_run_end+ - text: — each with explicit composition rules. No mystery node ordering.+ - listitem [ref=e133]:+ - strong [ref=e134]: MCP loaders.+ - text: Point at any+ - link "Model Context Protocol" [ref=e135] [cursor=pointer]:+ - /url: https://modelcontextprotocol.io+ - text: server (HTTP or stdio) and get back a list of+ - code [ref=e136]: AgentTool+ - text: s.+ - listitem [ref=e137]:+ - strong [ref=e138]: OpenTelemetry built in.+ - text: Attach a+ - code [ref=e139]: Tracer+ - text: and every prompt produces a tree of OTel spans aligned with the GenAI Semantic Conventions — works with Jaeger, Tempo, Honeycomb, Datadog, or any OTLP-compatible backend. No payloads recorded by default; opt in with+ - code [ref=e140]: record_content=True+ - text: and a+ - code [ref=e141]: redact+ - text: callback.+ - listitem [ref=e142]:+ - strong [ref=e143]: Human-in-the-Loop.+ - text: Suspend an agent mid-run, route a decision to a human (approve/reject/answer), then resume — with full checkpoint persistence so the agent survives a process restart while waiting.+ - heading "Where to go nextDirect link to Where to go next" [level=2] [ref=e144]:+ - text: Where to go next+ - link "Direct link to Where to go next" [ref=e145] [cursor=pointer]:+ - /url: "#where-to-go-next"+ - text: "#"+ - table [ref=e146]:+ - rowgroup [ref=e147]:+ - row "If you want to… Start here" [ref=e148]:+ - columnheader "If you want to…" [ref=e149]+ - columnheader "Start here" [ref=e150]+ - rowgroup [ref=e151]:+ - row "Install and run your first agent Getting Started → Installation" [ref=e152]:+ - cell "Install and run your first agent" [ref=e153]+ - cell "Getting Started → Installation" [ref=e154]:+ - link "Getting Started → Installation" [ref=e155] [cursor=pointer]:+ - /url: /docs/getting-started/installation+ - row "Understand the building blocks Getting Started → Core Concepts" [ref=e156]:+ - cell "Understand the building blocks" [ref=e157]+ - cell "Getting Started → Core Concepts" [ref=e158]:+ - link "Getting Started → Core Concepts" [ref=e159] [cursor=pointer]:+ - /url: /docs/getting-started/core-concepts+ - row "Wire a real tool-using agent Guides → Building Your First Agent" [ref=e160]:+ - cell "Wire a real tool-using agent" [ref=e161]+ - cell "Guides → Building Your First Agent" [ref=e162]:+ - link "Guides → Building Your First Agent" [ref=e163] [cursor=pointer]:+ - /url: /docs/guides/agents/first-agent+ - row "Persist a conversation across restarts Guides → SQLite Checkpointing" [ref=e164]:+ - cell "Persist a conversation across restarts" [ref=e165]+ - cell "Guides → SQLite Checkpointing" [ref=e166]:+ - link "Guides → SQLite Checkpointing" [ref=e167] [cursor=pointer]:+ - /url: /docs/guides/checkpointing/sqlite+ - row "Ship traces to Jaeger / Tempo / Honeycomb Guides → Tracing" [ref=e168]:+ - cell "Ship traces to Jaeger / Tempo / Honeycomb" [ref=e169]+ - cell "Guides → Tracing" [ref=e170]:+ - link "Guides → Tracing" [ref=e171] [cursor=pointer]:+ - /url: /docs/guides/tracing/overview+ - row "Look up a specific symbol API Reference" [ref=e172]:+ - cell "Look up a specific symbol" [ref=e173]+ - cell "API Reference" [ref=e174]:+ - link "API Reference" [ref=e175] [cursor=pointer]:+ - /url: /docs/api/+ - row "See full working examples Recipes" [ref=e176]:+ - cell "See full working examples" [ref=e177]+ - cell "Recipes" [ref=e178]:+ - link "Recipes" [ref=e179] [cursor=pointer]:+ - /url: /docs/recipes/weather-agent+ - row "Port an existing langgraph agent Migration → From langgraph" [ref=e180]:+ - cell "Port an existing langgraph agent" [ref=e181]+ - cell "Migration → From langgraph" [ref=e182]:+ - link "Migration → From langgraph" [ref=e183] [cursor=pointer]:+ - /url: /docs/migration/from-langgraph+ - heading "StatusDirect link to Status" [level=2] [ref=e184]:+ - text: Status+ - link "Direct link to Status" [ref=e185] [cursor=pointer]:+ - /url: "#status"+ - text: "#"+ - paragraph [ref=e186]:+ - text: You are reading the+ - strong [ref=e187]: CubePi 0.7+ - text: release docs. This is the latest stable documentation set for installing CubePi and building against the published 0.7 API.+ - paragraph [ref=e188]:+ - text: CubePi 0.7 adds provider-level+ - code [ref=e189]: generate()+ - text: ", compaction and subagent middleware,"+ - code [ref=e190]: on_run_end+ - text: ", typed provider errors,"+ - code [ref=e191]: Tracer.oneshot()+ - text: ", and checkpointed HITL run IDs. For unreleased changes on"+ - code [ref=e192]: main+ - text: ", switch to the"+ - strong [ref=e193]: Next+ - text: docs from the version picker.+ - paragraph [ref=e194]:+ - text: Source, issues, and discussion live on+ - link "GitHub" [ref=e195] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: .+ - complementary [ref=e196]:+ - generic [ref=e197]: Was this page helpful?+ - button "Yes" [ref=e198] [cursor=pointer]: 👍+ - button "No" [ref=e199] [cursor=pointer]: 👎+ - link "Edit this page" [ref=e203] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi/edit/main/website/versioned_docs/version-0.7/intro.mdx+ - img [ref=e204]+ - text: Edit this page+ - navigation "Docs pages" [ref=e208]:+ - link "Next Installation »" [ref=e209] [cursor=pointer]:+ - /url: /docs/getting-started/installation+ - generic [ref=e210]: Next+ - generic [ref=e211]: Installation »+ - list [ref=e214]:+ - listitem [ref=e215]:+ - link "What you get" [ref=e216] [cursor=pointer]:+ - /url: "#what-you-get"+ - listitem [ref=e217]:+ - link "Where to go next" [ref=e218] [cursor=pointer]:+ - /url: "#where-to-go-next"+ - listitem [ref=e219]:+ - link "Status" [ref=e220] [cursor=pointer]:+ - /url: "#status"\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-51-52-260Z.yml b/.playwright-cli/page-2026-06-05T01-51-52-260Z.ymlnew file mode 100644index 0000000..9b36659--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-51-52-260Z.yml@@ -0,0 +1,266 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [ref=e26] [cursor=pointer]:+ - img [ref=e27]+ - generic [ref=e31]:+ - complementary [ref=e32]:+ - navigation "文档侧边栏" [ref=e35]:+ - list [ref=e36]:+ - listitem [ref=e37]:+ - link "CubePi" [ref=e38] [cursor=pointer]:+ - /url: /zh-Hans/docs/+ - generic "CubePi" [ref=e39]+ - listitem [ref=e40]:+ - button "快速入门" [expanded] [ref=e42] [cursor=pointer]:+ - generic "快速入门" [ref=e43]+ - list [ref=e44]:+ - listitem [ref=e45]:+ - link "安装" [ref=e46] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - generic "安装" [ref=e47]+ - listitem [ref=e48]:+ - link "快速开始" [ref=e49] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - generic "快速开始" [ref=e50]+ - listitem [ref=e51]:+ - link "核心概念" [ref=e52] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - generic "核心概念" [ref=e53]+ - listitem [ref=e54]:+ - button "指南" [ref=e56] [cursor=pointer]:+ - generic "指南" [ref=e57]+ - listitem [ref=e58]:+ - generic [ref=e59]:+ - link "API Reference" [ref=e60] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - generic "API Reference" [ref=e61]+ - button "展开侧边栏分类 'API Reference'" [ref=e62] [cursor=pointer]+ - listitem [ref=e63]:+ - button "示例" [ref=e65] [cursor=pointer]:+ - generic "示例" [ref=e66]+ - listitem [ref=e67]:+ - button "迁移指南" [ref=e69] [cursor=pointer]:+ - generic "迁移指南" [ref=e70]+ - main [ref=e71]:+ - generic [ref=e73]:+ - generic [ref=e75]:+ - article [ref=e76]:+ - navigation "页面路径" [ref=e77]:+ - list [ref=e78]:+ - listitem [ref=e79]:+ - link "主页面" [ref=e80] [cursor=pointer]:+ - /url: /zh-Hans/+ - img [ref=e81]+ - listitem [ref=e83]:+ - generic [ref=e84]: CubePi+ - generic [ref=e85]: 版本:0.7(最新)+ - generic [ref=e86]:+ - heading "CubePi" [level=1] [ref=e88]+ - paragraph [ref=e89]:+ - strong [ref=e90]: CubePi+ - text: 是一个 Pythonic、原生异步的 Agent 框架,为高性能、可读性和 生产级持久化而设计。它把一次 Agent 运行建模为一个线性的+ - code [ref=e91]: while+ - text: 循环——可追踪、可调试,没有 DAG、没有图 DSL、没有出人意料的控制流。 整个核心循环你五分钟就能读完。+ - generic [ref=e93]:+ - code [ref=e95]:+ - generic [ref=e96]: pip install cubepi+ - button "复制代码到剪贴板" [ref=e98] [cursor=pointer]:+ - generic [ref=e99]:+ - img [ref=e100]+ - img [ref=e102]+ - paragraph [ref=e104]:+ - text: 然后跳到+ - link "快速开始" [ref=e105] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - text: ,五分钟内跑通一个 能调用工具的 Agent。+ - heading "你能拿到什么你能拿到什么的直接链接" [level=2] [ref=e106]:+ - text: 你能拿到什么+ - link "你能拿到什么的直接链接" [ref=e107] [cursor=pointer]:+ - /url: "#你能拿到什么"+ - text: "#"+ - list [ref=e108]:+ - listitem [ref=e109]:+ - strong [ref=e110]: 普通 async 函数,不是图节点。+ - text: Agent 循环就是一个对消息轮次的+ - code [ref=e111]: while+ - text: 循环。你把工具写成+ - code [ref=e112]: async def+ - text: 函数,框架负责路由调用并并行 执行。+ - listitem [ref=e113]:+ - strong [ref=e114]: 统一的流式模式。+ - text: 所有 provider 都通过+ - code [ref=e115]: MessageStream+ - text: 产出+ - code [ref=e116]: StreamEvent+ - text: 。你用+ - code [ref=e117]: async for+ - text: 迭代它。没有 callback 注册表,也没有 为文本和工具单独的 handler 类型。+ - listitem [ref=e118]:+ - strong [ref=e119]: 追加式检查点。+ - text: 每一轮只写入新消息,而不是整段对话历史。无论 会话多长,数据库 I/O 都是 O(1)——SQLite 适合个人开发,Postgres 适合 生产环境。+ - listitem [ref=e120]:+ - strong [ref=e121]: 原生多 provider。+ - text: Anthropic 和 OpenAI 开箱即用,通过+ - code [ref=e122]: Provider+ - text: Protocol 接入。要新增一个 provider 只需要写一个类。+ - listitem [ref=e123]:+ - strong [ref=e124]: 八个 Hook 的中间件。+ - code [ref=e125]: transform_context+ - text: 、+ - code [ref=e126]: convert_to_llm+ - text: 、+ - code [ref=e127]: transform_system_prompt+ - text: 、+ - code [ref=e128]: before_tool_call+ - text: 、+ - code [ref=e129]: after_tool_call+ - text: 、+ - code [ref=e130]: should_stop_after_turn+ - text: 、+ - code [ref=e131]: after_model_response+ - text: 、+ - code [ref=e132]: on_run_end+ - text: —— 每一个都有明确的组合规则。没有"节点执行顺序之谜"。+ - listitem [ref=e133]:+ - strong [ref=e134]: MCP 加载器。+ - text: 指向任意+ - link "Model Context Protocol" [ref=e135] [cursor=pointer]:+ - /url: https://modelcontextprotocol.io+ - text: 服务器 (HTTP 或 stdio),就能拿回一组+ - code [ref=e136]: AgentTool+ - text: 。+ - listitem [ref=e137]:+ - strong [ref=e138]: 原生 OpenTelemetry。+ - text: 挂一个+ - code [ref=e139]: Tracer+ - text: ",每次 prompt 就产生符合 GenAI 语义约定的 OTel span 树 —— Jaeger、Tempo、Honeycomb、 Datadog 等任意 OTLP 后端都能直接接收。默认不记录任何 payload; 需要时用"+ - code [ref=e140]: record_content=True+ - text: 配合+ - code [ref=e141]: redact+ - text: 回调显式开启。+ - listitem [ref=e142]:+ - strong [ref=e143]: 人机协同(HITL)。+ - text: 在 run 中途挂起 Agent,把决策路由给人工 (审批 / 拒绝 / 回答),然后继续执行 —— 支持检查点持久化, 等待期间进程可以重启。+ - heading "接下来去哪儿接下来去哪儿的直接链接" [level=2] [ref=e144]:+ - text: 接下来去哪儿+ - link "接下来去哪儿的直接链接" [ref=e145] [cursor=pointer]:+ - /url: "#接下来去哪儿"+ - text: "#"+ - table [ref=e146]:+ - rowgroup [ref=e147]:+ - row "如果你想… 从这里开始" [ref=e148]:+ - columnheader "如果你想…" [ref=e149]+ - columnheader "从这里开始" [ref=e150]+ - rowgroup [ref=e151]:+ - row "安装并跑通第一个 Agent 入门 → 安装" [ref=e152]:+ - cell "安装并跑通第一个 Agent" [ref=e153]+ - cell "入门 → 安装" [ref=e154]:+ - link "入门 → 安装" [ref=e155] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - row "理解骨架 入门 → 核心概念" [ref=e156]:+ - cell "理解骨架" [ref=e157]+ - cell "入门 → 核心概念" [ref=e158]:+ - link "入门 → 核心概念" [ref=e159] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - row "接入一个真实的工具 Agent 指南 → 构建第一个 Agent" [ref=e160]:+ - cell "接入一个真实的工具 Agent" [ref=e161]+ - cell "指南 → 构建第一个 Agent" [ref=e162]:+ - link "指南 → 构建第一个 Agent" [ref=e163] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/agents/first-agent+ - row "跨重启保留对话 指南 → SQLite 检查点" [ref=e164]:+ - cell "跨重启保留对话" [ref=e165]+ - cell "指南 → SQLite 检查点" [ref=e166]:+ - link "指南 → SQLite 检查点" [ref=e167] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/checkpointing/sqlite+ - row "查找某个具体符号 API 参考" [ref=e168]:+ - cell "查找某个具体符号" [ref=e169]+ - cell "API 参考" [ref=e170]:+ - link "API 参考" [ref=e171] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - row "看完整的可运行示例 Recipes" [ref=e172]:+ - cell "看完整的可运行示例" [ref=e173]+ - cell "Recipes" [ref=e174]:+ - link "Recipes" [ref=e175] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - row "从 langgraph Agent 迁移 迁移 → 来自 langgraph" [ref=e176]:+ - cell "从 langgraph Agent 迁移" [ref=e177]+ - cell "迁移 → 来自 langgraph" [ref=e178]:+ - link "迁移 → 来自 langgraph" [ref=e179] [cursor=pointer]:+ - /url: /zh-Hans/docs/migration/from-langgraph+ - heading "状态状态的直接链接" [level=2] [ref=e180]:+ - text: 状态+ - link "状态的直接链接" [ref=e181] [cursor=pointer]:+ - /url: "#状态"+ - text: "#"+ - paragraph [ref=e182]:+ - text: 你正在浏览的是+ - strong [ref=e183]: CubePi 0.7+ - text: 发布版文档。这是当前最新的稳定文档, 适用于安装 CubePi 并基于已发布的 0.7 API 构建应用。+ - paragraph [ref=e184]:+ - text: CubePi 0.7 新增 provider 级+ - code [ref=e185]: generate()+ - text: 、压缩和子 Agent 中间件、+ - code [ref=e186]: on_run_end+ - text: 、类型化 provider 错误、+ - code [ref=e187]: Tracer.oneshot()+ - text: ,以及带+ - code [ref=e188]: run_id+ - text: 的 checkpointed HITL 恢复能力。未发布的+ - code [ref=e189]: main+ - text: 分支变更请从右上角版本 选择器切到+ - strong [ref=e190]: Next+ - text: 文档。+ - paragraph [ref=e191]:+ - text: 源代码、Issue 和讨论都在+ - link "GitHub" [ref=e192] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: 。+ - complementary [ref=e193]:+ - generic [ref=e194]: Was this page helpful?+ - button "Yes" [ref=e195] [cursor=pointer]: 👍+ - button "No" [ref=e196] [cursor=pointer]: 👎+ - link "编辑此页" [ref=e200] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi/edit/main/website/versioned_docs/version-0.7/intro.mdx+ - img [ref=e201]+ - text: 编辑此页+ - navigation "文件选项卡" [ref=e205]:+ - link "下一页 安装 »" [ref=e206] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - generic [ref=e207]: 下一页+ - generic [ref=e208]: 安装 »+ - list [ref=e211]:+ - listitem [ref=e212]:+ - link "你能拿到什么" [ref=e213] [cursor=pointer]:+ - /url: "#你能拿到什么"+ - listitem [ref=e214]:+ - link "接下来去哪儿" [ref=e215] [cursor=pointer]:+ - /url: "#接下来去哪儿"+ - listitem [ref=e216]:+ - link "状态" [ref=e217] [cursor=pointer]:+ - /url: "#状态"\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-51-53-585Z.yml b/.playwright-cli/page-2026-06-05T01-51-53-585Z.ymlnew file mode 100644index 0000000..20bb299--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-51-53-585Z.yml@@ -0,0 +1,325 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [ref=e26] [cursor=pointer]:+ - img [ref=e27]+ - generic [ref=e31]:+ - complementary [ref=e32]:+ - navigation "文档侧边栏" [ref=e35]:+ - list [ref=e36]:+ - listitem [ref=e37]:+ - link "CubePi" [ref=e38] [cursor=pointer]:+ - /url: /zh-Hans/docs/+ - generic "CubePi" [ref=e39]+ - listitem [ref=e40]:+ - button "快速入门" [expanded] [ref=e42] [cursor=pointer]:+ - generic "快速入门" [ref=e43]+ - list [ref=e44]:+ - listitem [ref=e45]:+ - link "安装" [ref=e46] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - generic "安装" [ref=e47]+ - listitem [ref=e48]:+ - link "快速开始" [ref=e49] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - generic "快速开始" [ref=e50]+ - listitem [ref=e51]:+ - link "核心概念" [ref=e52] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - generic "核心概念" [ref=e53]+ - listitem [ref=e54]:+ - button "指南" [ref=e56] [cursor=pointer]:+ - generic "指南" [ref=e57]+ - listitem [ref=e58]:+ - generic [ref=e59]:+ - link "API Reference" [ref=e60] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - generic "API Reference" [ref=e61]+ - button "展开侧边栏分类 'API Reference'" [ref=e62] [cursor=pointer]+ - listitem [ref=e63]:+ - button "示例" [ref=e65] [cursor=pointer]:+ - generic "示例" [ref=e66]+ - listitem [ref=e67]:+ - button "迁移指南" [ref=e69] [cursor=pointer]:+ - generic "迁移指南" [ref=e70]+ - main [ref=e71]:+ - generic [ref=e73]:+ - generic [ref=e75]:+ - article [ref=e76]:+ - navigation "页面路径" [ref=e77]:+ - list [ref=e78]:+ - listitem [ref=e79]:+ - link "主页面" [ref=e80] [cursor=pointer]:+ - /url: /zh-Hans/+ - img [ref=e81]+ - listitem [ref=e83]:+ - generic [ref=e84]: 快速入门+ - listitem [ref=e85]:+ - generic [ref=e86]: 安装+ - generic [ref=e87]: 版本:0.7(最新)+ - generic [ref=e88]:+ - heading "安装" [level=1] [ref=e90]+ - paragraph [ref=e91]:+ - text: CubePi 需要+ - strong [ref=e92]: Python 3.11 或以上+ - text: 。核心运行时只有三个依赖:+ - code [ref=e93]: pydantic+ - text: 、+ - code [ref=e94]: anthropic+ - text: 、+ - code [ref=e95]: openai+ - text: 。可选功能(SQLite、Postgres、MCP、OpenTelemetry 追踪)通过 extras 按需安装,不用的话不会被拉进来。+ - heading "使用 pip使用 pip的直接链接" [level=2] [ref=e96]:+ - text: 使用 pip+ - link "使用 pip的直接链接" [ref=e97] [cursor=pointer]:+ - /url: "#使用-pip"+ - text: "#"+ - generic [ref=e99]:+ - code [ref=e101]:+ - generic [ref=e102]: pip install cubepi+ - button "复制代码到剪贴板" [ref=e104] [cursor=pointer]:+ - generic [ref=e105]:+ - img [ref=e106]+ - img [ref=e108]+ - paragraph [ref=e110]: "可选 extras:"+ - generic [ref=e112]:+ - code [ref=e114]:+ - generic [ref=e115]: "pip install \"cubepi[sqlite]\" # 安装 aiosqlite,启用 SQLiteCheckpointer"+ - generic [ref=e116]: "pip install \"cubepi[postgres]\" # 安装 asyncpg + sqlalchemy + msgpack"+ - generic [ref=e117]: "pip install \"cubepi[mcp]\" # 安装 MCP SDK,启用 MCP 工具加载器"+ - generic [ref=e118]: "pip install \"cubepi[tracing]\" # 安装 opentelemetry-sdk,启用 Tracer / Meter"+ - generic [ref=e119]: "pip install \"cubepi[tracing-otlp]\" # 加上 OTLP/HTTP 导出器"+ - generic [ref=e120]: "pip install \"cubepi[sqlite,mcp,tracing]\" # 组合"+ - button "复制代码到剪贴板" [ref=e122] [cursor=pointer]:+ - generic [ref=e123]:+ - img [ref=e124]+ - img [ref=e126]+ - heading "使用 uv使用 uv的直接链接" [level=2] [ref=e128]:+ - text: 使用 uv+ - link "使用 uv的直接链接" [ref=e129] [cursor=pointer]:+ - /url: "#使用-uv"+ - text: "#"+ - paragraph [ref=e130]:+ - link "uv" [ref=e131] [cursor=pointer]:+ - /url: https://github.com/astral-sh/uv+ - code [ref=e132]: uv+ - text: 比 pip 快很多,是推荐的工作流:+ - generic [ref=e134]:+ - code [ref=e136]:+ - generic [ref=e137]: uv add cubepi+ - generic [ref=e138]: uv add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+ - button "复制代码到剪贴板" [ref=e140] [cursor=pointer]:+ - generic [ref=e141]:+ - img [ref=e142]+ - img [ref=e144]+ - paragraph [ref=e146]:+ - text: 在已有 uv 项目里,改完+ - code [ref=e147]: pyproject.toml+ - text: 后+ - code [ref=e148]: uv sync+ - text: 会重新锁定环境。+ - heading "使用 Poetry使用 Poetry的直接链接" [level=2] [ref=e149]:+ - text: 使用 Poetry+ - link "使用 Poetry的直接链接" [ref=e150] [cursor=pointer]:+ - /url: "#使用-poetry"+ - text: "#"+ - generic [ref=e152]:+ - code [ref=e154]:+ - generic [ref=e155]: poetry add cubepi+ - generic [ref=e156]: poetry add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+ - button "复制代码到剪贴板" [ref=e158] [cursor=pointer]:+ - generic [ref=e159]:+ - img [ref=e160]+ - img [ref=e162]+ - heading "验证安装验证安装的直接链接" [level=2] [ref=e164]:+ - text: 验证安装+ - link "验证安装的直接链接" [ref=e165] [cursor=pointer]:+ - /url: "#验证安装"+ - text: "#"+ - generic [ref=e167]:+ - code [ref=e169]:+ - generic [ref=e170]: python -c "import cubepi; print(cubepi.__doc__)"+ - generic [ref=e171]: "# cubepi — Pythonic async-native agent framework."+ - button "复制代码到剪贴板" [ref=e173] [cursor=pointer]:+ - generic [ref=e174]:+ - img [ref=e175]+ - img [ref=e177]+ - paragraph [ref=e179]:+ - text: 如果报+ - code [ref=e180]: ImportError+ - text: ",大概率是解释器版本低于 3.11——用"+ - code [ref=e181]: python --version+ - text: 确认一下。+ - heading "配置 provider 凭据配置 provider 凭据的直接链接" [level=2] [ref=e182]:+ - text: 配置 provider 凭据+ - link "配置 provider 凭据的直接链接" [ref=e183] [cursor=pointer]:+ - /url: "#配置-provider-凭据"+ - text: "#"+ - paragraph [ref=e184]: CubePi 的 provider 从构造函数参数读取凭据。大多数部署会从环境变量 取出来:+ - generic [ref=e186]:+ - code [ref=e188]:+ - generic [ref=e189]: import os+ - generic [ref=e190]: from cubepi.providers.anthropic import AnthropicProvider+ - generic [ref=e191]: from cubepi.providers.openai import OpenAIProvider+ - generic [ref=e193]: anthropic = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ - generic [ref=e194]: openai = OpenAIProvider(api_key=os.environ["OPENAI_API_KEY"])+ - button "复制代码到剪贴板" [ref=e196] [cursor=pointer]:+ - generic [ref=e197]:+ - img [ref=e198]+ - img [ref=e200]+ - paragraph [ref=e202]:+ - text: 你也可以传+ - code [ref=e203]: base_url=...+ - text: 指向自托管端点或兼容代理(如 Anthropic Bedrock、 LiteLLM、vLLM)。+ - paragraph [ref=e204]:+ - link "FauxProvider" [ref=e205] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/providers/custom#using-fauxprovider-in-tests+ - text: (用于测试)不需要任何凭据。+ - heading "extras 选择指南extras 选择指南的直接链接" [level=2] [ref=e206]:+ - text: extras 选择指南+ - link "extras 选择指南的直接链接" [ref=e207] [cursor=pointer]:+ - /url: "#extras-选择指南"+ - text: "#"+ - table [ref=e208]:+ - rowgroup [ref=e209]:+ - row "Extra 拉入什么 什么时候装" [ref=e210]:+ - columnheader "Extra" [ref=e211]+ - columnheader "拉入什么" [ref=e212]+ - columnheader "什么时候装" [ref=e213]+ - rowgroup [ref=e214]:+ - row "(无) 仅核心 只需要内存里的状态,不用 MCP,不用追踪" [ref=e215]:+ - cell "(无)" [ref=e216]+ - cell "仅核心" [ref=e217]+ - cell "只需要内存里的状态,不用 MCP,不用追踪" [ref=e218]+ - row "[sqlite] aiosqlite 单进程应用需要落盘" [ref=e219]:+ - cell "[sqlite]" [ref=e220]:+ - code [ref=e221]: "[sqlite]"+ - cell "aiosqlite" [ref=e222]:+ - code [ref=e223]: aiosqlite+ - cell "单进程应用需要落盘" [ref=e224]+ - row "[postgres] asyncpg、sqlalchemy、msgpack 多实例 / 生产环境——见 Postgres 指南" [ref=e225]:+ - cell "[postgres]" [ref=e226]:+ - code [ref=e227]: "[postgres]"+ - cell "asyncpg、sqlalchemy、msgpack" [ref=e228]:+ - code [ref=e229]: asyncpg+ - text: 、+ - code [ref=e230]: sqlalchemy+ - text: 、+ - code [ref=e231]: msgpack+ - cell "多实例 / 生产环境——见 Postgres 指南" [ref=e232]:+ - text: 多实例 / 生产环境——见+ - link "Postgres 指南" [ref=e233] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/checkpointing/postgres+ - row "[mcp] mcp 想把 MCP server 工具挂到 Agent 上" [ref=e234]:+ - cell "[mcp]" [ref=e235]:+ - code [ref=e236]: "[mcp]"+ - cell "mcp" [ref=e237]:+ - code [ref=e238]: mcp+ - cell "想把 MCP server 工具挂到 Agent 上" [ref=e239]+ - row "[tracing] opentelemetry-sdk 想要 OpenTelemetry 追踪(含可选指标)——见 追踪指南" [ref=e240]:+ - cell "[tracing]" [ref=e241]:+ - code [ref=e242]: "[tracing]"+ - cell "opentelemetry-sdk" [ref=e243]:+ - code [ref=e244]: opentelemetry-sdk+ - cell "想要 OpenTelemetry 追踪(含可选指标)——见 追踪指南" [ref=e245]:+ - text: 想要 OpenTelemetry 追踪(含可选指标)——见+ - link "追踪指南" [ref=e246] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/tracing/overview+ - row "[tracing-otlp] opentelemetry-exporter-otlp-proto-http 把 trace 发往 OTLP/HTTP 后端(Jaeger ≥1.50、Tempo、Honeycomb、Datadog 等)" [ref=e247]:+ - cell "[tracing-otlp]" [ref=e248]:+ - code [ref=e249]: "[tracing-otlp]"+ - cell "opentelemetry-exporter-otlp-proto-http" [ref=e250]:+ - code [ref=e251]: opentelemetry-exporter-otlp-proto-http+ - cell "把 trace 发往 OTLP/HTTP 后端(Jaeger ≥1.50、Tempo、Honeycomb、Datadog 等)" [ref=e252]+ - row "[docs] griffe 仅文档站构建(贡献者用)" [ref=e253]:+ - cell "[docs]" [ref=e254]:+ - code [ref=e255]: "[docs]"+ - cell "griffe" [ref=e256]:+ - code [ref=e257]: griffe+ - cell "仅文档站构建(贡献者用)" [ref=e258]+ - heading "下一步下一步的直接链接" [level=2] [ref=e259]:+ - text: 下一步+ - link "下一步的直接链接" [ref=e260] [cursor=pointer]:+ - /url: "#下一步"+ - text: "#"+ - list [ref=e261]:+ - listitem [ref=e262]:+ - link "快速开始" [ref=e263] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - text: —— 五分钟内跑通你的第一个 Agent。+ - listitem [ref=e264]:+ - link "核心概念" [ref=e265] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - text: —— 在动手前先弄清+ - code [ref=e266]: Agent+ - text: /+ - code [ref=e267]: Tool+ - text: /+ - code [ref=e268]: Provider+ - text: /+ - code [ref=e269]: Checkpointer+ - text: 各自是什么。+ - complementary [ref=e270]:+ - generic [ref=e271]: Was this page helpful?+ - button "Yes" [ref=e272] [cursor=pointer]: 👍+ - button "No" [ref=e273] [cursor=pointer]: 👎+ - link "编辑此页" [ref=e277] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi/edit/main/website/versioned_docs/version-0.7/getting-started/installation.md+ - img [ref=e278]+ - text: 编辑此页+ - navigation "文件选项卡" [ref=e282]:+ - link "上一页 « CubePi" [ref=e283] [cursor=pointer]:+ - /url: /zh-Hans/docs/+ - generic [ref=e284]: 上一页+ - generic [ref=e285]: « CubePi+ - link "下一页 快速开始 »" [ref=e286] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - generic [ref=e287]: 下一页+ - generic [ref=e288]: 快速开始 »+ - list [ref=e291]:+ - listitem [ref=e292]:+ - link "使用 pip" [ref=e293] [cursor=pointer]:+ - /url: "#使用-pip"+ - listitem [ref=e294]:+ - link "使用 uv" [ref=e295] [cursor=pointer]:+ - /url: "#使用-uv"+ - listitem [ref=e296]:+ - link "使用 Poetry" [ref=e297] [cursor=pointer]:+ - /url: "#使用-poetry"+ - listitem [ref=e298]:+ - link "验证安装" [ref=e299] [cursor=pointer]:+ - /url: "#验证安装"+ - listitem [ref=e300]:+ - link "配置 provider 凭据" [ref=e301] [cursor=pointer]:+ - /url: "#配置-provider-凭据"+ - listitem [ref=e302]:+ - link "extras 选择指南" [ref=e303] [cursor=pointer]:+ - /url: "#extras-选择指南"+ - listitem [ref=e304]:+ - link "下一步" [ref=e305] [cursor=pointer]:+ - /url: "#下一步"\ No newline at end of filediff --git a/.playwright-cli/page-2026-06-05T01-52-04-726Z.yml b/.playwright-cli/page-2026-06-05T01-52-04-726Z.ymlnew file mode 100644index 0000000..9b36659--- /dev/null+++ b/.playwright-cli/page-2026-06-05T01-52-04-726Z.yml@@ -0,0 +1,266 @@+- generic [ref=e2]:+ - region "跳到主要内容":+ - link "跳到主要内容" [ref=e3] [cursor=pointer]:+ - /url: "#__docusaurus_skipToContent_fallback"+ - navigation "主导航" [ref=e4]:+ - generic [ref=e5]:+ - generic [ref=e6]:+ - link "CubePi logo CubePi" [ref=e7] [cursor=pointer]:+ - /url: /zh-Hans/+ - img "CubePi logo" [ref=e9]+ - generic [ref=e10]: CubePi+ - link "文档" [ref=e11] [cursor=pointer]:+ - /url: /zh-Hans/docs+ - link "API" [ref=e12] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - link "示例" [ref=e13] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - link "Changelog" [ref=e14] [cursor=pointer]:+ - /url: /zh-Hans/changelog+ - generic [ref=e15]:+ - button "0.7(最新)" [ref=e17] [cursor=pointer]+ - button "简体中文" [ref=e19] [cursor=pointer]:+ - img [ref=e20]+ - text: 简体中文+ - link "GitHub(在新标签页中打开)" [ref=e22] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: GitHub+ - img "(在新标签页中打开)" [ref=e23]+ - button "切换浅色/暗黑模式(当前为系统模式)" [ref=e26] [cursor=pointer]:+ - img [ref=e27]+ - generic [ref=e31]:+ - complementary [ref=e32]:+ - navigation "文档侧边栏" [ref=e35]:+ - list [ref=e36]:+ - listitem [ref=e37]:+ - link "CubePi" [ref=e38] [cursor=pointer]:+ - /url: /zh-Hans/docs/+ - generic "CubePi" [ref=e39]+ - listitem [ref=e40]:+ - button "快速入门" [expanded] [ref=e42] [cursor=pointer]:+ - generic "快速入门" [ref=e43]+ - list [ref=e44]:+ - listitem [ref=e45]:+ - link "安装" [ref=e46] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - generic "安装" [ref=e47]+ - listitem [ref=e48]:+ - link "快速开始" [ref=e49] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - generic "快速开始" [ref=e50]+ - listitem [ref=e51]:+ - link "核心概念" [ref=e52] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - generic "核心概念" [ref=e53]+ - listitem [ref=e54]:+ - button "指南" [ref=e56] [cursor=pointer]:+ - generic "指南" [ref=e57]+ - listitem [ref=e58]:+ - generic [ref=e59]:+ - link "API Reference" [ref=e60] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - generic "API Reference" [ref=e61]+ - button "展开侧边栏分类 'API Reference'" [ref=e62] [cursor=pointer]+ - listitem [ref=e63]:+ - button "示例" [ref=e65] [cursor=pointer]:+ - generic "示例" [ref=e66]+ - listitem [ref=e67]:+ - button "迁移指南" [ref=e69] [cursor=pointer]:+ - generic "迁移指南" [ref=e70]+ - main [ref=e71]:+ - generic [ref=e73]:+ - generic [ref=e75]:+ - article [ref=e76]:+ - navigation "页面路径" [ref=e77]:+ - list [ref=e78]:+ - listitem [ref=e79]:+ - link "主页面" [ref=e80] [cursor=pointer]:+ - /url: /zh-Hans/+ - img [ref=e81]+ - listitem [ref=e83]:+ - generic [ref=e84]: CubePi+ - generic [ref=e85]: 版本:0.7(最新)+ - generic [ref=e86]:+ - heading "CubePi" [level=1] [ref=e88]+ - paragraph [ref=e89]:+ - strong [ref=e90]: CubePi+ - text: 是一个 Pythonic、原生异步的 Agent 框架,为高性能、可读性和 生产级持久化而设计。它把一次 Agent 运行建模为一个线性的+ - code [ref=e91]: while+ - text: 循环——可追踪、可调试,没有 DAG、没有图 DSL、没有出人意料的控制流。 整个核心循环你五分钟就能读完。+ - generic [ref=e93]:+ - code [ref=e95]:+ - generic [ref=e96]: pip install cubepi+ - button "复制代码到剪贴板" [ref=e98] [cursor=pointer]:+ - generic [ref=e99]:+ - img [ref=e100]+ - img [ref=e102]+ - paragraph [ref=e104]:+ - text: 然后跳到+ - link "快速开始" [ref=e105] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/quick-start+ - text: ,五分钟内跑通一个 能调用工具的 Agent。+ - heading "你能拿到什么你能拿到什么的直接链接" [level=2] [ref=e106]:+ - text: 你能拿到什么+ - link "你能拿到什么的直接链接" [ref=e107] [cursor=pointer]:+ - /url: "#你能拿到什么"+ - text: "#"+ - list [ref=e108]:+ - listitem [ref=e109]:+ - strong [ref=e110]: 普通 async 函数,不是图节点。+ - text: Agent 循环就是一个对消息轮次的+ - code [ref=e111]: while+ - text: 循环。你把工具写成+ - code [ref=e112]: async def+ - text: 函数,框架负责路由调用并并行 执行。+ - listitem [ref=e113]:+ - strong [ref=e114]: 统一的流式模式。+ - text: 所有 provider 都通过+ - code [ref=e115]: MessageStream+ - text: 产出+ - code [ref=e116]: StreamEvent+ - text: 。你用+ - code [ref=e117]: async for+ - text: 迭代它。没有 callback 注册表,也没有 为文本和工具单独的 handler 类型。+ - listitem [ref=e118]:+ - strong [ref=e119]: 追加式检查点。+ - text: 每一轮只写入新消息,而不是整段对话历史。无论 会话多长,数据库 I/O 都是 O(1)——SQLite 适合个人开发,Postgres 适合 生产环境。+ - listitem [ref=e120]:+ - strong [ref=e121]: 原生多 provider。+ - text: Anthropic 和 OpenAI 开箱即用,通过+ - code [ref=e122]: Provider+ - text: Protocol 接入。要新增一个 provider 只需要写一个类。+ - listitem [ref=e123]:+ - strong [ref=e124]: 八个 Hook 的中间件。+ - code [ref=e125]: transform_context+ - text: 、+ - code [ref=e126]: convert_to_llm+ - text: 、+ - code [ref=e127]: transform_system_prompt+ - text: 、+ - code [ref=e128]: before_tool_call+ - text: 、+ - code [ref=e129]: after_tool_call+ - text: 、+ - code [ref=e130]: should_stop_after_turn+ - text: 、+ - code [ref=e131]: after_model_response+ - text: 、+ - code [ref=e132]: on_run_end+ - text: —— 每一个都有明确的组合规则。没有"节点执行顺序之谜"。+ - listitem [ref=e133]:+ - strong [ref=e134]: MCP 加载器。+ - text: 指向任意+ - link "Model Context Protocol" [ref=e135] [cursor=pointer]:+ - /url: https://modelcontextprotocol.io+ - text: 服务器 (HTTP 或 stdio),就能拿回一组+ - code [ref=e136]: AgentTool+ - text: 。+ - listitem [ref=e137]:+ - strong [ref=e138]: 原生 OpenTelemetry。+ - text: 挂一个+ - code [ref=e139]: Tracer+ - text: ",每次 prompt 就产生符合 GenAI 语义约定的 OTel span 树 —— Jaeger、Tempo、Honeycomb、 Datadog 等任意 OTLP 后端都能直接接收。默认不记录任何 payload; 需要时用"+ - code [ref=e140]: record_content=True+ - text: 配合+ - code [ref=e141]: redact+ - text: 回调显式开启。+ - listitem [ref=e142]:+ - strong [ref=e143]: 人机协同(HITL)。+ - text: 在 run 中途挂起 Agent,把决策路由给人工 (审批 / 拒绝 / 回答),然后继续执行 —— 支持检查点持久化, 等待期间进程可以重启。+ - heading "接下来去哪儿接下来去哪儿的直接链接" [level=2] [ref=e144]:+ - text: 接下来去哪儿+ - link "接下来去哪儿的直接链接" [ref=e145] [cursor=pointer]:+ - /url: "#接下来去哪儿"+ - text: "#"+ - table [ref=e146]:+ - rowgroup [ref=e147]:+ - row "如果你想… 从这里开始" [ref=e148]:+ - columnheader "如果你想…" [ref=e149]+ - columnheader "从这里开始" [ref=e150]+ - rowgroup [ref=e151]:+ - row "安装并跑通第一个 Agent 入门 → 安装" [ref=e152]:+ - cell "安装并跑通第一个 Agent" [ref=e153]+ - cell "入门 → 安装" [ref=e154]:+ - link "入门 → 安装" [ref=e155] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - row "理解骨架 入门 → 核心概念" [ref=e156]:+ - cell "理解骨架" [ref=e157]+ - cell "入门 → 核心概念" [ref=e158]:+ - link "入门 → 核心概念" [ref=e159] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/core-concepts+ - row "接入一个真实的工具 Agent 指南 → 构建第一个 Agent" [ref=e160]:+ - cell "接入一个真实的工具 Agent" [ref=e161]+ - cell "指南 → 构建第一个 Agent" [ref=e162]:+ - link "指南 → 构建第一个 Agent" [ref=e163] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/agents/first-agent+ - row "跨重启保留对话 指南 → SQLite 检查点" [ref=e164]:+ - cell "跨重启保留对话" [ref=e165]+ - cell "指南 → SQLite 检查点" [ref=e166]:+ - link "指南 → SQLite 检查点" [ref=e167] [cursor=pointer]:+ - /url: /zh-Hans/docs/guides/checkpointing/sqlite+ - row "查找某个具体符号 API 参考" [ref=e168]:+ - cell "查找某个具体符号" [ref=e169]+ - cell "API 参考" [ref=e170]:+ - link "API 参考" [ref=e171] [cursor=pointer]:+ - /url: /zh-Hans/docs/api/+ - row "看完整的可运行示例 Recipes" [ref=e172]:+ - cell "看完整的可运行示例" [ref=e173]+ - cell "Recipes" [ref=e174]:+ - link "Recipes" [ref=e175] [cursor=pointer]:+ - /url: /zh-Hans/docs/recipes/weather-agent+ - row "从 langgraph Agent 迁移 迁移 → 来自 langgraph" [ref=e176]:+ - cell "从 langgraph Agent 迁移" [ref=e177]+ - cell "迁移 → 来自 langgraph" [ref=e178]:+ - link "迁移 → 来自 langgraph" [ref=e179] [cursor=pointer]:+ - /url: /zh-Hans/docs/migration/from-langgraph+ - heading "状态状态的直接链接" [level=2] [ref=e180]:+ - text: 状态+ - link "状态的直接链接" [ref=e181] [cursor=pointer]:+ - /url: "#状态"+ - text: "#"+ - paragraph [ref=e182]:+ - text: 你正在浏览的是+ - strong [ref=e183]: CubePi 0.7+ - text: 发布版文档。这是当前最新的稳定文档, 适用于安装 CubePi 并基于已发布的 0.7 API 构建应用。+ - paragraph [ref=e184]:+ - text: CubePi 0.7 新增 provider 级+ - code [ref=e185]: generate()+ - text: 、压缩和子 Agent 中间件、+ - code [ref=e186]: on_run_end+ - text: 、类型化 provider 错误、+ - code [ref=e187]: Tracer.oneshot()+ - text: ,以及带+ - code [ref=e188]: run_id+ - text: 的 checkpointed HITL 恢复能力。未发布的+ - code [ref=e189]: main+ - text: 分支变更请从右上角版本 选择器切到+ - strong [ref=e190]: Next+ - text: 文档。+ - paragraph [ref=e191]:+ - text: 源代码、Issue 和讨论都在+ - link "GitHub" [ref=e192] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi+ - text: 。+ - complementary [ref=e193]:+ - generic [ref=e194]: Was this page helpful?+ - button "Yes" [ref=e195] [cursor=pointer]: 👍+ - button "No" [ref=e196] [cursor=pointer]: 👎+ - link "编辑此页" [ref=e200] [cursor=pointer]:+ - /url: https://github.com/cubeplexai/cubepi/edit/main/website/versioned_docs/version-0.7/intro.mdx+ - img [ref=e201]+ - text: 编辑此页+ - navigation "文件选项卡" [ref=e205]:+ - link "下一页 安装 »" [ref=e206] [cursor=pointer]:+ - /url: /zh-Hans/docs/getting-started/installation+ - generic [ref=e207]: 下一页+ - generic [ref=e208]: 安装 »+ - list [ref=e211]:+ - listitem [ref=e212]:+ - link "你能拿到什么" [ref=e213] [cursor=pointer]:+ - /url: "#你能拿到什么"+ - listitem [ref=e214]:+ - link "接下来去哪儿" [ref=e215] [cursor=pointer]:+ - /url: "#接下来去哪儿"+ - listitem [ref=e216]:+ - link "状态" [ref=e217] [cursor=pointer]:+ - /url: "#状态"\ No newline at end of filediff --git a/AGENTS.md b/AGENTS.mdindex 54abcd2..8015581 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -11,11 +11,11 @@ uv run pytest tests/ # run tests (asyncio_mode=auto)uv run pytest tests/path/test.py::test -v # single testuv run ruff check cubepi/ tests/uv run ruff format --check cubepi/ tests/ # CI checks formatting, doesn't fix+uv run mypy cubepi```-CI runs pytest + ruff only. **There is no mypy step** — ignore the stale-`.mypy_cache/`. Tests run on Python 3.11–3.14 (3.14 is `continue-on-error`);-local default is 3.13 (`.python-version`).+CI runs pytest + ruff + mypy. Tests run on Python 3.11–3.14 (3.14 is+`continue-on-error`); local default is 3.13 (`.python-version`).## Architecture@@ -28,7 +28,7 @@ execution engine), `middleware/`, `checkpointer/` (memory / sqlite / postgres),## Conventions & gotchas-- **Lean deps**: core deps are anthropic, openai, pydantic, pyyaml only.+- **Lean deps**: core deps are anthropic, openai, pydantic only.Everything else (sqlite, postgres, mcp, tracing, trace-cli) is an optionalextra in `pyproject.toml`. Don't add a hard dependency without strong reason.- **`cubepi.tracing` is lazily importable** (PEP 562 `__getattr__`): schemadiff --git a/CHANGELOG.md b/CHANGELOG.mdindex b921e94..cae2975 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+## [0.7.0] - 2026-06-05+### Added- **`Provider.generate(...)` one-shot helper** — providers now expose a@@ -21,6 +23,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0`subagent` tool that runs an ephemeral child `Agent`, supports shared toolsand middleware inheritance, captures child events, and exposes host callbacksfor application-specific event streaming.+- **Typed provider error taxonomy** — built-in providers wrap SDK failures in+ `ProviderError` subclasses such as `ContextLengthExceeded`, `RateLimited`,+ `ProviderAuthFailed`, `ProviderUnavailable`, and `ProviderBadRequest`.+- **`Tracer.oneshot(...)`** — trace a single background LLM call without a full+ `Agent` loop; the trace CLI can filter these runs via+ `--meta oneshot_operation=...`.+- **Checkpointed HITL run IDs** — checkpointers now persist the owning+ `run_id` atomically with pending HITL requests so hosts can resume the+ correct detached run after a restart.- **`on_run_end` middleware hook** — fires exactly once after all turns and toolcalls complete, before `AgentEndEvent`. Return a `list[Message]` to injectadditional messages and run one extra model turn (e.g. a memory-reflection@@ -35,6 +46,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Changed+- **Breaking:** agent construction now takes a bound model from+ `provider.model(...)`. Replace+ `Agent(provider=provider, model=Model(...))` with+ `Agent(model=provider.model("model-id", ...))`. `provider_id` now lives on+ provider constructors and is copied into model metadata used for tracing,+ response metadata, and error messages. `Tracer.oneshot(...)`,+ `CompactionMiddleware`, and `SubagentMiddleware` use the same bound-model+ shape.- **Breaking:** pre-model middleware hooks now receive `AgentContext` directly.Update custom middleware and explicit hook callables from the old signatures:- `transform_context(messages, *, signal=None)`@@ -51,6 +70,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- **Breaking:** custom providers must implement `Provider.generate(...)` orinherit from `BaseProvider`, which supplies `generate()` by consuming`stream()`.+- **Breaking:** Postgres and MySQL checkpointer schemas are now version 3. Host+ applications must add the nullable `run_id` column to `cubepi_threads` and+ call `write_schema_version_op()` in their Alembic migration before using this+ release.+- CI now runs `mypy cubepi` in addition to pytest and ruff.++### Fixed++- Tool argument `ValidationError`s are formatted as model-readable tool+ results, including literal and extra-field errors.+- Provider stream-level SDK exceptions are classified into typed cubepi errors+ instead of leaking raw vendor exception types.+- `Tracer.oneshot()` now closes chat spans on failure/cancellation, awaits+ stream completion and flushes, forwards abort signals, and surfaces silent+ producer failures.++### Removed++- Removed unused `PyYAML` from the default dependency set; the core dependency+ set is back to `anthropic`, `openai`, and `pydantic`.## [0.6.0] - 2026-05-31@@ -138,7 +177,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- **[0.2.0]** - 2026-05-10 — see the [release notes](https://github.com/cubeplexai/cubepi/releases/tag/v0.2.0).- **[0.1.0]** - 2026-05-09 — initial release. See the [release notes](https://github.com/cubeplexai/cubepi/releases/tag/v0.1.0).-[Unreleased]: https://github.com/cubeplexai/cubepi/compare/v0.6.0...HEAD+[Unreleased]: https://github.com/cubeplexai/cubepi/compare/v0.7.0...HEAD+[0.7.0]: https://github.com/cubeplexai/cubepi/compare/v0.6.0...v0.7.0[0.6.0]: https://github.com/cubeplexai/cubepi/compare/v0.5.0...v0.6.0[0.5.0]: https://github.com/cubeplexai/cubepi/compare/v0.4.0...v0.5.0[0.4.0]: https://github.com/cubeplexai/cubepi/compare/v0.3.0...v0.4.0diff --git a/README.md b/README.mdindex d9b13f9..1f4b57d 100644--- a/README.md+++ b/README.md@@ -23,7 +23,7 @@ CubePi is a Pythonic, async-native agent framework designed for high performance| **Dependencies** | Pulls in langchain-core, langgraph-sdk, and transitive deps | 3 core deps: `pydantic`, `anthropic`, `openai` || **Tool execution** | Tools are graph nodes with manual wiring | Declare tools as functions, framework handles routing and parallel execution || **Multi-provider** | Via langchain chat model adapters | Native `Provider` protocol — Anthropic, OpenAI built in, add your own with one class |-| **Middleware** | Graph-level middleware on node entry/exit | Agent-level middleware with 7 typed hooks and declarative composition rules |+| **Middleware** | Graph-level middleware on node entry/exit | Agent-level middleware with 8 typed hooks and declarative composition rules || **Observability** | LangSmith / Langfuse integration, full trace visualization | Native OpenTelemetry — `Tracer`, `Meter`, GenAI semconv, OTLP / JSONL exporters built in |## Install@@ -58,7 +58,7 @@ from cubepi.agent.types import AgentToolResultfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.base import TextContent-provider = AnthropicProvider(api_key="sk-...")+provider = AnthropicProvider(provider_id="anthropic", api_key="sk-...")class GetWeatherParams(BaseModel):city: str@@ -69,8 +69,7 @@ async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on)agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),tools=[AgentTool(name="get_weather",@@ -106,11 +105,11 @@ from cubepi.providers.openai import OpenAIProviderfrom cubepi.providers import FauxProvider# Real providers-anthropic = AnthropicProvider(api_key="...")-openai = OpenAIProvider(api_key="...")+anthropic = AnthropicProvider(provider_id="anthropic", api_key="...")+openai = OpenAIProvider(provider_id="openai", api_key="...")# Test provider — no API calls, fully deterministic-faux = FauxProvider()+faux = FauxProvider(provider_id="faux")faux.set_responses(["Hello!", "How can I help?"])```@@ -145,16 +144,17 @@ Composable hooks that modify behavior without touching the core loop:```pythonfrom cubepi import Middleware, compose_middleware+from cubepi.agent.types import BeforeToolCallResultclass LoggingMiddleware(Middleware):- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):print(f"Context has {len(messages)} messages")return messagesclass SafetyMiddleware(Middleware):async def before_tool_call(self, ctx, *, signal=None):if ctx.tool_call.name == "dangerous_tool":- return BeforeToolCallResult(block=True, content="Blocked by policy")+ return BeforeToolCallResult(block=True, reason="Blocked by policy")return Nonehooks = compose_middleware([LoggingMiddleware(), SafetyMiddleware()])@@ -214,7 +214,7 @@ Ship your agent tests without API keys:```pythonfrom cubepi.providers import FauxProvider, faux_text, faux_tool_call, faux_assistant_message-provider = FauxProvider()+provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message([faux_tool_call("search", {"query": "python"}),@@ -222,7 +222,7 @@ provider.set_responses([faux_assistant_message("Here are the results..."),])-agent = Agent(provider=provider, model=Model(id="test", provider="faux"), tools=[search_tool])+agent = Agent(model=provider.model("test"), tools=[search_tool])agent.subscribe(lambda event, signal=None: None) # subscribe before prompt to receive eventsawait agent.prompt("Search for python")# Streams realistic deltas — content_block_start, text_delta, etc.diff --git a/cubepi/__init__.py b/cubepi/__init__.pyindex dffaddc..0cc3622 100644--- a/cubepi/__init__.py+++ b/cubepi/__init__.py@@ -20,6 +20,7 @@ from cubepi.middleware import Middleware, compose_middlewarefrom cubepi.providers import (AssistantMessage,BaseProvider,+ BoundModel,Message,MessageStream,Model,@@ -58,6 +59,7 @@ __all__ = ["AgentToolResult","AssistantMessage","BaseProvider",+ "BoundModel","CapabilityDescriptor","ContextLengthExceeded","JsonObject",diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.pyindex 497dc26..52442f4 100644--- a/cubepi/agent/agent.py+++ b/cubepi/agent/agent.py@@ -26,6 +26,7 @@ from cubepi.agent.types import ()from cubepi.providers.base import (AssistantMessage,+ BoundModel,Message,Model,OnPayloadCallback,@@ -91,7 +92,7 @@ class _MessageQueue:class AgentState:system_prompt: str = ""model: Model = field(- default_factory=lambda: Model(id="unknown", provider="unknown")+ default_factory=lambda: Model(id="unknown", provider_id="unknown"))thinking: ThinkingLevel = "off"is_streaming: bool = False@@ -130,8 +131,7 @@ class Agent(Generic[TMessage]):def __init__(self,*,- provider: Provider,- model: Model,+ model: BoundModel,system_prompt: str = "",tools: list[AgentTool] | None = None,thinking: ThinkingLevel = "off",@@ -153,10 +153,10 @@ class Agent(Generic[TMessage]):middleware: list[Middleware] | None = None,channel: HitlChannel | None = None,) -> None:- self._provider = provider+ self._provider: Provider = model.providerself._state = AgentState(system_prompt=system_prompt,- model=model,+ model=model.spec,thinking=thinking,)if tools:diff --git a/cubepi/errors.py b/cubepi/errors.pyindex 1143402..4d3a6f3 100644--- a/cubepi/errors.py+++ b/cubepi/errors.py@@ -202,7 +202,7 @@ def classify_and_raise(msg = str(exc) or getattr(exc, "message", "")status = _status_of(exc)- provider = model.provider+ provider = model.provider_idmodel_id = model.idtokens_in = _estimate_input_tokens(messages)diff --git a/cubepi/middleware/compaction/__init__.py b/cubepi/middleware/compaction/__init__.pyindex 3246fd9..ac9bdb4 100644--- a/cubepi/middleware/compaction/__init__.py+++ b/cubepi/middleware/compaction/__init__.py@@ -12,7 +12,7 @@ from cubepi.middleware.compaction.boundary import safe_boundaryfrom cubepi.middleware.compaction.state import CompactionState, message_refsfrom cubepi.middleware.compaction.summarizer import summarizefrom cubepi.middleware.compaction.tokens import approx_tokens-from cubepi.providers.base import Message, Model, Provider, TextContent, UserMessage+from cubepi.providers.base import BoundModel, Message, TextContent, UserMessageSUMMARY_PREFIX = "[Conversation summary so far]\n"logger = logging.getLogger(__name__)@@ -68,15 +68,14 @@ class CompactionMiddleware(Middleware):def __init__(self,*,- summary_provider: Provider,- summary_model: Model,+ summary_model: BoundModel,max_tokens_before_compact: int,keep_recent_messages: int = 8,max_summary_tokens: int = 1024,min_compact_messages: int = 4,) -> None:- self._summary_provider = summary_provider- self._summary_model = summary_model+ self._summary_provider = summary_model.provider+ self._summary_model = summary_model.specself._max_tokens_before = max_tokens_before_compactself._keep_recent = keep_recent_messagesself._max_summary_tokens = max_summary_tokensdiff --git a/cubepi/middleware/subagents.py b/cubepi/middleware/subagents.pyindex 8ffc975..4519aac 100644--- a/cubepi/middleware/subagents.py+++ b/cubepi/middleware/subagents.py@@ -13,7 +13,7 @@ from pydantic import BaseModelfrom cubepi.agent.agent import Agentfrom cubepi.agent.types import AgentEvent, AgentTool, AgentToolResultfrom cubepi.middleware.base import Middleware-from cubepi.providers.base import AssistantMessage, Model, Provider, TextContent+from cubepi.providers.base import AssistantMessage, BoundModel, TextContentfrom cubepi.types import StructuredValuelogger = logging.getLogger(__name__)@@ -33,8 +33,7 @@ class SubagentSpec:name: strdescription: strsystem_prompt: str- provider: Provider | None = None- model: Model | None = None+ model: BoundModel | None = Nonetools: Sequence[AgentTool[BaseModel]] = field(default_factory=tuple)middleware: Sequence[Middleware] = field(default_factory=tuple)@@ -62,8 +61,7 @@ class SubagentMiddleware(Middleware):self,*,subagents: dict[str, SubagentSpec],- default_provider: Provider,- default_model: Model,+ default_model: BoundModel,shared_tools: Sequence[AgentTool[BaseModel]] = (),inherited_middleware: Sequence[Middleware] = (),excluded_tool_names: set[str] | None = None,@@ -83,7 +81,6 @@ class SubagentMiddleware(Middleware):excluded = excluded_tool_names or {"subagent"}self._subagents = dict(subagents)- self._default_provider = default_providerself._default_model = default_modelself._shared_tools = tuple(tool for tool in shared_tools if tool.name not in excluded@@ -153,12 +150,10 @@ class SubagentMiddleware(Middleware):request.subagent_type,self._subagents["general-purpose"],)- provider = spec.provider or self._default_providermodel = spec.model or self._default_modeltools = [*self._shared_tools, *spec.tools]middleware = [*self._inherited_middleware, *spec.middleware]child: Agent[BaseModel] = Agent(- provider=provider,model=model,system_prompt=spec.system_prompt,tools=tools,diff --git a/cubepi/providers/__init__.py b/cubepi/providers/__init__.pyindex 78b75ce..7fb10cb 100644--- a/cubepi/providers/__init__.py+++ b/cubepi/providers/__init__.py@@ -1,6 +1,7 @@from cubepi.providers.base import (AssistantMessage,BaseProvider,+ BoundModel,Content,ImageContent,Message,@@ -64,6 +65,7 @@ def get_openai_responses_provider():__all__ = ["AssistantMessage","BaseProvider",+ "BoundModel","Content","FauxProvider","ImageContent",diff --git a/cubepi/providers/anthropic.py b/cubepi/providers/anthropic.pyindex 5eaea0c..2939329 100644--- a/cubepi/providers/anthropic.py+++ b/cubepi/providers/anthropic.py@@ -113,8 +113,9 @@ class AnthropicProvider(BaseProvider):cache_policy: CacheMarkerPolicy | None = None,capability: CapabilityDescriptor | None = None,model_capability_overrides: dict[str, CapabilityDescriptor] | None = None,+ provider_id: str = "",) -> None:- super().__init__()+ super().__init__(provider_id=provider_id)import anthropickwargs: dict[str, Any] = {"api_key": api_key}@@ -268,7 +269,7 @@ class AnthropicProvider(BaseProvider):content=[],usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(@@ -325,7 +326,7 @@ class AnthropicProvider(BaseProvider):error_message=err_text,usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(@@ -778,7 +779,7 @@ class AnthropicProvider(BaseProvider):or 0,),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,response_id=getattr(response, "id", None),)diff --git a/cubepi/providers/base.py b/cubepi/providers/base.pyindex 0d60a92..047afa3 100644--- a/cubepi/providers/base.py+++ b/cubepi/providers/base.py@@ -80,7 +80,7 @@ class Usage(BaseModel):class Model(BaseModel):id: str- provider: str+ provider_id: str = ""api: str = ""reasoning: bool = Falsecontext_window: int = 200_000@@ -90,6 +90,12 @@ class Model(BaseModel):thinking_level_map: dict[str, str | None] | None = None+@dataclass(frozen=True)+class BoundModel:+ provider: Provider+ spec: Model++class TextContent(BaseModel):type: Literal["text"] = "text"text: str = ""@@ -189,7 +195,7 @@ def format_provider_error(/ ``__context__``. Without provider/model/cause context, a logged orpersisted error tells you nothing about *which* model failed or *why*."""- target = f"{model.provider}/{model.id}"+ target = f"{model.provider_id}/{model.id}" if model.provider_id else model.idif base_url:target = f"{target} @ {base_url}"@@ -614,11 +620,39 @@ class BaseProvider:below."""- def __init__(self) -> None:+ def __init__(self, *, provider_id: str = "") -> None:+ self.provider_id = provider_idself._request_listeners: list[OnRequestCallback] = []self._chunk_listeners: list[OnChunkCallback] = []self._response_listeners: list[OnResponseBodyCallback] = []+ def model(+ self,+ id: str,+ *,+ api: str = "",+ reasoning: bool = False,+ context_window: int = 200_000,+ max_tokens: int = 8192,+ temperature: float = 0.7,+ cost: ModelCost | None = None,+ thinking_level_map: dict[str, str | None] | None = None,+ ) -> BoundModel:+ return BoundModel(+ provider=self,+ spec=Model(+ id=id,+ provider_id=self.provider_id,+ api=api,+ reasoning=reasoning,+ context_window=context_window,+ max_tokens=max_tokens,+ temperature=temperature,+ cost=cost,+ thinking_level_map=thinking_level_map,+ ),+ )+async def stream(self,model: Model,diff --git a/cubepi/providers/faux.py b/cubepi/providers/faux.pyindex bf28dd5..6de76a4 100644--- a/cubepi/providers/faux.py+++ b/cubepi/providers/faux.py@@ -160,8 +160,9 @@ class FauxProvider(BaseProvider):tokens_per_second: float | None = None,token_size_min: int = 3,token_size_max: int = 5,+ provider_id: str = "",) -> None:- super().__init__()+ super().__init__(provider_id=provider_id)self._responses: list[FauxResponseStep] = []self._tokens_per_second = tokens_per_secondself._min = max(1, min(token_size_min, token_size_max))@@ -346,7 +347,7 @@ class FauxProvider(BaseProvider):resolved = resolved.model_copy(update={"usage": cache_usage,- "provider_id": model.provider,+ "provider_id": model.provider_id,"model_id": model.id,})diff --git a/cubepi/providers/models.py b/cubepi/providers/models.pyindex 20b268f..16c9b41 100644--- a/cubepi/providers/models.py+++ b/cubepi/providers/models.py@@ -87,11 +87,11 @@ def clamp_thinking_level(model: Model, level: ThinkingLevel) -> ThinkingLevel:def models_are_equal(a: Model | None, b: Model | None) -> bool:"""Return ``True`` if *a* and *b* refer to the same model.- Comparison is by ``id`` and ``provider``. Returns ``False`` when either+ Comparison is by ``id`` and ``provider_id``. Returns ``False`` when eitherargument is ``None``."""if a is None and b is None:return Trueif a is None or b is None:return False- return a.id == b.id and a.provider == b.provider+ return a.id == b.id and a.provider_id == b.provider_iddiff --git a/cubepi/providers/openai.py b/cubepi/providers/openai.pyindex 0564ad4..55aeebb 100644--- a/cubepi/providers/openai.py+++ b/cubepi/providers/openai.py@@ -47,8 +47,9 @@ class OpenAIProvider(BaseProvider):extra_headers: dict[str, str] | None = None,capability: CapabilityDescriptor | None = None,model_capability_overrides: dict[str, CapabilityDescriptor] | None = None,+ provider_id: str = "",) -> None:- super().__init__()+ super().__init__(provider_id=provider_id)import openaikwargs: dict[str, Any] = {}@@ -185,7 +186,7 @@ class OpenAIProvider(BaseProvider):content=[],usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(@@ -527,7 +528,7 @@ class OpenAIProvider(BaseProvider):error_message=err_text,usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(diff --git a/cubepi/providers/openai_responses.py b/cubepi/providers/openai_responses.pyindex a447445..fa4aab5 100644--- a/cubepi/providers/openai_responses.py+++ b/cubepi/providers/openai_responses.py@@ -62,8 +62,9 @@ class OpenAIResponsesProvider(BaseProvider):base_url: str | None = None,capability: CapabilityDescriptor | None = None,model_capability_overrides: dict[str, CapabilityDescriptor] | None = None,+ provider_id: str = "",) -> None:- super().__init__()+ super().__init__(provider_id=provider_id)import openaikwargs: dict[str, Any] = {}@@ -205,7 +206,7 @@ class OpenAIResponsesProvider(BaseProvider):content=[],usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(@@ -597,7 +598,7 @@ class OpenAIResponsesProvider(BaseProvider):error_message=err_text,usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)await self._emit(diff --git a/cubepi/tracing/meter.py b/cubepi/tracing/meter.pyindex 6ba47f2..4ee3994 100644--- a/cubepi/tracing/meter.py+++ b/cubepi/tracing/meter.py@@ -284,7 +284,7 @@ class Meter:self, state: "_MeterState", payload: dict, model: "Model") -> None:del payload- provider_name = map_provider_name(model.provider)+ provider_name = map_provider_name(model.provider_id)state.chat_open_ns = time.time_ns()state.chat_first_chunk_ns = None# Include gen_ai.request.model so failed/cancelled chat metricsdiff --git a/cubepi/tracing/recorder.py b/cubepi/tracing/recorder.pyindex 6c44d1e..938fd99 100644--- a/cubepi/tracing/recorder.py+++ b/cubepi/tracing/recorder.py@@ -930,7 +930,7 @@ class Recorder:ctx = trace.set_span_in_context(run.turn_span)attrs: dict[str, Any] = {GEN_AI_OPERATION_NAME: OP_CHAT,- GEN_AI_PROVIDER_NAME: map_provider_name(model.provider),+ GEN_AI_PROVIDER_NAME: map_provider_name(model.provider_id),GEN_AI_REQUEST_MODEL: model.id,GEN_AI_REQUEST_STREAM: True,# Record run_id as a span attribute (for filtering); the JSONLdiff --git a/cubepi/tracing/tracer.py b/cubepi/tracing/tracer.pyindex b342f61..2bcc08f 100644--- a/cubepi/tracing/tracer.py+++ b/cubepi/tracing/tracer.py@@ -30,7 +30,7 @@ from cubepi.tracing.schema import SCHEMA_URL, SCOPE_NAMEif TYPE_CHECKING:from cubepi.agent.agent import Agent- from cubepi.providers.base import BaseProvider, Message, Model+ from cubepi.providers.base import BaseProvider, BoundModel, Message, Model, Providerclass _OneShotSession:@@ -43,7 +43,7 @@ class _OneShotSession:def __init__(self,- provider: "BaseProvider",+ provider: "Provider",model: "Model",run: Any,) -> None:@@ -419,8 +419,7 @@ class Tracer:async def oneshot(self,*,- provider: "BaseProvider",- model: "Model",+ model: "BoundModel",operation: str = "oneshot",metadata: "dict[str, str | int | float | bool] | None" = None,record_content: bool | None = None,@@ -448,7 +447,6 @@ class Tracer:Example::async with tracer.oneshot(- provider=provider,model=model,operation="consolidate_memory",metadata={"conversation_id": conv_id, "user_id": user_id},@@ -475,6 +473,8 @@ class Tracer:record_content if record_content is not None else self._record_content)run_id = str(uuid.uuid4())+ provider = model.provider+ model_spec = model.specroot_attrs: dict[str, Any] = {GEN_AI_OPERATION_NAME: OP_INVOKE_AGENT,@@ -570,7 +570,7 @@ class Tracer:# calls that belong to this oneshot session.token = _active_run.set(run)try:- yield _OneShotSession(provider=provider, model=model, run=run)+ yield _OneShotSession(provider=provider, model=model_spec, run=run)except BaseException as _exc:# Mark the root span as failed so `cubepi trace ls` (which# reads status off the root) lists this run as an errordiff --git a/dev/plans/2026-06-05-bound-provider-model-api.md b/dev/plans/2026-06-05-bound-provider-model-api.mdnew file mode 100644index 0000000..86f96d4--- /dev/null+++ b/dev/plans/2026-06-05-bound-provider-model-api.md@@ -0,0 +1,945 @@+# Bound Provider Model API Implementation Plan++> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.++**Goal:** Make CubePi's public agent construction API use one bound model object, so callers write `model = provider.model("...")` and `Agent(model=model, ...)` instead of passing both `provider` and `Model(provider=...)`.++**Architecture:** Keep the serializable model configuration as a pure Pydantic object, but rename its provider metadata to `provider_id`. Add a lightweight `BoundModel` dataclass that pairs a runtime `Provider` with that pure model spec. Providers own `provider_id` and expose `provider.model(...)` as the main constructor for bound models; Agent and middleware unwrap the bound model internally.++**Tech Stack:** Python 3.11+, Pydantic v2, dataclasses, pytest, ruff, mypy, Docusaurus docs.++---++## File Structure++- Modify `cubepi/providers/base.py`: add `BoundModel`, rename `Model.provider` to `Model.provider_id`, add `provider_id` and `model()` to `BaseProvider`, and update provider-error formatting.+- Modify `cubepi/providers/{anthropic.py,openai.py,openai_responses.py,faux.py}`: accept optional `provider_id` and pass it to `BaseProvider`.+- Modify `cubepi/agent/agent.py`: make `Agent.__init__` accept only a bound model object; store the runtime provider and pure model spec internally.+- Modify `cubepi/agent/loop.py`: no behavior change expected, but type imports should keep using the pure model spec and provider protocol.+- Modify `cubepi/middleware/compaction/__init__.py` and `cubepi/middleware/subagents.py`: replace `*_provider + *_model` constructor pairs with one bound model.+- Modify `cubepi/tracing/{meter.py,recorder.py}` and `cubepi/errors.py`: read `model.provider_id`.+- Modify provider implementations and image provider code that currently write `provider_id=model.provider`.+- Modify exports in `cubepi/providers/__init__.py` and `cubepi/__init__.py`: export `BoundModel`.+- Modify tests under `tests/agent`, `tests/providers`, `tests/middleware`, `tests/tracing`, `tests/hitl`, and `tests/mcp`: update construction call sites.+- Modify docs and examples in `README.md`, `AGENTS.md`, `skills/cubepi/SKILL.md`, `website/docs`, `website/i18n/zh-Hans/...`, and `website/versioned_docs/version-0.7`.++## API Shape++Target public usage:++```python+from cubepi import Agent+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(provider_id="anthropic")+model = provider.model(+ "claude-sonnet-4-5-20250929",+ reasoning=True,+ max_tokens=8192,+)++agent = Agent(+ model=model,+ system_prompt="You are helpful.",+)+```++Target Cubebox mapping:++```python+provider = factory.build_cubepi_provider(+ provider_config,+ provider_id=provider_slug,+)+model = provider.model(+ model_id,+ reasoning=reasoning_enabled,+ max_tokens=max_tokens,+ temperature=temperature,+)+agent = Agent(model=model, ...)+```++The underlying pure model spec remains serializable:++```python+Model(+ id="claude-sonnet-4-5-20250929",+ provider_id="anthropic",+ reasoning=True,+)+```++No backwards-compatibility shim is included in this plan. This is a breaking 0.7 API change.++---++### Task 1: Add BoundModel and Provider-Owned provider_id++**Files:**+- Modify: `cubepi/providers/base.py`+- Modify: `cubepi/providers/__init__.py`+- Modify: `cubepi/__init__.py`+- Test: `tests/providers/test_bound_model.py`++- [ ] **Step 1: Write the failing bound-model tests**++Create `tests/providers/test_bound_model.py`:++```python+from cubepi.providers.base import BaseProvider, BoundModel, Model+++def test_base_provider_model_binds_runtime_provider_and_model_spec() -> None:+ provider = BaseProvider(provider_id="catalog-anthropic")++ bound = provider.model(+ "claude-sonnet-4-5-20250929",+ api="anthropic-messages",+ reasoning=True,+ context_window=200_000,+ max_tokens=8192,+ temperature=0.2,+ )++ assert isinstance(bound, BoundModel)+ assert bound.provider is provider+ assert bound.spec == Model(+ id="claude-sonnet-4-5-20250929",+ provider_id="catalog-anthropic",+ api="anthropic-messages",+ reasoning=True,+ context_window=200_000,+ max_tokens=8192,+ temperature=0.2,+ )+++def test_base_provider_model_leaves_provider_id_empty_by_default() -> None:+ provider = BaseProvider()++ bound = provider.model("local-model")++ assert bound.provider is provider+ assert bound.spec.provider_id == ""+ assert bound.spec.id == "local-model"+```++- [ ] **Step 2: Run the new test and verify it fails**++Run:++```bash+uv run pytest tests/providers/test_bound_model.py -q+```++Expected: FAIL because `BoundModel`, `BaseProvider.provider_id`, and `BaseProvider.model()` do not exist yet.++- [ ] **Step 3: Implement the minimal base API**++In `cubepi/providers/base.py`, change `Model` and `BaseProvider` to this shape:++```python+@dataclass(frozen=True)+class BoundModel:+ provider: Provider+ spec: Model+++class Model(BaseModel):+ id: str+ provider_id: str = ""+ api: str = ""+ reasoning: bool = False+ context_window: int = 200_000+ max_tokens: int = 8192+ temperature: float = 0.7+ cost: ModelCost | None = None+ thinking_level_map: dict[str, str | None] | None = None+```++Update `BaseProvider.__init__` and add `model()`:++```python+class BaseProvider:+ def __init__(self, *, provider_id: str = "") -> None:+ self.provider_id = provider_id+ self._request_listeners: list[OnRequestCallback] = []+ self._chunk_listeners: list[OnChunkCallback] = []+ self._response_listeners: list[OnResponseBodyCallback] = []++ def model(+ self,+ id: str,+ *,+ api: str = "",+ reasoning: bool = False,+ context_window: int = 200_000,+ max_tokens: int = 8192,+ temperature: float = 0.7,+ cost: ModelCost | None = None,+ thinking_level_map: dict[str, str | None] | None = None,+ ) -> BoundModel:+ return BoundModel(+ provider=self,+ spec=Model(+ id=id,+ provider_id=self.provider_id,+ api=api,+ reasoning=reasoning,+ context_window=context_window,+ max_tokens=max_tokens,+ temperature=temperature,+ cost=cost,+ thinking_level_map=thinking_level_map,+ ),+ )+```++Keep `Provider` as the runtime protocol with `stream()` and `generate()`. Do not require custom protocol implementors to expose `model()`; users with custom providers can either inherit `BaseProvider` or construct `BoundModel(provider=custom_provider, spec=Model(...))`.++- [ ] **Step 4: Export the new type**++In `cubepi/providers/__init__.py` and `cubepi/__init__.py`, import and add `BoundModel` to `__all__`.++- [ ] **Step 5: Run the bound-model tests**++Run:++```bash+uv run pytest tests/providers/test_bound_model.py -q+```++Expected: PASS.++---++### Task 2: Move Built-In Providers onto BaseProvider(provider_id=...)++**Files:**+- Modify: `cubepi/providers/anthropic.py`+- Modify: `cubepi/providers/openai.py`+- Modify: `cubepi/providers/openai_responses.py`+- Modify: `cubepi/providers/faux.py`+- Test: `tests/providers/test_bound_model.py`++- [ ] **Step 1: Extend the failing test for concrete providers**++Append to `tests/providers/test_bound_model.py`:++```python+from cubepi.providers.faux import FauxProvider+++def test_concrete_provider_accepts_provider_id() -> None:+ provider = FauxProvider(provider_id="faux-catalog")++ bound = provider.model("faux-1")++ assert bound.provider is provider+ assert bound.spec.provider_id == "faux-catalog"+ assert bound.spec.id == "faux-1"+```++- [ ] **Step 2: Run the concrete-provider test and verify it fails**++Run:++```bash+uv run pytest tests/providers/test_bound_model.py::test_concrete_provider_accepts_provider_id -q+```++Expected: FAIL because `FauxProvider.__init__` does not accept `provider_id`.++- [ ] **Step 3: Add provider_id to concrete provider constructors**++Update each built-in text provider constructor to accept `provider_id: str = ""` and call `super().__init__(provider_id=provider_id)`.++Example for `cubepi/providers/openai.py`:++```python+class OpenAIProvider(BaseProvider):+ def __init__(+ self,+ *,+ api_key: str | None = None,+ base_url: str | None = None,+ extra_body: dict[str, Any] | None = None,+ extra_headers: dict[str, str] | None = None,+ capability: CapabilityDescriptor | None = None,+ model_capability_overrides: dict[str, CapabilityDescriptor] | None = None,+ provider_id: str = "",+ ) -> None:+ super().__init__(provider_id=provider_id)+```++Apply the same pattern to:++```python+AnthropicProvider(..., provider_id: str = "")+OpenAIResponsesProvider(..., provider_id: str = "")+FauxProvider(..., provider_id: str = "")+```++- [ ] **Step 4: Run provider constructor tests**++Run:++```bash+uv run pytest tests/providers/test_bound_model.py -q+```++Expected: PASS.++---++### Task 3: Rename Model.provider to Model.provider_id Internally++**Files:**+- Modify: `cubepi/providers/base.py`+- Modify: `cubepi/providers/{anthropic.py,openai.py,openai_responses.py,faux.py}`+- Modify: `cubepi/providers/images/{types.py,openai_images.py,faux.py,registry.py}`+- Modify: `cubepi/tracing/{meter.py,recorder.py}`+- Modify: `cubepi/errors.py`+- Modify: tests that assert provider metadata+- Test: `tests/providers/test_bound_model.py`+- Test: `tests/tracing/test_meter.py`+- Test: `tests/tracing/test_recorder.py`+- Test: `tests/test_errors.py`++- [ ] **Step 1: Add focused tests for provider_id metadata**++Append to `tests/providers/test_bound_model.py`:++```python+from cubepi.providers.base import AssistantMessage, TextContent+++def test_assistant_message_uses_model_provider_id_metadata() -> None:+ model = Model(id="faux-1", provider_id="faux-provider")++ message = AssistantMessage(+ content=[TextContent(text="ok")],+ provider_id=model.provider_id,+ model_id=model.id,+ )++ assert message.provider_id == "faux-provider"+ assert message.model_id == "faux-1"+```++- [ ] **Step 2: Run focused tests and verify old references still need cleanup**++Run:++```bash+uv run pytest tests/providers/test_bound_model.py -q+uv run ruff check cubepi/providers/base.py cubepi/providers cubepi/tracing cubepi/errors.py+```++Expected: the new test passes after Task 1, but ruff or pytest still finds runtime references to `model.provider` once `Model.provider` has been removed.++- [ ] **Step 3: Replace `model.provider` reads with `model.provider_id`**++Run a search:++```bash+rg "model\\.provider" cubepi tests website/docs README.md+```++Update code references in `cubepi/`:++```python+provider_id=model.provider_id+```++Update error formatting in `cubepi/providers/base.py`:++```python+target = f"{model.provider_id}/{model.id}" if model.provider_id else model.id+```++Update `cubepi/errors.py`:++```python+provider = model.provider_id+```++Do not update docs in this task; docs are handled after the code API settles.++- [ ] **Step 4: Update tests to instantiate pure model specs with provider_id**++Replace test construction like:++```python+Model(id="faux-1", provider="faux")+```++with:++```python+Model(id="faux-1", provider_id="faux")+```++Use `provider.model("...")` only in tests that construct an `Agent` or middleware requiring a bound runtime model.++- [ ] **Step 5: Run metadata and provider tests**++Run:++```bash+uv run pytest tests/providers tests/tracing tests/test_errors.py -q+```++Expected: PASS for provider metadata, tracing, and error tests.++---++### Task 4: Make Agent Accept Only a Bound Model++**Files:**+- Modify: `cubepi/agent/agent.py`+- Modify: `cubepi/agent/loop.py` only if type imports need cleanup+- Test: `tests/agent/test_agent.py`+- Test: `tests/agent/test_loop.py`+- Test: `tests/agent/test_e2e.py`++- [ ] **Step 1: Add a focused Agent API test**++Add to `tests/agent/test_agent.py`:++```python+from cubepi import Agent+from cubepi.providers.faux import FauxProvider+++def test_agent_unwraps_bound_model() -> None:+ provider = FauxProvider(provider_id="faux")+ model = provider.model("faux-1")++ agent: Agent = Agent(model=model)++ assert agent.state.model.id == "faux-1"+ assert agent.state.model.provider_id == "faux"+```++- [ ] **Step 2: Run the focused Agent API test and verify it fails**++Run:++```bash+uv run pytest tests/agent/test_agent.py::test_agent_unwraps_bound_model -q+```++Expected: FAIL because `Agent.__init__` still requires `provider=`.++- [ ] **Step 3: Change Agent.__init__ signature and internal unwrapping**++In `cubepi/agent/agent.py`, change imports:++```python+from cubepi.providers.base import BoundModel, Model, Provider, ...+```++Change the constructor signature from:++```python+def __init__(+ self,+ *,+ provider: Provider,+ model: Model,+ ...+) -> None:+```++to:++```python+def __init__(+ self,+ *,+ model: BoundModel,+ ...+) -> None:+```++At the top of the constructor, unwrap:++```python+self._provider: Provider = model.provider+self._state = AgentState(+ system_prompt=system_prompt,+ model=model.spec,+ thinking=thinking,+)+```++Keep `AgentState.model` typed as the pure `Model` spec.++- [ ] **Step 4: Update Agent construction tests**++Replace:++```python+provider = FauxProvider()+agent = Agent(provider=provider, model=Model(id="faux-1", provider_id="faux"))+```++with:++```python+provider = FauxProvider(provider_id="faux")+agent = Agent(model=provider.model("faux-1"))+```++When a test needs non-default spec fields:++```python+agent = Agent(+ model=provider.model("faux-reasoning", reasoning=True),+ thinking="medium",+)+```++- [ ] **Step 5: Run Agent tests**++Run:++```bash+uv run pytest tests/agent -q+```++Expected: PASS.++---++### Task 5: Update Compaction and Subagent Middleware to Use Bound Models++**Files:**+- Modify: `cubepi/middleware/compaction/__init__.py`+- Modify: `cubepi/middleware/compaction/summarizer.py` if type imports require cleanup+- Modify: `cubepi/middleware/subagents.py`+- Test: `tests/middleware/test_compaction.py`+- Test: `tests/middleware/test_subagents.py`+- Test: `tests/middleware/compaction/test_summarizer.py`++- [ ] **Step 1: Add/update middleware API tests**++Update `tests/middleware/test_compaction.py` setup to use:++```python+provider = FauxProvider(provider_id="faux")+middleware = CompactionMiddleware(+ summary_model=provider.model("summary-model"),+ max_tokens_before_compact=1,+)+```++Update `tests/middleware/test_subagents.py` setup to use:++```python+provider = FauxProvider(provider_id="faux")+middleware = SubagentMiddleware(+ subagents={},+ default_model=provider.model("faux-1"),+)+```++For per-subagent overrides, use:++```python+SubagentSpec(+ name="researcher",+ description="Research one topic",+ system_prompt="You research.",+ model=other_provider.model("faux-researcher"),+)+```++- [ ] **Step 2: Run middleware tests and verify they fail**++Run:++```bash+uv run pytest tests/middleware/test_compaction.py tests/middleware/test_subagents.py -q+```++Expected: FAIL because constructors still require separate provider arguments and `Agent(...)` calls still pass `provider=`.++- [ ] **Step 3: Update CompactionMiddleware**++Change constructor imports and signature:++```python+from cubepi.providers.base import BoundModel, Message, Model, TextContent, UserMessage+++class CompactionMiddleware(Middleware):+ def __init__(+ self,+ *,+ summary_model: BoundModel,+ max_tokens_before_compact: int,+ keep_recent_messages: int = 8,+ max_summary_tokens: int = 1024,+ min_compact_messages: int = 4,+ ) -> None:+ self._summary_provider = summary_model.provider+ self._summary_model = summary_model.spec+```++Keep the `summarize(provider=..., model=...)` helper unchanged because it is an internal low-level call.++- [ ] **Step 4: Update SubagentMiddleware**++Change `SubagentSpec`:++```python+@dataclass(frozen=True)+class SubagentSpec:+ name: str+ description: str+ system_prompt: str+ model: BoundModel | None = None+ tools: Sequence[AgentTool[BaseModel]] = field(default_factory=tuple)+ middleware: Sequence[Middleware] = field(default_factory=tuple)+```++Change `SubagentMiddleware.__init__`:++```python+def __init__(+ self,+ *,+ subagents: dict[str, SubagentSpec],+ default_model: BoundModel,+ shared_tools: Sequence[AgentTool[BaseModel]] = (),+ inherited_middleware: Sequence[Middleware] = (),+ ...+) -> None:+ self._default_model = default_model+```++Change `_run_subagent`:++```python+model = spec.model or self._default_model+child: Agent[BaseModel] = Agent(+ model=model,+ system_prompt=spec.system_prompt,+ tools=tools,+ middleware=middleware,+)+```++- [ ] **Step 5: Run middleware tests**++Run:++```bash+uv run pytest tests/middleware -q+```++Expected: PASS.++---++### Task 6: Update All Remaining Test Call Sites++**Files:**+- Modify: `tests/**/*.py`++- [ ] **Step 1: Search all old public API call sites**++Run:++```bash+rg "Agent\\([^\\n]*provider=|provider=.*model=|Model\\([^\\n]*provider=" tests cubepi+rg "default_provider|summary_provider|spec\\.provider|model\\.provider" tests cubepi+```++Expected: output lists remaining old API usage.++- [ ] **Step 2: Replace Agent constructions**++Use this mechanical pattern:++```python+provider = FauxProvider(provider_id="faux")+agent = Agent(model=provider.model("faux-1"), ...)+```++For tests that already have `provider` and `model_id` variables:++```python+agent = Agent(model=provider.model(model_id), ...)+```++For tests that need a pure model spec without an Agent:++```python+model = Model(id="faux-1", provider_id="faux")+```++- [ ] **Step 3: Replace compaction/subagent constructions**++Use:++```python+CompactionMiddleware(summary_model=provider.model("summary-model"), ...)+SubagentMiddleware(default_model=provider.model("faux-1"), ...)+```++- [ ] **Step 4: Run the full Python test suite**++Run:++```bash+uv run pytest tests/ -q+```++Expected: PASS, with the existing skipped tests unchanged.++---++### Task 7: Update Docs, Examples, and Release Notes++**Files:**+- Modify: `README.md`+- Modify: `AGENTS.md`+- Modify: `skills/cubepi/SKILL.md`+- Modify: `website/docs/**/*.md`+- Modify: `website/docs/**/*.mdx`+- Modify: `website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/**/*.md`+- Modify: `website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/**/*.mdx`+- Modify: `website/versioned_docs/version-0.7/**/*.mdx`+- Modify: `website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/**/*.md`+- Modify: `CHANGELOG.md`++- [ ] **Step 1: Search docs for old API**++Run:++```bash+rg "Agent\\(|Model\\(|provider=|default_provider|summary_provider|model\\.provider" README.md AGENTS.md skills/cubepi/SKILL.md website/docs website/i18n/zh-Hans/docusaurus-plugin-content-docs/current website/versioned_docs/version-0.7 website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7+```++Expected: output lists all examples and prose that need updates.++- [ ] **Step 2: Update basic examples**++Replace examples like:++```python+provider = AnthropicProvider()+agent = Agent(+ provider=provider,+ model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+)+```++with:++```python+provider = AnthropicProvider(provider_id="anthropic")+model = provider.model("claude-sonnet-4-5-20250929")+agent = Agent(model=model)+```++- [ ] **Step 3: Update compaction and subagent examples**++Replace:++```python+CompactionMiddleware(+ summary_provider=cheap_provider,+ summary_model=Model(id="claude-haiku-4-5", provider="anthropic"),+ max_tokens_before_compact=80_000,+)+```++with:++```python+CompactionMiddleware(+ summary_model=cheap_provider.model("claude-haiku-4-5"),+ max_tokens_before_compact=80_000,+)+```++Replace:++```python+SubagentMiddleware(+ subagents=subagents,+ default_provider=provider,+ default_model=model,+)+```++with:++```python+SubagentMiddleware(+ subagents=subagents,+ default_model=model,+)+```++- [ ] **Step 4: Update conceptual docs**++Add a short explanation in provider and quick-start docs:++```text+`provider.model(...)` returns a bound model. The provider holds credentials,+base URL, wire API configuration, and optional `provider_id` metadata. The+model spec stays serializable; CubePi stores only the spec in agent state and+uses the bound provider object for runtime calls.+```++Remove prose that says `Model(id=..., provider=...)` is required.++- [ ] **Step 5: Update CHANGELOG breaking-change entry**++Add to the 0.7.0 breaking changes:++```markdown+- Agent construction now takes a bound model from `provider.model(...)`.+ Replace `Agent(provider=provider, model=Model(...))` with+ `Agent(model=provider.model("model-id", ...))`. `provider_id` now lives on+ the provider constructor and is copied into the model metadata used for+ tracing and error messages.+```++- [ ] **Step 6: Re-run docs search**++Run:++```bash+rg "Agent\\([^\\n]*provider=|Model\\([^\\n]*provider=|default_provider|summary_provider|model\\.provider" README.md AGENTS.md skills/cubepi/SKILL.md website/docs website/i18n/zh-Hans/docusaurus-plugin-content-docs/current website/versioned_docs/version-0.7 website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7+```++Expected: no old public API examples remain. Internal explanations may mention the old API only in migration text.++---++### Task 8: Update API Reference Generation and Versioned Docs++**Files:**+- Modify generated files under `website/versioned_docs/version-0.7/api/*.mdx`+- Test: `website/scripts/tests/test_build_api_reference.py`++- [ ] **Step 1: Regenerate API reference**++Run:++```bash+cd website+pnpm build:api+```++Expected: API docs include `BoundModel`, `Model.provider_id`, and updated `Agent` signature.++- [ ] **Step 2: Inspect generated API snippets**++Run:++```bash+rg "BoundModel|provider_id|Agent\\(" website/docs/api website/versioned_docs/version-0.7/api+```++Expected: generated API docs expose `BoundModel`, show `provider_id`, and no longer show `Agent(..., provider=...)` as the constructor signature.++- [ ] **Step 3: Run API reference tests**++Run:++```bash+cd website+pnpm test -- VersionAwareDocLink+cd ..+uv run pytest website/scripts/tests/test_build_api_reference.py -q+```++Expected: PASS.++---++### Task 9: Static Checks and Full Validation++**Files:**+- No new files expected++- [ ] **Step 1: Run Python lint and formatting checks**++Run:++```bash+uv run ruff check cubepi/ tests/+uv run ruff format --check cubepi/ tests/+```++Expected: PASS.++- [ ] **Step 2: Run mypy**++Run:++```bash+uv run mypy cubepi+```++Expected: PASS. Existing mypy informational notes are acceptable.++- [ ] **Step 3: Run full Python tests**++Run:++```bash+uv run pytest tests/ -q+```++Expected: PASS, with the existing skipped tests unchanged.++- [ ] **Step 4: Run website checks**++Run:++```bash+cd website+pnpm typecheck+pnpm test+pnpm build+```++Expected: PASS. Existing Docusaurus v4 deprecation warning is acceptable.++- [ ] **Step 5: Build package**++Run:++```bash+uv build+```++Expected: PASS and produces `dist/cubepi-0.7.0.tar.gz` and `dist/cubepi-0.7.0-py3-none-any.whl`.++- [ ] **Step 6: Check release diff cleanliness**++Run:++```bash+git diff --check+git status --short+```++Expected: `git diff --check` emits no whitespace errors. `git status --short` shows only intentional release-prep/API-change files.++---++## Self-Review++- Spec coverage: The plan covers provider-owned `provider_id`, bound model construction, Agent single-argument model construction, compaction/subagent middleware, tracing/error metadata, docs, generated API reference, and full release validation.+- Placeholder scan: No implementation step uses unresolved placeholders; every code-changing task includes concrete signatures or replacement examples.+- Type consistency: `Model` is the pure Pydantic spec, `BoundModel` is the runtime pair, `provider.model(...)` returns `BoundModel`, and `AgentState.model` remains pure `Model`.diff --git a/examples/checkpointing_mysql.py b/examples/checkpointing_mysql.pyindex eb15b46..6aa56b4 100644--- a/examples/checkpointing_mysql.py+++ b/examples/checkpointing_mysql.py@@ -36,7 +36,6 @@ from cubepi.checkpointer.mysql.alembic_helpers import (write_schema_version_op,)from cubepi.checkpointer.mysql.checkpointer import _parse_dsn-from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProvider, faux_assistant_messageADMIN_DSN = os.environ.get(@@ -98,11 +97,10 @@ async def bootstrap_schema(dsn: str) -> None:def build_agent(checkpointer: MySQLCheckpointer) -> Agent:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Hi! I'll remember this.")])return Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),checkpointer=checkpointer,thread_id=THREAD_ID,)diff --git a/examples/checkpointing_postgres.py b/examples/checkpointing_postgres.pyindex d86de3e..955f835 100644--- a/examples/checkpointing_postgres.py+++ b/examples/checkpointing_postgres.py@@ -35,7 +35,6 @@ from cubepi.checkpointer.postgres.alembic_helpers import (create_message_partitions_op,write_schema_version_op,)-from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProvider, faux_assistant_messageADMIN_DSN = os.environ.get(@@ -92,11 +91,10 @@ async def bootstrap_schema(dsn: str) -> None:def build_agent(checkpointer: PostgresCheckpointer) -> Agent:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Hi! I'll remember this.")])return Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),checkpointer=checkpointer,thread_id=THREAD_ID,)diff --git a/pyproject.toml b/pyproject.tomlindex edc0c84..b751b37 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,6 @@[project]name = "cubepi"-version = "0.6.0"+version = "0.7.0"description = "Pythonic async-native agent framework"readme = "README.md"license = "MIT"@@ -19,7 +19,6 @@ dependencies = ["anthropic>=0.100.0","openai>=2.36.0","pydantic>=2.13.4",- "pyyaml>=6.0.3",][project.optional-dependencies]diff --git a/skills/cubepi/SKILL.md b/skills/cubepi/SKILL.mdindex 0de6d25..f9f8091 100644--- a/skills/cubepi/SKILL.md+++ b/skills/cubepi/SKILL.md@@ -36,9 +36,9 @@ from cubepi import Agent, AgentTool, Modelfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.openai import OpenAIProvider-provider = AnthropicProvider() # or OpenAIProvider()+provider = AnthropicProvider(provider_id="anthropic") # or OpenAIProvider(provider_id="openai")model = Model(id="claude-opus-4-5-20251001")-agent = Agent(provider=provider, model=model, system_prompt="You are helpful.")+agent = Agent(model=model, system_prompt="You are helpful.")await agent.prompt("Hello")await agent.wait_for_idle()@@ -85,10 +85,10 @@ to `Agent(middleware=[...])`:from cubepi import Middlewareclass MyMiddleware(Middleware):- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):return messages # filter / inject messages before each LLM call- async def transform_system_prompt(self, sp, *, signal=None):+ async def transform_system_prompt(self, sp, *, ctx, signal=None):return sp + "\nExtra." # append to system promptasync def before_tool_call(self, ctx, *, signal=None):diff --git a/tests/agent/test_agent.py b/tests/agent/test_agent.pyindex e2e5372..f470077 100644--- a/tests/agent/test_agent.py+++ b/tests/agent/test_agent.py@@ -16,13 +16,22 @@ from pydantic import BaseModeldef make_model() -> Model:- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")class TestAgentInit:+ def test_agent_unwraps_bound_model(self):+ provider = FauxProvider(provider_id="faux")+ bound_model = provider.model("faux-1")++ agent = Agent(model=bound_model)++ assert agent.state.model.id == "faux-1"+ assert agent.state.model.provider_id == "faux"+def test_default_state(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))assert agent.state.system_prompt == ""assert agent.state.thinking == "off"@@ -34,10 +43,9 @@ class TestAgentInit:assert agent.state.error_message is Nonedef test_custom_initial_state(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),system_prompt="You are a helpful assistant.",thinking="low",)@@ -63,8 +71,7 @@ class TestAgentInit:tools = [tool]agent = Agent(- provider=FauxProvider(),- model=make_model(),+ model=FauxProvider(provider_id="faux").model("faux-1"),middleware=[ToolMiddleware()],)@@ -73,8 +80,8 @@ class TestAgentInit:class TestAgentSubscribe:def test_subscribe_and_unsubscribe(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))count = 0@@ -88,9 +95,9 @@ class TestAgentSubscribe:unsub()async def test_events_emitted_on_prompt(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))events = []agent.subscribe(lambda e, s=None: events.append(e.type))@@ -106,9 +113,9 @@ class TestAgentSubscribe:async def bad_stream(*args, **kwargs):raise RuntimeError("provider exploded")- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.stream = bad_stream- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))events = []agent.subscribe(lambda e, s=None: events.append(e.type))@@ -132,9 +139,9 @@ class TestAgentSubscribe:assert agent.state.error_message == "provider exploded"async def test_await_async_subscribers_before_prompt_resolves(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))barrier = asyncio.Event()listener_finished = False@@ -171,8 +178,8 @@ class TestAgentSubscribe:class TestAgentState:def test_tools_are_copied(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))tools = [AgentTool(@@ -188,8 +195,8 @@ class TestAgentState:assert agent.state.tools is not toolsdef test_messages_are_copied(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))messages = [UserMessage(content=[TextContent(text="hi")])]agent.state.messages = messages@@ -198,16 +205,16 @@ class TestAgentState:class TestAgentQueues:def test_steer_queues_message(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))msg = UserMessage(content=[TextContent(text="steering")])agent.steer(msg)assert msg not in agent.state.messagesdef test_follow_up_queues_message(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))msg = UserMessage(content=[TextContent(text="follow-up")])agent.follow_up(msg)@@ -216,15 +223,15 @@ class TestAgentQueues:class TestAgentAbort:def test_abort_without_active_run_does_not_throw(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))agent.abort()class TestAgentPromptGuards:async def test_raises_when_prompt_called_while_streaming(self):barrier = asyncio.Event()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")async def slow_stream(*args, **kwargs):from cubepi.providers.base import MessageStream, StreamEvent@@ -241,7 +248,7 @@ class TestAgentPromptGuards:return msprovider.stream = slow_stream- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))task = asyncio.create_task(agent.prompt("first"))await asyncio.sleep(0.02)@@ -258,7 +265,7 @@ class TestAgentPromptGuards:async def test_raises_when_resume_called_while_streaming(self):barrier = asyncio.Event()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")async def slow_stream(*args, **kwargs):from cubepi.providers.base import MessageStream, StreamEvent@@ -275,7 +282,7 @@ class TestAgentPromptGuards:return msprovider.stream = slow_stream- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))task = asyncio.create_task(agent.prompt("first"))await asyncio.sleep(0.02)@@ -292,14 +299,14 @@ class TestAgentPromptGuards:class TestAgentResume:async def test_resume_processes_follow_up_messages(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Initial response"),faux_assistant_message("Processed"),])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("Initial")agent.follow_up(UserMessage(content=[TextContent(text="follow-up")]))@@ -318,14 +325,14 @@ class TestAgentResume:async def test_resume_drains_steering_queue_before_follow_up(self):"""resume() should drain the steering queue first when the lastmessage is from the assistant."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Initial"),faux_assistant_message("Steered"),])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("hello")agent.steer(UserMessage(content=[TextContent(text="steer-msg")]))@@ -344,9 +351,9 @@ class TestAgentResume:async def test_resume_raises_on_assistant_last_with_empty_queues(self):"""resume() raises RuntimeError when the last message is from theassistant and both steering and follow-up queues are empty."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("done")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("hello")@@ -390,8 +397,8 @@ class TestMessageQueueAllMode:class TestAgentStatePendingToolCalls:def test_setter_makes_a_copy(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))original = {"call-1", "call-2"}agent.state.pending_tool_calls = original@@ -404,9 +411,9 @@ class TestAgentStatePendingToolCalls:class TestAgentReset:async def test_reset_clears_state_after_prompt(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("response")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("hello")assert len(agent.state.messages) > 0@@ -420,8 +427,8 @@ class TestAgentReset:assert agent.state.error_message is Noneasync def test_reset_clears_queues(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))agent.steer(UserMessage(content=[TextContent(text="steer")]))agent.follow_up(UserMessage(content=[TextContent(text="follow")]))@@ -436,7 +443,7 @@ class TestAgentReset:class TestAgentAbortSignal:async def test_abort_sets_signal_during_active_run(self):barrier = asyncio.Event()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")async def slow_stream(*args, **kwargs):from cubepi.providers.base import MessageStream, StreamEvent@@ -453,7 +460,7 @@ class TestAgentAbortSignal:return msprovider.stream = slow_stream- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))task = asyncio.create_task(agent.prompt("hello"))await asyncio.sleep(0.02)@@ -470,15 +477,15 @@ class TestAgentAbortSignal:class TestAgentWaitForIdle:async def test_returns_immediately_when_no_active_run(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))# No active run, _active_done is None — should return immediatelyawait agent.wait_for_idle()async def test_waits_until_prompt_completes(self):barrier = asyncio.Event()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")async def slow_stream(*args, **kwargs):from cubepi.providers.base import MessageStream, StreamEvent@@ -495,7 +502,7 @@ class TestAgentWaitForIdle:return msprovider.stream = slow_stream- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))prompt_task = asyncio.create_task(agent.prompt("hello"))await asyncio.sleep(0.02)@@ -519,9 +526,9 @@ class TestAgentWaitForIdle:class TestAgentPromptInputTypes:async def test_prompt_with_message_object(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("response")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))msg = UserMessage(content=[TextContent(text="direct message")])await agent.prompt(msg)@@ -538,9 +545,9 @@ class TestAgentPromptInputTypes:assert isinstance(agent.state.messages[-1], AssistantMessage)async def test_prompt_with_list_of_messages(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("response")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))msgs = [UserMessage(content=[TextContent(text="first")]),@@ -589,7 +596,7 @@ def test_message_queue_remove_ignores_messages_without_steer_id():@pytest.mark.asyncioasync def test_agent_cancel_steer_removes_queued_steer():- agent = Agent(provider=FauxProvider(), model=make_model())+ agent = Agent(model=FauxProvider(provider_id="faux").model("faux-1"))agent.steer(_steer_msg("hi", "s1"))assert agent.cancel_steer("s1") is Trueassert agent.cancel_steer("s1") is Falsediff --git a/tests/agent/test_checkpointer_integration.py b/tests/agent/test_checkpointer_integration.pyindex a41e552..9a110d7 100644--- a/tests/agent/test_checkpointer_integration.py+++ b/tests/agent/test_checkpointer_integration.py@@ -22,17 +22,16 @@ from cubepi.providers.faux import (def make_model() -> Model:- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")class TestCheckpointerIntegration:async def test_messages_persisted_on_message_end(self):checkpointer = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Hello!")])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),checkpointer=checkpointer,thread_id="thread-1",)@@ -43,11 +42,10 @@ class TestCheckpointerIntegration:async def test_history_restored_on_prompt(self):checkpointer = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("First reply")])agent1 = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),checkpointer=checkpointer,thread_id="thread-1",)@@ -55,8 +53,7 @@ class TestCheckpointerIntegration:provider.set_responses([faux_assistant_message("Second reply")])agent2 = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),checkpointer=checkpointer,thread_id="thread-1",)@@ -64,9 +61,9 @@ class TestCheckpointerIntegration:assert len(agent2.state.messages) == 4 # 2 from first + 2 from secondasync def test_no_checkpointer_works_as_before(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Hi")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("Hello")assert len(agent.state.messages) == 2@@ -88,7 +85,7 @@ class TestCheckpointerIntegration:)checkpointer = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")# First response: assistant calls the echo tool# Second response: assistant gives a final text answerprovider.set_responses(@@ -101,8 +98,7 @@ class TestCheckpointerIntegration:])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[echo_tool],checkpointer=checkpointer,thread_id="thread-tool",@@ -147,7 +143,7 @@ class TestCheckpointerIntegration:)checkpointer = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")# --- First session: tool-use conversation ---provider.set_responses(@@ -160,8 +156,7 @@ class TestCheckpointerIntegration:])agent1 = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[echo_tool],checkpointer=checkpointer,thread_id="thread-tool-restore",@@ -173,8 +168,7 @@ class TestCheckpointerIntegration:# --- Second session: same thread, new Agent ---provider.set_responses([faux_assistant_message("Sure, continuing.")])agent2 = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[echo_tool],checkpointer=checkpointer,thread_id="thread-tool-restore",@@ -214,7 +208,7 @@ class TestCheckpointerIntegration:)checkpointer = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message(@@ -224,8 +218,7 @@ class TestCheckpointerIntegration:])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[block_tool],checkpointer=checkpointer,thread_id="thread-cancel",@@ -282,7 +275,7 @@ class TestCheckpointerIntegration:],)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message(@@ -292,8 +285,7 @@ class TestCheckpointerIntegration:])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[block_tool],checkpointer=checkpointer,thread_id="thread-reuse",@@ -321,7 +313,7 @@ class TestCheckpointerIntegration:async def test_cancel_backfill_only_for_unanswered_calls(self):"""Backfill skips already-answered tool_calls and only fills orphans —e.g. one of two parallel calls finished before the cancel landed."""- agent = Agent(provider=FauxProvider(), model=make_model())+ agent = Agent(model=FauxProvider(provider_id="faux").model("faux-1"))agent._state._messages = [UserMessage(content=[TextContent(text="go")]),AssistantMessage(@@ -348,7 +340,7 @@ class TestCheckpointerIntegration:async def test_cancel_backfill_noop_without_orphans(self):"""No assistant message, or no unanswered tool_calls — nothing added."""- agent = Agent(provider=FauxProvider(), model=make_model())+ agent = Agent(model=FauxProvider(provider_id="faux").model("faux-1"))# No assistant at all.agent._state._messages = [UserMessage(content=[TextContent(text="hi")])]await agent._complete_cancelled_tool_calls()diff --git a/tests/agent/test_context_extra.py b/tests/agent/test_context_extra.pyindex 79f13b7..bbd5c4c 100644--- a/tests/agent/test_context_extra.py+++ b/tests/agent/test_context_extra.py@@ -55,7 +55,6 @@ async def test_agent_hydrates_ctx_extra_from_checkpointer() -> None:through the turn and written back via save_extra."""from cubepi.agent.agent import Agentfrom cubepi.checkpointer import SQLiteCheckpointer- from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagewith tempfile.TemporaryDirectory() as d:@@ -63,11 +62,10 @@ async def test_agent_hydrates_ctx_extra_from_checkpointer() -> None:async with SQLiteCheckpointer(str(path)) as cp:await cp.save_extra("t-hyd", {"seeded": True})- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),checkpointer=cp,thread_id="t-hyd",)@@ -86,18 +84,16 @@ async def test_agent_persists_ctx_extra_mutation_after_turn() -> None:save_extra after the turn completes."""from cubepi.agent.agent import Agentfrom cubepi.checkpointer import SQLiteCheckpointer- from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagewith tempfile.TemporaryDirectory() as d:path = Path(d) / "persist.db"async with SQLiteCheckpointer(str(path)) as cp:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),checkpointer=cp,thread_id="t-pst",)diff --git a/tests/agent/test_e2e.py b/tests/agent/test_e2e.pyindex 51eb823..da3d5db 100644--- a/tests/agent/test_e2e.py+++ b/tests/agent/test_e2e.py@@ -22,7 +22,7 @@ from cubepi.providers.faux import (def make_model() -> Model:- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")class CalculateParams(BaseModel):@@ -49,11 +49,10 @@ def make_calculate_tool() -> AgentTool:class TestE2EBasic:async def test_basic_text_prompt(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("4")])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),system_prompt="You are a helpful assistant.",)@@ -66,7 +65,7 @@ class TestE2EBasic:assert "4" in agent.state.messages[1].content[0].textasync def test_tool_execution_with_pending_tracking(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message(@@ -82,8 +81,7 @@ class TestE2EBasic:])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),system_prompt="Always use the calculator tool for math.",tools=[make_calculate_tool()],)@@ -118,7 +116,7 @@ class TestE2EBasic:),])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))prompt_task = asyncio.create_task(agent.prompt("Count"))await asyncio.sleep(0.03)@@ -133,7 +131,7 @@ class TestE2EBasic:async def test_lifecycle_events_during_streaming(self):provider = FauxProvider(token_size_min=1, token_size_max=1)provider.set_responses([faux_assistant_message("1 2 3 4 5")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))events = []agent.subscribe(lambda e, s=None: events.append(e.type))@@ -147,7 +145,7 @@ class TestE2EBasic:assert "agent_end" in eventsasync def test_context_across_multiple_turns(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("Nice to meet you, Alice."),@@ -166,7 +164,7 @@ class TestE2EBasic:),])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))await agent.prompt("My name is Alice.")assert len(agent.state.messages) == 2@@ -176,15 +174,14 @@ class TestE2EBasic:assert "alice" in agent.state.messages[3].content[0].text.lower()async def test_thinking_content_preserved(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message([faux_thinking("step by step"), faux_text("4")]),])agent = Agent(- provider=provider,- model=Model(id="faux-reasoning", provider="faux", reasoning=True),+ model=provider.model("faux-reasoning", reasoning=True),thinking="low",)@@ -199,8 +196,8 @@ class TestE2EBasic:class TestE2EResume:async def test_raises_when_no_messages(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=make_model())+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model("faux-1"))try:await agent.resume()@@ -209,9 +206,9 @@ class TestE2EResume:assert "no messages" in str(e).lower()async def test_continue_from_user_message(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("HELLO WORLD")])- agent = Agent(provider=provider, model=make_model())+ agent = Agent(model=provider.model("faux-1"))agent.state.messages = [UserMessage(content=[TextContent(text="Say HELLO WORLD")]),@@ -224,11 +221,10 @@ class TestE2EResume:assert agent.state.messages[1].role == "assistant"async def test_continue_from_tool_result(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("The answer is 8.")])agent = Agent(- provider=provider,- model=make_model(),+ model=provider.model("faux-1"),tools=[make_calculate_tool()],)diff --git a/tests/agent/test_loop.py b/tests/agent/test_loop.pyindex 31883e0..7fb790e 100644--- a/tests/agent/test_loop.py+++ b/tests/agent/test_loop.py@@ -27,7 +27,7 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_message, faux_toodef make_model() -> Model:- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")def make_user_message(text: str) -> UserMessage:diff --git a/tests/hitl/test_agent_abort_pending.py b/tests/hitl/test_agent_abort_pending.pyindex 091c19e..3a02b66 100644--- a/tests/hitl/test_agent_abort_pending.py+++ b/tests/hitl/test_agent_abort_pending.py@@ -11,7 +11,7 @@ from cubepi.checkpointer.memory import MemoryCheckpointerfrom cubepi.hitl import AskUserfrom cubepi.hitl.channel import CheckpointedChannelfrom cubepi.hitl.middleware import ApprovalPolicyMiddleware-from cubepi.providers.base import Model, TextContent+from cubepi.providers.base import TextContentfrom cubepi.providers.faux import (FauxProvider,faux_assistant_message,@@ -40,7 +40,7 @@ def _bash_tool() -> AgentTool:async def test_abort_pending_closes_conversation():cp = MemoryCheckpointer()ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message(@@ -50,8 +50,7 @@ async def test_abort_pending_closes_conversation():])agent = Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),tools=[_bash_tool()],middleware=[ApprovalPolicyMiddleware(ch, policy=lambda c: AskUser())],channel=ch,diff --git a/tests/hitl/test_agent_channel_wiring.py b/tests/hitl/test_agent_channel_wiring.pyindex 4c2ed9b..1e50683 100644--- a/tests/hitl/test_agent_channel_wiring.py+++ b/tests/hitl/test_agent_channel_wiring.py@@ -3,15 +3,13 @@ from cubepi.agent.agent import Agentfrom cubepi.hitl import HitlErrorfrom cubepi.hitl.channel import InMemoryChannelfrom cubepi.providers.faux import FauxProvider, faux_assistant_message-from cubepi.providers.base import Modeldef _agent(channel=None):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("")])return Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),channel=channel,)diff --git a/tests/hitl/test_agent_respond.py b/tests/hitl/test_agent_respond.pyindex 464a412..eb0d94a 100644--- a/tests/hitl/test_agent_respond.py+++ b/tests/hitl/test_agent_respond.py@@ -16,7 +16,7 @@ from cubepi.hitl import ()from cubepi.hitl.channel import CheckpointedChannelfrom cubepi.hitl.middleware import ApprovalPolicyMiddleware-from cubepi.providers.base import Model, TextContent+from cubepi.providers.base import TextContentfrom cubepi.providers.faux import (FauxProvider,faux_assistant_message,@@ -54,7 +54,7 @@ def _two_turn_bash_responses():def _faux_with(responses) -> FauxProvider:- p = FauxProvider()+ p = FauxProvider(provider_id="faux")p.set_responses(responses)return p@@ -62,11 +62,10 @@ def _faux_with(responses) -> FauxProvider:async def test_respond_completes_a_suspended_run():cp = MemoryCheckpointer()ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses(_two_turn_bash_responses())agent = Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),tools=[_bash_tool()],middleware=[ApprovalPolicyMiddleware(ch, policy=lambda c: AskUser()),@@ -104,9 +103,9 @@ async def test_respond_completes_a_suspended_run():async def test_respond_stale_answer():cp = MemoryCheckpointer()ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")+ provider = _faux_with([faux_assistant_message("")])agent = Agent(- provider=_faux_with([faux_assistant_message("")]),- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),channel=ch,checkpointer=cp,thread_id="t-1",@@ -132,9 +131,9 @@ async def test_respond_stale_answer():async def test_respond_no_pending():cp = MemoryCheckpointer()ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")+ provider = _faux_with([faux_assistant_message("")])agent = Agent(- provider=_faux_with([faux_assistant_message("")]),- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),channel=ch,checkpointer=cp,thread_id="t-1",diff --git a/tests/hitl/test_edge_cases.py b/tests/hitl/test_edge_cases.pyindex d24c875..9c53062 100644--- a/tests/hitl/test_edge_cases.py+++ b/tests/hitl/test_edge_cases.py@@ -23,7 +23,6 @@ from cubepi.hitl.testing import NoopChannel, ScriptedChannelfrom cubepi.hitl.types import ApproveRequest, HitlRequestfrom cubepi.providers.base import (AssistantMessage,- Model,TextContent,ToolCall,Usage,@@ -35,11 +34,10 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_messagedef _agent(*, channel=None, checkpointer=None, thread_id=None):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("done")])return Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),channel=channel,checkpointer=checkpointer,thread_id=thread_id,diff --git a/tests/hitl/test_resume_cache_prefix.py b/tests/hitl/test_resume_cache_prefix.pyindex 8109d8a..00b8cab 100644--- a/tests/hitl/test_resume_cache_prefix.py+++ b/tests/hitl/test_resume_cache_prefix.py@@ -12,7 +12,7 @@ from cubepi.checkpointer.sqlite import SQLiteCheckpointerfrom cubepi.hitl import ApproveAnswer, AskUserfrom cubepi.hitl.channel import CheckpointedChannelfrom cubepi.hitl.middleware import ApprovalPolicyMiddleware-from cubepi.providers.base import Model, TextContent+from cubepi.providers.base import TextContentfrom cubepi.providers.faux import (FauxProvider,faux_assistant_message,@@ -50,15 +50,14 @@ def _two_turn_bash_responses():async def _suspend_resume_and_capture(checkpointer):ch = CheckpointedChannel(checkpointer=checkpointer, thread_id="t-1")- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses(_two_turn_bash_responses())captured: list[dict] = []provider.subscribe_request(lambda payload, model: captured.append(payload))agent = Agent(- provider=provider,- model=Model(id="faux", provider="faux"),+ model=provider.model("faux"),tools=[_bash_tool()],middleware=[ApprovalPolicyMiddleware(ch, policy=lambda c: AskUser())],channel=ch,diff --git a/tests/hitl/test_subagent_channel_inheritance.py b/tests/hitl/test_subagent_channel_inheritance.pyindex 616e26d..e42840e 100644--- a/tests/hitl/test_subagent_channel_inheritance.py+++ b/tests/hitl/test_subagent_channel_inheritance.py@@ -6,13 +6,13 @@ from cubepi.hitl import ApproveAnswerfrom cubepi.hitl.channel import InMemoryChannelfrom cubepi.hitl.testing import NoopChannel, ScriptedChannelfrom cubepi.hitl.types import Question-from cubepi.providers.base import Model, TextContent+from cubepi.providers.base import TextContentfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagedef _faux_with(responses):"""Helper: build a FauxProvider preloaded with responses (mirrors real API)."""- p = FauxProvider()+ p = FauxProvider(provider_id="faux")p.set_responses(responses)return p@@ -49,9 +49,9 @@ async def test_subagent_inherits_parent_channel():async def subagent_execute(call_id, args, *, signal=None, on_update=None):# The subagent factory uses the same channel object as parent.+ inner_provider = _faux_with([faux_assistant_message("")])inner = Agent(- provider=_faux_with([faux_assistant_message("")]),- model=Model(id="faux", provider="faux"),+ model=inner_provider.model("faux"),channel=parent_ch,)# Verify same channel@@ -66,9 +66,9 @@ async def test_subagent_inherits_parent_channel():execution_mode="sequential",)+ parent_provider = _faux_with([faux_assistant_message("")])parent = Agent(- provider=_faux_with([faux_assistant_message("")]),- model=Model(id="faux", provider="faux"),+ model=parent_provider.model("faux"),tools=[subagent_tool],channel=parent_ch,)diff --git a/tests/mcp/test_adapter.py b/tests/mcp/test_adapter.pyindex 864b16c..b928298 100644--- a/tests/mcp/test_adapter.py+++ b/tests/mcp/test_adapter.py@@ -322,7 +322,7 @@ async def test_agent_abort_during_mcp_call_does_not_raise() -> None:from cubepi.agent.agent import Agentfrom cubepi.mcp._adapter import make_mcp_agent_tool- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagein_flight = _asyncio.Event()@@ -338,7 +338,7 @@ async def test_agent_abort_during_mcp_call_does_not_raise() -> None:input_schema={"type": "object", "properties": {}, "required": []},call_remote=_slow,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -351,8 +351,7 @@ async def test_agent_abort_during_mcp_call_does_not_raise() -> None:])agent = Agent(- provider=provider,- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),system_prompt="s",tools=[mcp_tool],)diff --git a/tests/middleware/compaction/test_summarizer.py b/tests/middleware/compaction/test_summarizer.pyindex 82abb34..7619841 100644--- a/tests/middleware/compaction/test_summarizer.py+++ b/tests/middleware/compaction/test_summarizer.py@@ -56,7 +56,7 @@ class _FakeProvider:async def test_summarize_uses_provider_generate_with_common_overrides() -> None:provider = _FakeProvider(" Compressed summary. ")- model = Model(id="summary-model", provider="faux")+ model = Model(id="summary-model", provider_id="faux")signal = asyncio.Event()result = await summarize(@@ -86,7 +86,7 @@ async def test_summarize_merges_existing_state() -> None:result = await summarize(provider=provider,- model=Model(id="summary-model", provider="faux"),+ model=Model(id="summary-model", provider_id="faux"),messages_to_summarize=[UserMessage(content=[TextContent(text="new")])],existing=existing,)@@ -121,7 +121,7 @@ async def test_summarize_raises_on_provider_error_message() -> None:try:await summarize(provider=_ErrorProvider(""),- model=Model(id="summary-model", provider="faux"),+ model=Model(id="summary-model", provider_id="faux"),messages_to_summarize=[UserMessage(content=[TextContent(text="new")])],existing=None,)diff --git a/tests/middleware/test_after_model_response.py b/tests/middleware/test_after_model_response.pyindex 3e40ac9..3ac586d 100644--- a/tests/middleware/test_after_model_response.py+++ b/tests/middleware/test_after_model_response.py@@ -2,7 +2,7 @@import pytest-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.agent.types import AgentContextfrom cubepi.middleware.base import Middleware, TurnAction, compose_middlewarefrom cubepi.providers.base import (@@ -126,7 +126,7 @@ def test_no_middleware_hook_absent() -> None:@pytest.mark.asyncioasync def test_agent_stops_when_middleware_returns_stop() -> None:"""decision='stop' terminates after the first model response."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")# Two responses queued; second should never fire because we stop after first.provider.set_responses([@@ -135,8 +135,7 @@ async def test_agent_stops_when_middleware_returns_stop() -> None:])agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_Stop()],)await agent.prompt("hi")@@ -148,7 +147,7 @@ async def test_agent_stops_when_middleware_returns_stop() -> None:@pytest.mark.asyncioasync def test_agent_loops_when_middleware_returns_loop_to_model() -> None:"""decision='loop_to_model' re-invokes the model with inject_messages."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("first"),@@ -170,8 +169,7 @@ async def test_agent_loops_when_middleware_returns_loop_to_model() -> None:return Noneagent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_LoopOnce()],)await agent.prompt("hi")@@ -185,7 +183,7 @@ async def test_agent_loops_when_middleware_returns_loop_to_model() -> None:async def test_agent_direct_hook_returning_none_emits_message_end() -> None:"""When after_model_response is passed directly (bypassing compose) andreturns None, the loop still emits message_end via the dedicated branch."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])hook_calls: list[AssistantMessage] = []@@ -195,8 +193,7 @@ async def test_agent_direct_hook_returning_none_emits_message_end() -> None:return Noneagent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),after_model_response=_hook,)@@ -216,11 +213,10 @@ async def test_agent_direct_hook_returning_none_emits_message_end() -> None:@pytest.mark.asyncioasync def test_agent_no_middleware_natural_flow() -> None:"""Without after_model_response middleware, natural flow proceeds."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),)events: list[str] = []@@ -258,11 +254,10 @@ async def test_ctx_extra_flows_into_loop_context(tmp_path) -> None:# Seed extra BEFORE prompt by saving directlyawait cp.save_extra("t", {"seeded": "value"})- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),checkpointer=cp,thread_id="t",middleware=[_Observe()],@@ -292,11 +287,10 @@ async def test_turn_action_response_persists_in_agent_state(tmp_path) -> None:return TurnAction(response=_mk("MUTATED"))cp = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ORIGINAL")])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),checkpointer=cp,thread_id="t",middleware=[_Mutate()],@@ -342,7 +336,7 @@ async def test_turn_action_inject_messages_persist(tmp_path) -> None:)cp = MemoryCheckpointer()- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("first"),@@ -350,8 +344,7 @@ async def test_turn_action_inject_messages_persist(tmp_path) -> None:])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),checkpointer=cp,thread_id="t",middleware=[_InjectOnce()],diff --git a/tests/middleware/test_base.py b/tests/middleware/test_base.pyindex 551f5b8..c385816 100644--- a/tests/middleware/test_base.py+++ b/tests/middleware/test_base.py@@ -202,7 +202,7 @@ class TestAgentMiddlewareWiring:async def test_agent_uses_middleware_convert_to_llm(self):"""Middleware-declared convert_to_llm must be wired up by Agent."""- from cubepi import Agent, Model+ from cubepi import Agentfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagecaptured: dict = {}@@ -214,11 +214,10 @@ class TestAgentMiddlewareWiring:captured["ctx"] = ctxreturn list(messages)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[MarkConvert()],)await agent.prompt("hi")diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.pyindex 49530f5..02a8436 100644--- a/tests/middleware/test_compaction.py+++ b/tests/middleware/test_compaction.py@@ -9,6 +9,7 @@ from cubepi.middleware.compaction import _load_statefrom cubepi.middleware.compaction.state import message_ref, message_refsfrom cubepi.providers.base import (AssistantMessage,+ BoundModel,Message,Model,StreamOptions,@@ -71,8 +72,10 @@ def _make_middleware(max_tokens_before: int = 1000,) -> CompactionMiddleware:return CompactionMiddleware(- summary_provider=provider,- summary_model=Model(id="summary-model", provider="faux"),+ summary_model=BoundModel(+ provider=provider,+ spec=Model(id="summary-model", provider_id="faux"),+ ),max_tokens_before_compact=max_tokens_before,keep_recent_messages=2,max_summary_tokens=512,diff --git a/tests/middleware/test_on_run_end.py b/tests/middleware/test_on_run_end.pyindex 71044e6..5571783 100644--- a/tests/middleware/test_on_run_end.py+++ b/tests/middleware/test_on_run_end.py@@ -4,7 +4,7 @@ from __future__ import annotationsimport pytest-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.agent.types import AgentContextfrom cubepi.middleware.base import Middleware, compose_middlewarefrom cubepi.providers.base import TextContent, UserMessage@@ -85,7 +85,7 @@ async def test_all_none_returns_none() -> None:@pytest.mark.asyncioasync def test_on_run_end_fires_after_main_run() -> None:"""on_run_end injects a message and the agent runs one more model call."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("main"),@@ -101,8 +101,7 @@ async def test_on_run_end_fires_after_main_run() -> None:return [UserMessage(content=[TextContent(text="reflect now")])]agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_Reflect()],)await agent.prompt("hi")@@ -114,7 +113,7 @@ async def test_on_run_end_fires_after_main_run() -> None:@pytest.mark.asyncioasync def test_on_run_end_fires_exactly_once() -> None:"""Reflection pass does NOT trigger another on_run_end (_reflection_fired guard)."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("main"),@@ -131,8 +130,7 @@ async def test_on_run_end_fires_exactly_once() -> None:return [UserMessage(content=[TextContent(text="reflect")])]agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_CountFires()],)await agent.prompt("hi")@@ -144,7 +142,7 @@ async def test_on_run_end_fires_exactly_once() -> None:@pytest.mark.asyncioasync def test_on_run_end_none_does_not_add_turn() -> None:"""Returning None from on_run_end does not trigger an extra model call."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("main")])class _NoOp(Middleware):@@ -152,8 +150,7 @@ async def test_on_run_end_none_does_not_add_turn() -> None:return Noneagent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_NoOp()],)await agent.prompt("hi")@@ -166,7 +163,7 @@ async def test_on_run_end_injected_messages_in_history() -> None:"""Messages injected by on_run_end appear in agent.state.messages."""from cubepi.providers.base import AssistantMessage- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("main"),@@ -179,8 +176,7 @@ async def test_on_run_end_injected_messages_in_history() -> None:return [UserMessage(content=[TextContent(text="reflect")])]agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_Inject()],)await agent.prompt("hi")@@ -201,7 +197,7 @@ async def test_on_run_end_fires_via_should_stop_after_turn() -> None:"""on_run_end fires when should_stop_after_turn exits the inner loop."""from cubepi.agent.types import ShouldStopAfterTurnContext- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("main"),@@ -224,8 +220,7 @@ async def test_on_run_end_fires_via_should_stop_after_turn() -> None:return [UserMessage(content=[TextContent(text="reflect")])]agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_StopAfterFirst(), _Reflect()],)await agent.prompt("hi")@@ -237,7 +232,7 @@ async def test_on_run_end_fires_via_should_stop_after_turn() -> None:@pytest.mark.asyncioasync def test_on_run_end_skipped_on_error() -> None:"""on_run_end does NOT fire when stop_reason is error/aborted."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")err_msg = faux_assistant_message("oops")err_msg = err_msg.model_copy(update={"stop_reason": "error"})provider.set_responses([err_msg])@@ -250,8 +245,7 @@ async def test_on_run_end_skipped_on_error() -> None:return Noneagent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),middleware=[_Reflect()],)await agent.prompt("hi")diff --git a/tests/middleware/test_subagents.py b/tests/middleware/test_subagents.pyindex 5f1f7e2..1c65801 100644--- a/tests/middleware/test_subagents.py+++ b/tests/middleware/test_subagents.py@@ -7,7 +7,7 @@ from pydantic import BaseModelfrom cubepi import Agent, AgentTool, AgentToolResult, TextContent, UserMessagefrom cubepi.middleware.subagents import SubagentMiddleware, SubagentSpec-from cubepi.providers.base import AssistantMessage, MessageStream, Model, StreamEvent+from cubepi.providers.base import AssistantMessage, MessageStream, StreamEventfrom cubepi.providers.faux import FauxProvider, faux_assistant_message, faux_tool_call@@ -17,7 +17,7 @@ def _make_middleware(subagents: dict[str, SubagentSpec] | None = None,**kwargs: Any,) -> SubagentMiddleware:- provider = provider or FauxProvider()+ provider = provider or FauxProvider(provider_id="faux")subagents = subagents or {"general-purpose": SubagentSpec(name="general-purpose",@@ -27,8 +27,7 @@ def _make_middleware(}return SubagentMiddleware(subagents=subagents,- default_provider=provider,- default_model=Model(id="faux-1", provider="faux"),+ default_model=provider.model("faux-1"),**kwargs,)@@ -92,7 +91,7 @@ def test_shared_tools_filter_is_configurable() -> None:async def test_subagent_tool_dispatches_to_child_agent() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("subagent reply")])middleware = _make_middleware(provider=provider)[tool] = middleware.tools@@ -113,7 +112,7 @@ async def test_subagent_tool_dispatches_to_child_agent() -> None:async def test_unknown_subagent_type_falls_back_to_general_purpose() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("fallback reply")])middleware = _make_middleware(provider=provider)[tool] = middleware.tools@@ -174,7 +173,7 @@ async def test_subagent_tool_returns_only_child_final_answer() -> None:parameters=_EchoParams,execute=_echo,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message(@@ -204,7 +203,7 @@ async def test_subagent_tool_returns_only_child_final_answer() -> None:async def test_event_mapper_and_handler_receive_child_events() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("mapped reply")])handled: list[tuple[str, dict[str, Any]]] = []@@ -236,7 +235,7 @@ async def test_event_mapper_and_handler_receive_child_events() -> None:async def test_string_event_mapper_payload_is_treated_as_one_payload() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("mapped reply")])handled: list[tuple[str, str]] = []@@ -269,9 +268,9 @@ async def test_string_event_mapper_payload_is_treated_as_one_payload() -> None:def test_child_error_handles_empty_non_assistant_and_error_assistant_states() -> None:+ provider = FauxProvider(provider_id="faux")child = Agent(- provider=FauxProvider(),- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),)assert SubagentMiddleware._child_error(child) is None@@ -289,9 +288,9 @@ def test_child_error_handles_empty_non_assistant_and_error_assistant_states() ->def test_final_assistant_text_returns_empty_without_assistant_message() -> None:+ provider = FauxProvider(provider_id="faux")child = Agent(- provider=FauxProvider(),- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),)child.state.messages = [UserMessage(content=[TextContent(text="user only")])]@@ -322,7 +321,7 @@ async def test_inner_agent_failure_returns_tool_error() -> None:async def test_tracer_detaches_after_success_and_cancel() -> None:tracer = _FakeTracer(awaitable_detach=True)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])middleware = _make_middleware(provider=provider, tracer=tracer)[tool] = middleware.tools@@ -457,9 +456,9 @@ def test_tracer_attach_failure_is_ignored() -> None:raise RuntimeError("attach failed")middleware = _make_middleware(tracer=_BadTracer())+ provider = FauxProvider(provider_id="faux")child = Agent(- provider=FauxProvider(),- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),)assert middleware._attach_tracer(child) is Nonediff --git a/tests/middleware/test_transform_system_prompt.py b/tests/middleware/test_transform_system_prompt.pyindex 0e2d9c8..cf7eedb 100644--- a/tests/middleware/test_transform_system_prompt.py+++ b/tests/middleware/test_transform_system_prompt.py@@ -2,7 +2,7 @@import pytest-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.agent.types import AgentContextfrom cubepi.middleware.base import Middleware, compose_middlewarefrom cubepi.providers.faux import FauxProvider, faux_assistant_message@@ -69,11 +69,10 @@ async def test_agent_applies_transform_system_prompt() -> None:captured.append(sp)return sp + "\n[C]"- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),system_prompt="base",middleware=[_AppendA(), _AppendB(), _Capturing()],)@@ -97,13 +96,12 @@ async def test_agent_without_middleware_passes_system_prompt_unchanged() -> Noneself, model, messages, system_prompt=system_prompt, **kw)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("ok")])provider.stream = _capturing_stream.__get__(provider, FauxProvider) # type: ignore[method-assign]agent = Agent(- model=Model(id="test", provider="faux"),- provider=provider,+ model=provider.model("test"),system_prompt="base",)await agent.prompt("hi")diff --git a/tests/providers/test_anthropic.py b/tests/providers/test_anthropic.pyindex b51d4ae..decc280 100644--- a/tests/providers/test_anthropic.py+++ b/tests/providers/test_anthropic.py@@ -307,7 +307,7 @@ class TestCacheControlOnTools:def _anthropic_model(*, reasoning: bool = False, max_tokens: int = 8192) -> Model:return Model(id="claude-sonnet-4-20250514" if not reasoning else "claude-sonnet-4-20250514",- provider="anthropic",+ provider_id="anthropic",api="anthropic",reasoning=reasoning,max_tokens=max_tokens,diff --git a/tests/providers/test_base.py b/tests/providers/test_base.pyindex 1710a80..df7faf3 100644--- a/tests/providers/test_base.py+++ b/tests/providers/test_base.py@@ -27,7 +27,7 @@ from cubepi.providers.base import (class TestFormatProviderError:@staticmethoddef _model() -> Model:- return Model(id="gpt-4o", provider="openai")+ return Model(id="gpt-4o", provider_id="openai")def test_includes_provider_model_and_exception(self):msg = format_provider_error(RuntimeError("boom"), self._model())@@ -131,7 +131,7 @@ class TestMessageTypes:assert msg.is_error is Falsedef test_model_defaults(self):- m = Model(id="gpt-4o", provider="openai")+ m = Model(id="gpt-4o", provider_id="openai")assert m.context_window == 200_000assert m.max_tokens == 8192assert m.reasoning is False@@ -244,7 +244,7 @@ class TestBaseProviderGenerate:)result = await provider.generate(- Model(id="gpt-4o", provider="openai"),+ Model(id="gpt-4o", provider_id="openai"),[UserMessage(content=[TextContent(text="hi")])],system_prompt="system",)@@ -254,7 +254,7 @@ class TestBaseProviderGenerate:async def test_generate_applies_common_per_call_overrides(self):provider = _RecordingProvider(AssistantMessage(content=[]))- base_model = Model(id="gpt-4o", provider="openai", max_tokens=128)+ base_model = Model(id="gpt-4o", provider_id="openai", max_tokens=128)base_options = StreamOptions(thinking="low")budgets = ThinkingBudgets(low=4096)diff --git a/tests/providers/test_bound_model.py b/tests/providers/test_bound_model.pynew file mode 100644index 0000000..cbb0991--- /dev/null+++ b/tests/providers/test_bound_model.py@@ -0,0 +1,61 @@+from cubepi.providers.base import AssistantMessage, BaseProvider, BoundModel, Model+from cubepi.providers.base import TextContent+from cubepi.providers.faux import FauxProvider+++def test_base_provider_model_binds_runtime_provider_and_model_spec() -> None:+ provider = BaseProvider(provider_id="catalog-anthropic")++ bound = provider.model(+ "claude-sonnet-4-5-20250929",+ api="anthropic-messages",+ reasoning=True,+ context_window=200_000,+ max_tokens=8192,+ temperature=0.2,+ )++ assert isinstance(bound, BoundModel)+ assert bound.provider is provider+ assert bound.spec == Model(+ id="claude-sonnet-4-5-20250929",+ provider_id="catalog-anthropic",+ api="anthropic-messages",+ reasoning=True,+ context_window=200_000,+ max_tokens=8192,+ temperature=0.2,+ )+++def test_base_provider_model_leaves_provider_id_empty_by_default() -> None:+ provider = BaseProvider()++ bound = provider.model("local-model")++ assert bound.provider is provider+ assert bound.spec.provider_id == ""+ assert bound.spec.id == "local-model"+++def test_concrete_provider_accepts_provider_id() -> None:+ provider = FauxProvider(provider_id="faux-catalog")++ bound = provider.model("faux-1")++ assert bound.provider is provider+ assert bound.spec.provider_id == "faux-catalog"+ assert bound.spec.id == "faux-1"+++def test_assistant_message_uses_model_provider_id_metadata() -> None:+ model = Model(id="faux-1", provider_id="faux-provider")++ message = AssistantMessage(+ content=[TextContent(text="ok")],+ provider_id=model.provider_id,+ model_id=model.id,+ )++ assert message.provider_id == "faux-provider"+ assert message.model_id == "faux-1"diff --git a/tests/providers/test_content_index.py b/tests/providers/test_content_index.pyindex 9ddae38..abc68c4 100644--- a/tests/providers/test_content_index.py+++ b/tests/providers/test_content_index.py@@ -9,7 +9,7 @@ from cubepi.providers.faux import (def _make_model() -> Model:- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")class TestContentIndexTextOnly:diff --git a/tests/providers/test_faux.py b/tests/providers/test_faux.pyindex 63f1bfe..97ccd90 100644--- a/tests/providers/test_faux.py+++ b/tests/providers/test_faux.py@@ -68,7 +68,7 @@ class TestFauxProvider:def _make_model(self):from cubepi.providers.base import Model- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_basic_text_response(self):provider = FauxProvider()@@ -298,7 +298,7 @@ class TestFauxProvider:async def test_provider_metadata_set(self):provider = FauxProvider()provider.set_responses([faux_assistant_message("hello")])- model = Model(id="faux-1", provider="faux")+ model = Model(id="faux-1", provider_id="faux")stream = await provider.stream(model, [])_ = [e async for e in stream]result = await stream.result()@@ -308,7 +308,7 @@ class TestFauxProvider:async def test_provider_metadata_on_partial_events(self):provider = FauxProvider()provider.set_responses([faux_assistant_message("hello")])- model = Model(id="faux-1", provider="faux")+ model = Model(id="faux-1", provider_id="faux")stream = await provider.stream(model, [])events = [e async for e in stream]start_event = next(e for e in events if e.type == "start")@@ -323,7 +323,7 @@ class TestFauxProviderExtendedFactory:def _make_model(self):from cubepi.providers.base import Model- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_extended_sync_factory_receives_all_args(self):"""Factory with 4 params receives system_prompt and tools."""@@ -470,7 +470,7 @@ class TestFauxProviderPromptCache:def _make_model(self):from cubepi.providers.base import Model- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_first_call_populates_cache_write(self):"""First call should have cache_write > 0 and cache_read == 0."""@@ -666,7 +666,7 @@ class TestFauxProviderProduceExceptionHandling:"""Tests for the _produce exception handler (lines 279-290)."""def _make_model(self):- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_factory_raising_exception_produces_error_result(self):"""When a factory raises an Exception, _produce catches it and@@ -720,7 +720,7 @@ class TestFauxProviderAbortDuringBlocks:"""Tests for abort signal checks during block iteration and chunk streaming."""def _make_model(self):- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_abort_between_blocks(self):"""Abort signal set between blocks triggers the block-level abort@@ -877,7 +877,7 @@ class TestFauxProviderCacheTokenCalculation:"""Tests for cache token calculation logic (lines 185-216)."""def _make_model(self):- return Model(id="faux-1", provider="faux")+ return Model(id="faux-1", provider_id="faux")async def test_cache_usage_first_call_structure(self):"""First call: input_tokens == prompt_tokens, cache_write == prompt_tokens,diff --git a/tests/providers/test_listener_registry.py b/tests/providers/test_listener_registry.pyindex 3f6c1ba..4e0e16f 100644--- a/tests/providers/test_listener_registry.py+++ b/tests/providers/test_listener_registry.py@@ -16,7 +16,7 @@ from cubepi.providers.base import Model, StreamEvent, StreamOptions, UserMessagefrom cubepi.providers.faux import FauxProvider, faux_assistant_message-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")async def _drain(stream) -> None:@@ -385,8 +385,8 @@ class TestConcurrentStreams:faux_assistant_message("b"),])- model_a = Model(id="faux-a", provider="faux")- model_b = Model(id="faux-b", provider="faux")+ model_a = Model(id="faux-a", provider_id="faux")+ model_b = Model(id="faux-b", provider_id="faux")ms_a, ms_b = await asyncio.gather(provider.stream(model_a, [UserMessage(content=[])]),diff --git a/tests/providers/test_models.py b/tests/providers/test_models.pyindex 84b3218..8957a61 100644--- a/tests/providers/test_models.py+++ b/tests/providers/test_models.py@@ -23,7 +23,7 @@ def _model() -> Model:return Model(id=model_id,- provider=provider,+ provider_id=provider,reasoning=reasoning,thinking_level_map=thinking_level_map,)diff --git a/tests/providers/test_openai.py b/tests/providers/test_openai.pyindex fbb5465..05b1595 100644--- a/tests/providers/test_openai.py+++ b/tests/providers/test_openai.py@@ -26,7 +26,7 @@ from cubepi.providers.base import (def _model() -> Model:- return Model(id="gpt-4o", provider="openai", api="openai")+ return Model(id="gpt-4o", provider_id="openai", api="openai")def _make_chunk(diff --git a/tests/providers/test_openai_extras_and_schema.py b/tests/providers/test_openai_extras_and_schema.pyindex 16898ef..c7de4d7 100644--- a/tests/providers/test_openai_extras_and_schema.py+++ b/tests/providers/test_openai_extras_and_schema.py@@ -22,7 +22,7 @@ from cubepi.providers.openai import OpenAIProviderdef _model() -> Model:- return Model(id="gpt-4o", provider="openai", api="openai")+ return Model(id="gpt-4o", provider_id="openai", api="openai")def _make_chunk(*, finish_reason=None):diff --git a/tests/providers/test_openai_reasoning.py b/tests/providers/test_openai_reasoning.pyindex 8b52aff..ee3559b 100644--- a/tests/providers/test_openai_reasoning.py+++ b/tests/providers/test_openai_reasoning.py@@ -22,7 +22,7 @@ from cubepi.providers.base import (def _model() -> Model:- return Model(id="deepseek-chat", provider="deepseek", api="openai")+ return Model(id="deepseek-chat", provider_id="deepseek", api="openai")def _make_chunk(diff --git a/tests/test_errors.py b/tests/test_errors.pyindex bc30638..abc271a 100644--- a/tests/test_errors.py+++ b/tests/test_errors.py@@ -31,7 +31,7 @@ def _model(model_id: str = "gpt-4o",context_window: int = 128_000,) -> Model:- return Model(id=model_id, provider=provider, context_window=context_window)+ return Model(id=model_id, provider_id=provider, context_window=context_window)def _messages(text: str = "hello") -> list:diff --git a/tests/tracing/test_attach_atomicity.py b/tests/tracing/test_attach_atomicity.pyindex b01cb16..0319d05 100644--- a/tests/tracing/test_attach_atomicity.py+++ b/tests/tracing/test_attach_atomicity.py@@ -17,7 +17,7 @@ from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProviderfrom cubepi.tracing import Tracer-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")def _build_unattached() -> tuple[Agent, FauxProvider, Tracer]:@@ -33,8 +33,8 @@ def _build_unattached() -> tuple[Agent, FauxProvider, Tracer]:def force_flush(self, timeout_millis: int = 30_000) -> bool:return True- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="t")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="t")tracer = Tracer(service_name="t",agent_name="t",diff --git a/tests/tracing/test_content_recording.py b/tests/tracing/test_content_recording.pyindex a0b72cf..0b33a5e 100644--- a/tests/tracing/test_content_recording.py+++ b/tests/tracing/test_content_recording.py@@ -29,7 +29,7 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class _Capture(SpanExporter):@@ -59,10 +59,9 @@ def _json_attr(span: ReadableSpan, key: str) -> Any:async def _build(*, record_content: bool, redact=None, tools=None):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")agent = Agent(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),system_prompt="be helpful, be careful",tools=tools,)diff --git a/tests/tracing/test_mcp_spans.py b/tests/tracing/test_mcp_spans.pyindex 1b02fb9..ff834fa 100644--- a/tests/tracing/test_mcp_spans.py+++ b/tests/tracing/test_mcp_spans.py@@ -359,7 +359,7 @@ class TestTracerProviderRouting:async def test_mcp_span_lands_in_tracer_exporter(self):from cubepi.agent.agent import Agent- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import (FauxProvider,faux_assistant_message,@@ -389,7 +389,7 @@ class TestTracerProviderRouting:protocol_version="2025-11-25",)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -400,8 +400,7 @@ class TestTracerProviderRouting:])agent = Agent(- provider=provider,- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),system_prompt="s",tools=[mcp_tool],)@@ -444,7 +443,7 @@ class TestTracerProviderRouting:exporter. Refcounted register/unregister is the contract(codex round-6 review on PR #86)."""from cubepi.agent.agent import Agent- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer@@ -465,7 +464,7 @@ class TestTracerProviderRouting:},call_remote=call_remote,)- provider_a = FauxProvider()+ provider_a = FauxProvider(provider_id="faux")provider_a.append_responses([faux_assistant_message(@@ -476,8 +475,7 @@ class TestTracerProviderRouting:])return Agent(- provider=provider_a,- model=Model(id="faux-1", provider="faux"),+ model=provider_a.model("faux-1"),system_prompt="s",tools=[mcp_tool],)@@ -597,7 +595,7 @@ class TestMCPSpanParentage:leg and Tracer B's exporter would receive a stray span withagent A's trace_id (codex round-7 review on PR #86)."""from cubepi.agent.agent import Agent- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer@@ -616,7 +614,7 @@ class TestMCPSpanParentage:},call_remote=call_remote,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -627,8 +625,7 @@ class TestMCPSpanParentage:])return Agent(- provider=provider,- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),system_prompt="s",tools=[mcp_tool],)@@ -693,7 +690,7 @@ class TestMCPSpanParentage:from cubepi.agent.agent import Agentfrom cubepi.mcp._adapter import make_mcp_agent_tool- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer@@ -719,7 +716,7 @@ class TestMCPSpanParentage:},call_remote=call_remote,)- prov = FauxProvider()+ prov = FauxProvider(provider_id="faux")prov.append_responses([faux_assistant_message(@@ -730,8 +727,7 @@ class TestMCPSpanParentage:])return Agent(- provider=prov,- model=Model(id="faux-1", provider="faux"),+ model=prov.model("faux-1"),system_prompt="s",tools=[mcp_tool],)@@ -794,7 +790,7 @@ class TestMCPSpanParentage:from cubepi.agent.agent import Agentfrom cubepi.agent.types import AgentToolfrom cubepi.mcp._adapter import make_mcp_agent_tool- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer@@ -820,7 +816,7 @@ class TestMCPSpanParentage:execution_mode="parallel",)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -831,8 +827,7 @@ class TestMCPSpanParentage:])agent = Agent(- provider=provider,- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),system_prompt="s",tools=[mcp_tool],)@@ -872,7 +867,7 @@ class TestMCPSpanParentage:from cubepi.agent.agent import Agentfrom cubepi.mcp import _tracing as mcp_tracingfrom cubepi.mcp._adapter import make_mcp_agent_tool- from cubepi.providers.base import Model, ToolCall+ from cubepi.providers.base import ToolCallfrom cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer@@ -898,7 +893,7 @@ class TestMCPSpanParentage:call_remote=call_remote,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -909,8 +904,7 @@ class TestMCPSpanParentage:])agent = Agent(- provider=provider,- model=Model(id="faux-1", provider="faux"),+ model=provider.model("faux-1"),system_prompt="s",tools=[mcp_tool],)diff --git a/tests/tracing/test_meter.py b/tests/tracing/test_meter.pyindex ee37023..6a07e7b 100644--- a/tests/tracing/test_meter.py+++ b/tests/tracing/test_meter.py@@ -22,7 +22,7 @@ from cubepi.tracing import Meterfrom cubepi.tracing.schema import SCHEMA_URL, SCOPE_NAME-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")def _build_meter() -> tuple[Meter, InMemoryMetricReader]:@@ -68,9 +68,9 @@ def _by_name(points: list[tuple[str, Any]], name: str) -> list[Any]:class TestDurationMetric:async def test_chat_and_invoke_agent_duration(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()meter.attach(agent)@@ -90,9 +90,9 @@ class TestDurationMetric:class TestTokenUsageMetric:async def test_token_usage_input_and_output(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi there")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()meter.attach(agent)@@ -111,7 +111,7 @@ class TestTimeToFirstChunkMetric:async def test_ttfc_emitted_when_first_chunk_seen(self):provider = FauxProvider(tokens_per_second=200.0)provider.append_responses([faux_assistant_message("hello world")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()meter.attach(agent)@@ -129,9 +129,9 @@ class TestRequestModelOnChatMetrics:failed / cancelled requests (no response body, no response.model)can still be grouped by the requested model. Codex P2 findingon PR #85."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()meter.attach(agent)@@ -173,7 +173,7 @@ class TestProviderOnToolMetrics:return AgentToolResult(content=[TextContent(text="ok")])tool = AgentTool(name="t", description="t", parameters=P, execute=run)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -183,7 +183,7 @@ class TestProviderOnToolMetrics:faux_assistant_message("final"),])- agent = Agent(provider=provider, model=MODEL, system_prompt="s", tools=[tool])+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s", tools=[tool])meter, reader = _build_meter()meter.attach(agent)@@ -219,10 +219,13 @@ class TestConcurrentAgents:A → response B). With instance-level state the first responsewould record duration against B's attrs; with per-attach stateeach one records against its own attrs."""- agent_a = Agent(provider=FauxProvider(), model=MODEL, system_prompt="s")+ agent_a = Agent(+ model=FauxProvider(provider_id="faux").model(MODEL.id),+ system_prompt="s",+ )+ provider_b = FauxProvider(provider_id="faux")agent_b = Agent(- provider=FauxProvider(),- model=Model(id="faux-2", provider="faux"),+ model=provider_b.model("faux-2"),system_prompt="s",)meter, reader = _build_meter()@@ -241,12 +244,12 @@ class TestConcurrentAgents:# Open both chat windows.req_a({"messages": []}, MODEL)- req_b({"messages": []}, Model(id="faux-2", provider="faux"))+ req_b({"messages": []}, Model(id="faux-2", provider_id="faux"))# Close them in original order.body_a = {"model": "faux-1", "usage": {"input_tokens": 1, "output_tokens": 2}}body_b = {"model": "faux-2", "usage": {"input_tokens": 3, "output_tokens": 4}}resp_a(body_a, MODEL, None)- resp_b(body_b, Model(id="faux-2", provider="faux"), None)+ resp_b(body_b, Model(id="faux-2", provider_id="faux"), None)points = _all_metric_points(reader)chat_durations = [@@ -274,9 +277,9 @@ class TestAttachedContextManager:wrapper that detaches on exit."""async def test_basic_usage(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()async with meter.attached(agent):@@ -288,8 +291,8 @@ class TestAttachedContextManager:assert len(durations) >= 2 # chat + invoke_agentasync def test_exception_inside_block_still_detaches(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, _reader = _build_meter()try:async with meter.attached(agent):@@ -307,9 +310,9 @@ class TestAttachedDefensiveBranches:healthy body return — covers the defensive branch."""async def test_swallows_detach_exception(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, _reader = _build_meter()# Patch attach to return a detach that raises.@@ -333,8 +336,8 @@ class TestAttachedDefensiveBranches:class TestNoMetricsWithoutListeners:async def test_detach_stops_emission(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")meter, reader = _build_meter()detach = meter.attach(agent)detach()diff --git a/tests/tracing/test_oneshot.py b/tests/tracing/test_oneshot.pyindex a2e25a9..6fe9351 100644--- a/tests/tracing/test_oneshot.py+++ b/tests/tracing/test_oneshot.py@@ -24,7 +24,7 @@ from cubepi.tracing import Tracerfrom cubepi.tracing.tracer import _OneShotSession-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class InMemoryExporter(SpanExporter):@@ -61,9 +61,9 @@ def _spans_by_name(spans: list[ReadableSpan]) -> dict[str, ReadableSpan]:@pytest.mark.asyncioasync def test_oneshot_session_type() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, _ = _make_tracer()- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:assert isinstance(session, _OneShotSession)await tracer.shutdown()@@ -73,7 +73,7 @@ async def test_oneshot_session_uses_provider_generate() -> None:provider = _GenerateOnlyProvider()tracer, _ = _make_tracer()- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:text = await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -90,13 +90,12 @@ async def test_oneshot_session_uses_provider_generate() -> None:@pytest.mark.asyncioasync def test_oneshot_produces_root_and_chat_spans() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hello world")])tracer, exporter = _make_tracer()async with tracer.oneshot(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),operation="test_op",metadata={"conversation_id": "conv-123", "user_id": "usr-456"},) as session:@@ -141,13 +140,12 @@ async def test_oneshot_produces_root_and_chat_spans() -> None:@pytest.mark.asyncioasync def test_oneshot_metadata_on_root_span() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])tracer, exporter = _make_tracer()async with tracer.oneshot(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),operation="consolidate_memory",metadata={"conversation_id": "conv-abc"},) as session:@@ -172,13 +170,12 @@ async def test_oneshot_user_metadata_cannot_shadow_reserved_oneshot_operation()"""If a caller's metadata dict includes 'oneshot_operation', the valuederived from the operation argument must still win — the documented`cubepi trace ls --meta oneshot_operation=<op>` filter depends on it."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])tracer, exporter = _make_tracer()async with tracer.oneshot(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),operation="real_op",metadata={"oneshot_operation": "user_supplied_value"},) as session:@@ -200,11 +197,11 @@ async def test_oneshot_user_metadata_cannot_shadow_reserved_oneshot_operation()@pytest.mark.asyncioasync def test_oneshot_no_metadata_ok() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("result")])tracer, exporter = _make_tracer()- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:text = await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -220,13 +217,12 @@ async def test_oneshot_no_metadata_ok() -> None:@pytest.mark.asyncioasync def test_oneshot_record_content_captures_messages() -> None:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("answer")])tracer, exporter = _make_tracer(record_content=True)async with tracer.oneshot(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),record_content=True,) as session:await session.generate(@@ -265,7 +261,7 @@ async def test_oneshot_generate_error_event_raises_and_marks_root() -> None:from opentelemetry.trace import StatusCodefrom unittest.mock import AsyncMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, exporter = _make_tracer()error_stream = MessageStream()@@ -275,7 +271,7 @@ async def test_oneshot_generate_error_event_raises_and_marks_root() -> None:)with patch.object(provider, "stream", new=AsyncMock(return_value=error_stream)):with pytest.raises(RuntimeError, match="boom"):- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -308,13 +304,13 @@ async def test_oneshot_subscribe_failure_closes_stream_file(tmp_path) -> None:atexit_flush=False,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")with patch.object(provider, "subscribe_chunk", side_effect=RuntimeError("chunk sub failed")):with pytest.raises(RuntimeError, match="chunk sub failed"):- async with tracer.oneshot(provider=provider, model=MODEL):+ async with tracer.oneshot(model=provider.model(MODEL.id)):pass # pragma: no coverawait tracer.shutdown()@@ -339,7 +335,7 @@ async def test_oneshot_propagates_silent_producer_failure() -> None:from opentelemetry.trace import StatusCodefrom unittest.mock import AsyncMock, MagicMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, exporter = _make_tracer()# Build a stream that emits done normally but whose result() raises@@ -357,7 +353,7 @@ async def test_oneshot_propagates_silent_producer_failure() -> None:with patch.object(provider, "stream", new=AsyncMock(return_value=fake_stream)):with pytest.raises(RuntimeError, match="producer crashed"):- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -378,14 +374,14 @@ async def test_oneshot_subscribe_failure_raises_and_ends_root_span() -> None:"""If provider subscription raises, oneshot re-raises and ends the root span."""from unittest.mock import patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, exporter = _make_tracer()with patch.object(provider, "subscribe_request", side_effect=RuntimeError("subscribe failed")):with pytest.raises(RuntimeError, match="subscribe failed"):- async with tracer.oneshot(provider=provider, model=MODEL):+ async with tracer.oneshot(model=provider.model(MODEL.id)):pass # pragma: no cover — never reachedawait tracer.force_flush()@@ -401,7 +397,7 @@ async def test_oneshot_detacher_exception_is_swallowed() -> None:"""Detach errors during cleanup must not propagate to the caller."""from unittest.mock import patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])tracer, _ = _make_tracer()@@ -417,7 +413,7 @@ async def test_oneshot_detacher_exception_is_swallowed() -> None:return raising_detachwith patch.object(provider, "subscribe_request", side_effect=patched_subscribe):- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:text = await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -435,7 +431,7 @@ async def test_oneshot_passes_signal_to_provider_and_sets_on_cancel() -> None:import asynciofrom unittest.mock import AsyncMock, MagicMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, _ = _make_tracer()captured_options: dict[str, StreamOptions | None] = {"opts": None}@@ -455,7 +451,7 @@ async def test_oneshot_passes_signal_to_provider_and_sets_on_cancel() -> None:return await real_stream(**kwargs)async def do_work():- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:with patch.object(provider, "stream", new=patched_stream):await session.generate(system="sys",@@ -491,10 +487,10 @@ async def test_oneshot_record_stream_writes_jsonl(tmp_path) -> None: # type: igatexit_flush=False,)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("streamed text")])- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:text = await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="hi")])],@@ -521,7 +517,7 @@ async def test_oneshot_partial_subscribe_failure_unwinds_listeners() -> None:listener must be unsubscribed before re-raising (no dangling listeners)."""from unittest.mock import patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, exporter = _make_tracer()# Track how many listeners were subscribed / detached@@ -546,7 +542,7 @@ async def test_oneshot_partial_subscribe_failure_unwinds_listeners() -> None:),pytest.raises(RuntimeError, match="chunk sub failed"),):- async with tracer.oneshot(provider=provider, model=MODEL):+ async with tracer.oneshot(model=provider.model(MODEL.id)):pass # pragma: no coverawait tracer.force_flush()@@ -567,7 +563,7 @@ async def test_oneshot_cancelled_generate_closes_chat_span() -> None:import asynciofrom unittest.mock import AsyncMock, MagicMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")tracer, exporter = _make_tracer()# A stream that never yields (blocks forever) so we can cancel it@@ -579,7 +575,7 @@ async def test_oneshot_cancelled_generate_closes_chat_span() -> None:hanging.__aiter__ = lambda self: hanging_stream()async def do_work():- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:with patch.object(provider, "stream", new=AsyncMock(return_value=hanging)):await session.generate(system="sys",@@ -620,7 +616,7 @@ async def test_oneshot_cancel_mid_stream_marks_open_chat_span_aborted() -> None:tracer, exporter = _make_tracer()async def do_work():- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="q")])],@@ -651,13 +647,13 @@ async def test_oneshot_does_not_interfere_with_concurrent_agent() -> None:from cubepi.agent.agent import Agent- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")# Push responses for both agent and oneshotprovider.append_responses([faux_assistant_message("agent reply")])provider.append_responses([faux_assistant_message("oneshot reply")])tracer, exporter = _make_tracer()- agent = Agent(provider=provider, model=MODEL, system_prompt="sys")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="sys")detach = tracer.attach(agent)# Run agent and oneshot concurrently@@ -666,7 +662,7 @@ async def test_oneshot_does_not_interfere_with_concurrent_agent() -> None:return str(agent.state.messages[-1].content[0].text) # type: ignore[index]async def run_oneshot() -> str:- async with tracer.oneshot(provider=provider, model=MODEL) as session:+ async with tracer.oneshot(model=provider.model(MODEL.id)) as session:return await session.generate(system="sys",messages=[UserMessage(content=[TextContent(text="oneshot q")])],diff --git a/tests/tracing/test_otlp_exporter.py b/tests/tracing/test_otlp_exporter.pyindex 7494f6c..5e755a2 100644--- a/tests/tracing/test_otlp_exporter.py+++ b/tests/tracing/test_otlp_exporter.py@@ -64,7 +64,9 @@ class TestOpenAIRequestAttrs:"messages": [],"service_tier": "scale",}- recorder._on_provider_request(payload, Model(id="gpt-test", provider="openai"))+ recorder._on_provider_request(+ payload, Model(id="gpt-test", provider_id="openai")+ )chat = recorder._run.chat_span # type: ignore[union-attr]# chat_span is an actual SDK Span; read via .attributesattrs = dict(chat.attributes or {})diff --git a/tests/tracing/test_recorder.py b/tests/tracing/test_recorder.pyindex c5858d9..c96befc 100644--- a/tests/tracing/test_recorder.py+++ b/tests/tracing/test_recorder.py@@ -24,7 +24,7 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class InMemoryExporter(SpanExporter):@@ -48,10 +48,9 @@ async def _build(tools: list[AgentTool] | None = None,before_tool_call=None,) -> tuple[Agent, FauxProvider, InMemoryExporter, Tracer]:- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")agent = Agent(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),system_prompt=system_prompt,tools=tools,before_tool_call=before_tool_call,@@ -383,7 +382,7 @@ class TestErrorAndAbort:async def test_agent_aborted_marks_cubepi_aborted(self):# Slow chunking so the abort signal lands MID-stream.provider = FauxProvider(tokens_per_second=10.0)- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -421,8 +420,8 @@ class TestErrorAndAbort:the provider to actually cooperate."""import asyncio as _asyncio- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -462,8 +461,8 @@ class TestErrorAndAbort:case; only a (None, None) shape coinciding with the agent'sabort signal counts as a real abort (codex P2 follow-up onPR #87)."""- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -809,8 +808,8 @@ class TestRequestMaxTokensCrossProvider:MINOR)."""async def test_max_output_tokens_lands_in_request_max_tokens(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -871,8 +870,8 @@ class TestDetachFlushGuarantee:def test_detach_outside_loop_returns_none(self):# Build a Tracer + Agent in sync context (no running loop).- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t", agent_name="a", exporters=[])detach = tracer.attach(agent)# No running loop → flush is the caller's responsibility.@@ -889,7 +888,7 @@ class TestCancellationExportsSpans:async def test_cancelled_run_still_exports_invoke_agent_span(self):provider = FauxProvider(tokens_per_second=10.0)- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])detach = tracer.attach(agent)@@ -1059,8 +1058,8 @@ class TestAttachedContextManager:pile up handlers."""async def test_basic_usage(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])provider.append_responses([faux_assistant_message("ok")])@@ -1077,7 +1076,7 @@ class TestAttachedContextManager:async def test_cancellation_inside_block_still_closes_spans(self):provider = FauxProvider(tokens_per_second=10.0)- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])provider.append_responses([faux_assistant_message("x" * 400)])@@ -1102,8 +1101,8 @@ class TestAttachedContextManager:assert attrs.get("cubepi.aborted") is Trueasync def test_exception_inside_block_still_detaches(self):- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t", agent_name="a", exporters=[])try:@@ -1123,8 +1122,8 @@ class TestAttachedContextManager:what ``await detach()`` would do manually. Otherwise userscontinue past the block thinking spans landed (codex P2 onPR #90)."""- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t", agent_name="a", exporters=[])provider.append_responses([faux_assistant_message("ok")])@@ -1151,8 +1150,8 @@ class TestAttachedContextManager:"""When the body raised, the flush failure must NOT mask theoriginal exception — the body's exception is the realproblem. Matches the standard contextlib pattern."""- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t", agent_name="a", exporters=[])async def _bad_flush(*_a, **_kw):@@ -1170,8 +1169,8 @@ class TestAttachedContextManager:"""The intended top-level idiom — Tracer + attached in one``async with`` line."""- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")provider.append_responses([faux_assistant_message("ok")])exporter = InMemoryExporter()@@ -1508,9 +1507,9 @@ class TestJsonlExporter:async def test_writes_jsonl_files(self, tmp_path):from cubepi.tracing.exporters import JsonlSpanExporter- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = JsonlSpanExporter(directory=tmp_path)tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)diff --git a/tests/tracing/test_recorder_coverage.py b/tests/tracing/test_recorder_coverage.pyindex b1a6c44..7c54c48 100644--- a/tests/tracing/test_recorder_coverage.py+++ b/tests/tracing/test_recorder_coverage.py@@ -21,7 +21,7 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class _Capture(SpanExporter):@@ -50,9 +50,9 @@ def _attrs(span: ReadableSpan) -> dict[str, Any]:class TestRunIdOnEverySpan:async def test_run_id_propagated_to_all_spans(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = _Capture()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -76,9 +76,9 @@ class TestJsonlSharding:JsonlSpanExporter routed them to unknown-run.jsonl."""from cubepi.tracing.exporters import JsonlSpanExporter- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("ok")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = JsonlSpanExporter(directory=tmp_path)tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -100,7 +100,7 @@ class TestJsonlSharding:class TestChatSpanAbort:async def test_chat_span_marks_aborted_on_cooperative_abort(self):provider = FauxProvider(tokens_per_second=10.0)- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = _Capture()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])tracer.attach(agent)@@ -346,9 +346,9 @@ class TestSchemaConstants:class TestDetach:async def test_detach_unsubscribes(self):- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("a")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")exporter = _Capture()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])detach = tracer.attach(agent)@@ -403,9 +403,9 @@ class TestTracerConfig:class TestStreamRecording:async def test_stream_file_created_and_closed(self, tmp_path):"""record_stream=True writes a .stream.jsonl file for the run."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hello world")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -426,7 +426,7 @@ class TestStreamRecording:"""Each text chunk produces a text_delta line in the stream file."""provider = FauxProvider(tokens_per_second=1000.0)provider.append_responses([faux_assistant_message("abc")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -456,7 +456,7 @@ class TestStreamRecording:"""provider = FauxProvider(tokens_per_second=5.0)provider.append_responses([faux_assistant_message("x" * 300)])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -494,7 +494,7 @@ class TestStreamRecording:return AgentToolResult(content=[TextContent(text="done")])tool = AgentTool(name="noop", description="d", parameters=P, execute=noop)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message(@@ -504,7 +504,7 @@ class TestStreamRecording:faux_assistant_message("all done"),])- agent = Agent(provider=provider, model=MODEL, system_prompt="s", tools=[tool])+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s", tools=[tool])tracer = Tracer(service_name="t",agent_name="a",@@ -534,9 +534,9 @@ class TestStreamRecording:async def test_no_stream_file_without_record_stream(self, tmp_path):"""Default (record_stream=False) must not create any stream file."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t", agent_name="a", exporters=[])tracer.attach(agent)@@ -548,9 +548,9 @@ class TestStreamRecording:async def test_stream_events_have_elapsed_time_field(self, tmp_path):"""Every recorded stream event must carry a numeric 't' (elapsed) field."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -576,11 +576,11 @@ class TestStreamRecording:async def test_stream_error_event_recorded(self, tmp_path):"""An error stop_reason causes FauxProvider to emit an 'error' StreamEvent,which _write_stream_event captures in the stream file."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("oops", stop_reason="error", error_message="boom")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -608,9 +608,9 @@ class TestStreamRecording:"""If the stream file write raises, _write_stream_event swallows it silently."""from unittest.mock import MagicMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -652,9 +652,9 @@ class TestStreamRecording:async def test_stream_open_exception_swallowed(self, tmp_path):"""If the stream file cannot be opened, the recorder continues without crashing."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")# Pass stream_dir as an existing file (not a directory) so mkdir fails.bad_dir = tmp_path / "not_a_dir.txt"bad_dir.write_text("i am a file")@@ -677,9 +677,9 @@ class TestStreamRecording:import cubepi.tracing.recorder as _rec_modfrom unittest.mock import MagicMock, patch- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.append_responses([faux_assistant_message("hi")])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",@@ -716,7 +716,7 @@ class TestStreamRecording:provider = FauxProvider(tokens_per_second=5.0)provider.append_responses([faux_assistant_message("x" * 300)])- agent = Agent(provider=provider, model=MODEL, system_prompt="s")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="s")tracer = Tracer(service_name="t",agent_name="a",diff --git a/tests/tracing/test_subagent_nesting.py b/tests/tracing/test_subagent_nesting.pyindex 4328300..9bc9ecc 100644--- a/tests/tracing/test_subagent_nesting.py+++ b/tests/tracing/test_subagent_nesting.py@@ -30,7 +30,7 @@ from cubepi.providers.faux import FauxProvider, faux_assistant_messagefrom cubepi.tracing import Tracer-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class InMemoryExporter(SpanExporter):@@ -54,15 +54,14 @@ class _Empty(BaseModel):def _make_agent(provider: FauxProvider, tools: list[AgentTool] | None) -> Agent:return Agent(- provider=provider,- model=MODEL,+ model=provider.model(MODEL.id),system_prompt="test prompt",tools=tools,)async def test_shared_provider_does_not_double_mint_inner_chat():- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])@@ -121,7 +120,7 @@ async def test_cancelled_inner_gate_released_for_parents_next_turn():proves both that the gate was released AND that no inner chat leaked ontothe parent run."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])@@ -195,7 +194,7 @@ async def test_inner_run_nests_under_active_tool_span():"""from cubepi.mcp import _tracing as mcp_tracing- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])@@ -238,7 +237,7 @@ async def test_full_nested_subtree_via_real_tool():execute_tool inner_tool}`` — the inner chat and inner tool span both descendfrom the inner run's root, and the inner root nests under the tool span."""- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")exporter = InMemoryExporter()tracer = Tracer(service_name="t", agent_name="a", exporters=[exporter])diff --git a/tests/tracing/test_trace_scope.py b/tests/tracing/test_trace_scope.pyindex 1da7132..bea86ec 100644--- a/tests/tracing/test_trace_scope.py+++ b/tests/tracing/test_trace_scope.py@@ -19,7 +19,7 @@ from cubepi.providers.base import Modelfrom cubepi.providers.faux import FauxProviderfrom cubepi.tracing import Tracer, trace-MODEL = Model(id="faux-1", provider="faux")+MODEL = Model(id="faux-1", provider_id="faux")class _NullExporter(SpanExporter):@@ -34,8 +34,8 @@ class _NullExporter(SpanExporter):def _build() -> tuple[Agent, FauxProvider, Tracer]:- provider = FauxProvider()- agent = Agent(provider=provider, model=MODEL, system_prompt="t")+ provider = FauxProvider(provider_id="faux")+ agent = Agent(model=provider.model(MODEL.id), system_prompt="t")tracer = Tracer(service_name="t",agent_name="t",diff --git a/uv.lock b/uv.lockindex 3d623eb..31e2250 100644--- a/uv.lock+++ b/uv.lock@@ -519,13 +519,12 @@ wheels = [[[package]]name = "cubepi"-version = "0.6.0"+version = "0.7.0"source = { editable = "." }dependencies = [{ name = "anthropic" },{ name = "openai" },{ name = "pydantic" },- { name = "pyyaml" },][package.optional-dependencies]@@ -590,7 +589,6 @@ requires-dist = [{ name = "opentelemetry-exporter-otlp-proto-http", marker = "extra == 'tracing-otlp'", specifier = ">=1.30" },{ name = "opentelemetry-sdk", marker = "extra == 'tracing'", specifier = ">=1.30" },{ name = "pydantic", specifier = ">=2.13.4" },- { name = "pyyaml", specifier = ">=6.0.3" },{ name = "rich", marker = "extra == 'trace-cli'", specifier = ">=13.0" },{ name = "sqlalchemy", marker = "extra == 'mysql'", specifier = ">=2.0" },{ name = "sqlalchemy", marker = "extra == 'postgres'", specifier = ">=2.0" },@@ -1545,61 +1543,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/c0/d2/21af5c535501a7233e734b8af901574572da66fcc254cb35d0609c9080dd/pywin32-311-cp314-cp314-win_arm64.whl", hash = "sha256:a508e2d9025764a8270f93111a970e1d0fbfc33f4153b388bb649b7eec4f9b42", size = 8932540, upload-time = "2025-07-14T20:13:36.379Z" },]-[[package]]-name = "pyyaml"-version = "6.0.3"-source = { registry = "https://pypi.org/simple/" }-sdist = { url = "https://files.pythonhosted.org/packages/05/8e/961c0007c59b8dd7729d542c61a4d537767a59645b82a0b521206e1e25c2/pyyaml-6.0.3.tar.gz", hash = "sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f", size = 130960, upload-time = "2025-09-25T21:33:16.546Z" }-wheels = [- { url = "https://files.pythonhosted.org/packages/6d/16/a95b6757765b7b031c9374925bb718d55e0a9ba8a1b6a12d25962ea44347/pyyaml-6.0.3-cp311-cp311-macosx_10_13_x86_64.whl", hash = "sha256:44edc647873928551a01e7a563d7452ccdebee747728c1080d881d68af7b997e", size = 185826, upload-time = "2025-09-25T21:31:58.655Z" },- { url = "https://files.pythonhosted.org/packages/16/19/13de8e4377ed53079ee996e1ab0a9c33ec2faf808a4647b7b4c0d46dd239/pyyaml-6.0.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:652cb6edd41e718550aad172851962662ff2681490a8a711af6a4d288dd96824", size = 175577, upload-time = "2025-09-25T21:32:00.088Z" },- { url = "https://files.pythonhosted.org/packages/0c/62/d2eb46264d4b157dae1275b573017abec435397aa59cbcdab6fc978a8af4/pyyaml-6.0.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:10892704fc220243f5305762e276552a0395f7beb4dbf9b14ec8fd43b57f126c", size = 775556, upload-time = "2025-09-25T21:32:01.31Z" },- { url = "https://files.pythonhosted.org/packages/10/cb/16c3f2cf3266edd25aaa00d6c4350381c8b012ed6f5276675b9eba8d9ff4/pyyaml-6.0.3-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:850774a7879607d3a6f50d36d04f00ee69e7fc816450e5f7e58d7f17f1ae5c00", size = 882114, upload-time = "2025-09-25T21:32:03.376Z" },- { url = "https://files.pythonhosted.org/packages/71/60/917329f640924b18ff085ab889a11c763e0b573da888e8404ff486657602/pyyaml-6.0.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b8bb0864c5a28024fac8a632c443c87c5aa6f215c0b126c449ae1a150412f31d", size = 806638, upload-time = "2025-09-25T21:32:04.553Z" },- { url = "https://files.pythonhosted.org/packages/dd/6f/529b0f316a9fd167281a6c3826b5583e6192dba792dd55e3203d3f8e655a/pyyaml-6.0.3-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:1d37d57ad971609cf3c53ba6a7e365e40660e3be0e5175fa9f2365a379d6095a", size = 767463, upload-time = "2025-09-25T21:32:06.152Z" },- { url = "https://files.pythonhosted.org/packages/f2/6a/b627b4e0c1dd03718543519ffb2f1deea4a1e6d42fbab8021936a4d22589/pyyaml-6.0.3-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:37503bfbfc9d2c40b344d06b2199cf0e96e97957ab1c1b546fd4f87e53e5d3e4", size = 794986, upload-time = "2025-09-25T21:32:07.367Z" },- { url = "https://files.pythonhosted.org/packages/45/91/47a6e1c42d9ee337c4839208f30d9f09caa9f720ec7582917b264defc875/pyyaml-6.0.3-cp311-cp311-win32.whl", hash = "sha256:8098f252adfa6c80ab48096053f512f2321f0b998f98150cea9bd23d83e1467b", size = 142543, upload-time = "2025-09-25T21:32:08.95Z" },- { url = "https://files.pythonhosted.org/packages/da/e3/ea007450a105ae919a72393cb06f122f288ef60bba2dc64b26e2646fa315/pyyaml-6.0.3-cp311-cp311-win_amd64.whl", hash = "sha256:9f3bfb4965eb874431221a3ff3fdcddc7e74e3b07799e0e84ca4a0f867d449bf", size = 158763, upload-time = "2025-09-25T21:32:09.96Z" },- { url = "https://files.pythonhosted.org/packages/d1/33/422b98d2195232ca1826284a76852ad5a86fe23e31b009c9886b2d0fb8b2/pyyaml-6.0.3-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:7f047e29dcae44602496db43be01ad42fc6f1cc0d8cd6c83d342306c32270196", size = 182063, upload-time = "2025-09-25T21:32:11.445Z" },- { url = "https://files.pythonhosted.org/packages/89/a0/6cf41a19a1f2f3feab0e9c0b74134aa2ce6849093d5517a0c550fe37a648/pyyaml-6.0.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:fc09d0aa354569bc501d4e787133afc08552722d3ab34836a80547331bb5d4a0", size = 173973, upload-time = "2025-09-25T21:32:12.492Z" },- { url = "https://files.pythonhosted.org/packages/ed/23/7a778b6bd0b9a8039df8b1b1d80e2e2ad78aa04171592c8a5c43a56a6af4/pyyaml-6.0.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9149cad251584d5fb4981be1ecde53a1ca46c891a79788c0df828d2f166bda28", size = 775116, upload-time = "2025-09-25T21:32:13.652Z" },- { url = "https://files.pythonhosted.org/packages/65/30/d7353c338e12baef4ecc1b09e877c1970bd3382789c159b4f89d6a70dc09/pyyaml-6.0.3-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5fdec68f91a0c6739b380c83b951e2c72ac0197ace422360e6d5a959d8d97b2c", size = 844011, upload-time = "2025-09-25T21:32:15.21Z" },- { url = "https://files.pythonhosted.org/packages/8b/9d/b3589d3877982d4f2329302ef98a8026e7f4443c765c46cfecc8858c6b4b/pyyaml-6.0.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ba1cc08a7ccde2d2ec775841541641e4548226580ab850948cbfda66a1befcdc", size = 807870, upload-time = "2025-09-25T21:32:16.431Z" },- { url = "https://files.pythonhosted.org/packages/05/c0/b3be26a015601b822b97d9149ff8cb5ead58c66f981e04fedf4e762f4bd4/pyyaml-6.0.3-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:8dc52c23056b9ddd46818a57b78404882310fb473d63f17b07d5c40421e47f8e", size = 761089, upload-time = "2025-09-25T21:32:17.56Z" },- { url = "https://files.pythonhosted.org/packages/be/8e/98435a21d1d4b46590d5459a22d88128103f8da4c2d4cb8f14f2a96504e1/pyyaml-6.0.3-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:41715c910c881bc081f1e8872880d3c650acf13dfa8214bad49ed4cede7c34ea", size = 790181, upload-time = "2025-09-25T21:32:18.834Z" },- { url = "https://files.pythonhosted.org/packages/74/93/7baea19427dcfbe1e5a372d81473250b379f04b1bd3c4c5ff825e2327202/pyyaml-6.0.3-cp312-cp312-win32.whl", hash = "sha256:96b533f0e99f6579b3d4d4995707cf36df9100d67e0c8303a0c55b27b5f99bc5", size = 137658, upload-time = "2025-09-25T21:32:20.209Z" },- { url = "https://files.pythonhosted.org/packages/86/bf/899e81e4cce32febab4fb42bb97dcdf66bc135272882d1987881a4b519e9/pyyaml-6.0.3-cp312-cp312-win_amd64.whl", hash = "sha256:5fcd34e47f6e0b794d17de1b4ff496c00986e1c83f7ab2fb8fcfe9616ff7477b", size = 154003, upload-time = "2025-09-25T21:32:21.167Z" },- { url = "https://files.pythonhosted.org/packages/1a/08/67bd04656199bbb51dbed1439b7f27601dfb576fb864099c7ef0c3e55531/pyyaml-6.0.3-cp312-cp312-win_arm64.whl", hash = "sha256:64386e5e707d03a7e172c0701abfb7e10f0fb753ee1d773128192742712a98fd", size = 140344, upload-time = "2025-09-25T21:32:22.617Z" },- { url = "https://files.pythonhosted.org/packages/d1/11/0fd08f8192109f7169db964b5707a2f1e8b745d4e239b784a5a1dd80d1db/pyyaml-6.0.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8da9669d359f02c0b91ccc01cac4a67f16afec0dac22c2ad09f46bee0697eba8", size = 181669, upload-time = "2025-09-25T21:32:23.673Z" },- { url = "https://files.pythonhosted.org/packages/b1/16/95309993f1d3748cd644e02e38b75d50cbc0d9561d21f390a76242ce073f/pyyaml-6.0.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:2283a07e2c21a2aa78d9c4442724ec1eb15f5e42a723b99cb3d822d48f5f7ad1", size = 173252, upload-time = "2025-09-25T21:32:25.149Z" },- { url = "https://files.pythonhosted.org/packages/50/31/b20f376d3f810b9b2371e72ef5adb33879b25edb7a6d072cb7ca0c486398/pyyaml-6.0.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ee2922902c45ae8ccada2c5b501ab86c36525b883eff4255313a253a3160861c", size = 767081, upload-time = "2025-09-25T21:32:26.575Z" },- { url = "https://files.pythonhosted.org/packages/49/1e/a55ca81e949270d5d4432fbbd19dfea5321eda7c41a849d443dc92fd1ff7/pyyaml-6.0.3-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a33284e20b78bd4a18c8c2282d549d10bc8408a2a7ff57653c0cf0b9be0afce5", size = 841159, upload-time = "2025-09-25T21:32:27.727Z" },- { url = "https://files.pythonhosted.org/packages/74/27/e5b8f34d02d9995b80abcef563ea1f8b56d20134d8f4e5e81733b1feceb2/pyyaml-6.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0f29edc409a6392443abf94b9cf89ce99889a1dd5376d94316ae5145dfedd5d6", size = 801626, upload-time = "2025-09-25T21:32:28.878Z" },- { url = "https://files.pythonhosted.org/packages/f9/11/ba845c23988798f40e52ba45f34849aa8a1f2d4af4b798588010792ebad6/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:f7057c9a337546edc7973c0d3ba84ddcdf0daa14533c2065749c9075001090e6", size = 753613, upload-time = "2025-09-25T21:32:30.178Z" },- { url = "https://files.pythonhosted.org/packages/3d/e0/7966e1a7bfc0a45bf0a7fb6b98ea03fc9b8d84fa7f2229e9659680b69ee3/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:eda16858a3cab07b80edaf74336ece1f986ba330fdb8ee0d6c0d68fe82bc96be", size = 794115, upload-time = "2025-09-25T21:32:31.353Z" },- { url = "https://files.pythonhosted.org/packages/de/94/980b50a6531b3019e45ddeada0626d45fa85cbe22300844a7983285bed3b/pyyaml-6.0.3-cp313-cp313-win32.whl", hash = "sha256:d0eae10f8159e8fdad514efdc92d74fd8d682c933a6dd088030f3834bc8e6b26", size = 137427, upload-time = "2025-09-25T21:32:32.58Z" },- { url = "https://files.pythonhosted.org/packages/97/c9/39d5b874e8b28845e4ec2202b5da735d0199dbe5b8fb85f91398814a9a46/pyyaml-6.0.3-cp313-cp313-win_amd64.whl", hash = "sha256:79005a0d97d5ddabfeeea4cf676af11e647e41d81c9a7722a193022accdb6b7c", size = 154090, upload-time = "2025-09-25T21:32:33.659Z" },- { url = "https://files.pythonhosted.org/packages/73/e8/2bdf3ca2090f68bb3d75b44da7bbc71843b19c9f2b9cb9b0f4ab7a5a4329/pyyaml-6.0.3-cp313-cp313-win_arm64.whl", hash = "sha256:5498cd1645aa724a7c71c8f378eb29ebe23da2fc0d7a08071d89469bf1d2defb", size = 140246, upload-time = "2025-09-25T21:32:34.663Z" },- { url = "https://files.pythonhosted.org/packages/9d/8c/f4bd7f6465179953d3ac9bc44ac1a8a3e6122cf8ada906b4f96c60172d43/pyyaml-6.0.3-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:8d1fab6bb153a416f9aeb4b8763bc0f22a5586065f86f7664fc23339fc1c1fac", size = 181814, upload-time = "2025-09-25T21:32:35.712Z" },- { url = "https://files.pythonhosted.org/packages/bd/9c/4d95bb87eb2063d20db7b60faa3840c1b18025517ae857371c4dd55a6b3a/pyyaml-6.0.3-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:34d5fcd24b8445fadc33f9cf348c1047101756fd760b4dacb5c3e99755703310", size = 173809, upload-time = "2025-09-25T21:32:36.789Z" },- { url = "https://files.pythonhosted.org/packages/92/b5/47e807c2623074914e29dabd16cbbdd4bf5e9b2db9f8090fa64411fc5382/pyyaml-6.0.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:501a031947e3a9025ed4405a168e6ef5ae3126c59f90ce0cd6f2bfc477be31b7", size = 766454, upload-time = "2025-09-25T21:32:37.966Z" },- { url = "https://files.pythonhosted.org/packages/02/9e/e5e9b168be58564121efb3de6859c452fccde0ab093d8438905899a3a483/pyyaml-6.0.3-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:b3bc83488de33889877a0f2543ade9f70c67d66d9ebb4ac959502e12de895788", size = 836355, upload-time = "2025-09-25T21:32:39.178Z" },- { url = "https://files.pythonhosted.org/packages/88/f9/16491d7ed2a919954993e48aa941b200f38040928474c9e85ea9e64222c3/pyyaml-6.0.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c458b6d084f9b935061bc36216e8a69a7e293a2f1e68bf956dcd9e6cbcd143f5", size = 794175, upload-time = "2025-09-25T21:32:40.865Z" },- { url = "https://files.pythonhosted.org/packages/dd/3f/5989debef34dc6397317802b527dbbafb2b4760878a53d4166579111411e/pyyaml-6.0.3-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7c6610def4f163542a622a73fb39f534f8c101d690126992300bf3207eab9764", size = 755228, upload-time = "2025-09-25T21:32:42.084Z" },- { url = "https://files.pythonhosted.org/packages/d7/ce/af88a49043cd2e265be63d083fc75b27b6ed062f5f9fd6cdc223ad62f03e/pyyaml-6.0.3-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5190d403f121660ce8d1d2c1bb2ef1bd05b5f68533fc5c2ea899bd15f4399b35", size = 789194, upload-time = "2025-09-25T21:32:43.362Z" },- { url = "https://files.pythonhosted.org/packages/23/20/bb6982b26a40bb43951265ba29d4c246ef0ff59c9fdcdf0ed04e0687de4d/pyyaml-6.0.3-cp314-cp314-win_amd64.whl", hash = "sha256:4a2e8cebe2ff6ab7d1050ecd59c25d4c8bd7e6f400f5f82b96557ac0abafd0ac", size = 156429, upload-time = "2025-09-25T21:32:57.844Z" },- { url = "https://files.pythonhosted.org/packages/f4/f4/a4541072bb9422c8a883ab55255f918fa378ecf083f5b85e87fc2b4eda1b/pyyaml-6.0.3-cp314-cp314-win_arm64.whl", hash = "sha256:93dda82c9c22deb0a405ea4dc5f2d0cda384168e466364dec6255b293923b2f3", size = 143912, upload-time = "2025-09-25T21:32:59.247Z" },- { url = "https://files.pythonhosted.org/packages/7c/f9/07dd09ae774e4616edf6cda684ee78f97777bdd15847253637a6f052a62f/pyyaml-6.0.3-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:02893d100e99e03eda1c8fd5c441d8c60103fd175728e23e431db1b589cf5ab3", size = 189108, upload-time = "2025-09-25T21:32:44.377Z" },- { url = "https://files.pythonhosted.org/packages/4e/78/8d08c9fb7ce09ad8c38ad533c1191cf27f7ae1effe5bb9400a46d9437fcf/pyyaml-6.0.3-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:c1ff362665ae507275af2853520967820d9124984e0f7466736aea23d8611fba", size = 183641, upload-time = "2025-09-25T21:32:45.407Z" },- { url = "https://files.pythonhosted.org/packages/7b/5b/3babb19104a46945cf816d047db2788bcaf8c94527a805610b0289a01c6b/pyyaml-6.0.3-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6adc77889b628398debc7b65c073bcb99c4a0237b248cacaf3fe8a557563ef6c", size = 831901, upload-time = "2025-09-25T21:32:48.83Z" },- { url = "https://files.pythonhosted.org/packages/8b/cc/dff0684d8dc44da4d22a13f35f073d558c268780ce3c6ba1b87055bb0b87/pyyaml-6.0.3-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a80cb027f6b349846a3bf6d73b5e95e782175e52f22108cfa17876aaeff93702", size = 861132, upload-time = "2025-09-25T21:32:50.149Z" },- { url = "https://files.pythonhosted.org/packages/b1/5e/f77dc6b9036943e285ba76b49e118d9ea929885becb0a29ba8a7c75e29fe/pyyaml-6.0.3-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:00c4bdeba853cc34e7dd471f16b4114f4162dc03e6b7afcc2128711f0eca823c", size = 839261, upload-time = "2025-09-25T21:32:51.808Z" },- { url = "https://files.pythonhosted.org/packages/ce/88/a9db1376aa2a228197c58b37302f284b5617f56a5d959fd1763fb1675ce6/pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:66e1674c3ef6f541c35191caae2d429b967b99e02040f5ba928632d9a7f0f065", size = 805272, upload-time = "2025-09-25T21:32:52.941Z" },- { url = "https://files.pythonhosted.org/packages/da/92/1446574745d74df0c92e6aa4a7b0b3130706a4142b2d1a5869f2eaa423c6/pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:16249ee61e95f858e83976573de0f5b2893b3677ba71c9dd36b9cf8be9ac6d65", size = 829923, upload-time = "2025-09-25T21:32:54.537Z" },- { url = "https://files.pythonhosted.org/packages/f0/7a/1c7270340330e575b92f397352af856a8c06f230aa3e76f86b39d01b416a/pyyaml-6.0.3-cp314-cp314t-win_amd64.whl", hash = "sha256:4ad1906908f2f5ae4e5a8ddfce73c320c2a1429ec52eafd27138b7f1cbe341c9", size = 174062, upload-time = "2025-09-25T21:32:55.767Z" },- { url = "https://files.pythonhosted.org/packages/f1/12/de94a39c2ef588c7e6455cfbe7343d3b2dc9d6b6b2f40c4c6565744c873d/pyyaml-6.0.3-cp314-cp314t-win_arm64.whl", hash = "sha256:ebc55a14a21cb14062aa4162f906cd962b28e2e9ea38f9b4391244cd8de4ae0b", size = 149341, upload-time = "2025-09-25T21:32:56.828Z" },-]-[[package]]name = "referencing"version = "0.37.0"diff --git a/website/docs/getting-started/core-concepts.md b/website/docs/getting-started/core-concepts.mdindex 3d639b9..0f17f99 100644--- a/website/docs/getting-started/core-concepts.md+++ b/website/docs/getting-started/core-concepts.md@@ -181,7 +181,7 @@ from cubepi.hitl import InMemoryChannel, ConfirmToolCallMiddleware, ask_user_toochannel = InMemoryChannel()agent = Agent(- provider=…, model=…,+ model=…,tools=[bash_tool, ask_user_tool(channel)],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],channel=channel,diff --git a/website/docs/getting-started/installation.md b/website/docs/getting-started/installation.mdindex 7e2c560..d7ee8dc 100644--- a/website/docs/getting-started/installation.md+++ b/website/docs/getting-started/installation.md@@ -67,8 +67,8 @@ import osfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.openai import OpenAIProvider-anthropic = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])-openai = OpenAIProvider(api_key=os.environ["OPENAI_API_KEY"])+anthropic = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+openai = OpenAIProvider(provider_id="openai", api_key=os.environ["OPENAI_API_KEY"])```You can also pass `base_url=...` to either provider to point at adiff --git a/website/docs/getting-started/quick-start.md b/website/docs/getting-started/quick-start.mdindex aa98eb9..9994ca4 100644--- a/website/docs/getting-started/quick-start.md+++ b/website/docs/getting-started/quick-start.md@@ -40,11 +40,10 @@ async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, onasync def main():- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a concise weather assistant.",tools=[AgentTool(@@ -104,9 +103,7 @@ runtime emits: `agent_start`, `turn_start`, `message_start`,- **Subscribe before you prompt.** Listeners only receive eventsemitted *after* `subscribe` was called. Calling `prompt` first meansthe early events are gone.-- **`Model(id=…, provider=…)` is required.** The agent uses- `model.provider` to clamp thinking levels and route metadata. The id- must match a model the provider supports.+- **`provider.model(...)` binds the model to its provider.** The provider holds credentials and optional `provider_id` metadata; the model id must match a model that provider supports.- **The `execute` signature is fixed.** Keep `(tool_call_id, params,*, signal, on_update)` even if you don't use the keyword-onlyarguments. CubePi always passes them.diff --git a/website/docs/guides/agents/first-agent.md b/website/docs/guides/agents/first-agent.mdindex 4bf1df0..c0c9bc0 100644--- a/website/docs/guides/agents/first-agent.md+++ b/website/docs/guides/agents/first-agent.md@@ -19,7 +19,7 @@ import osfrom cubepi import Modelfrom cubepi.providers.anthropic import AnthropicProvider-provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])model = Model(id="claude-sonnet-4-5-20250929",provider="anthropic",@@ -81,7 +81,6 @@ A few details:from cubepi import Agentagent = Agent(- provider=provider,model=model,system_prompt="You are a concise weather assistant.",tools=[weather_tool],diff --git a/website/docs/guides/agents/multi-turn.md b/website/docs/guides/agents/multi-turn.mdindex 4a525f0..fc21806 100644--- a/website/docs/guides/agents/multi-turn.md+++ b/website/docs/guides/agents/multi-turn.md@@ -82,7 +82,7 @@ returns, to drain the queue.```pythonasync with SQLiteCheckpointer("conv.db") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id="conv-1")+ agent = Agent(model=…, checkpointer=cp, thread_id="conv-1")await agent.prompt("hello") # loads existing history firstawait agent.prompt("how are you?")```@@ -125,7 +125,6 @@ from cubepi.checkpointer import SQLiteCheckpointerasync with SQLiteCheckpointer("conv.db") as cp:agent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id="user-42",diff --git a/website/docs/guides/agents/tool-use.md b/website/docs/guides/agents/tool-use.mdindex 6a335eb..01f80c8 100644--- a/website/docs/guides/agents/tool-use.md+++ b/website/docs/guides/agents/tool-use.md@@ -51,7 +51,6 @@ That's almost always what you want.```pythonagent = Agent(- provider=provider,model=model,tools=[search_tool, fetch_url_tool, summarise_tool],)diff --git a/website/docs/guides/checkpointing/custom.md b/website/docs/guides/checkpointing/custom.mdindex e36e0b5..7e8e0a8 100644--- a/website/docs/guides/checkpointing/custom.md+++ b/website/docs/guides/checkpointing/custom.md@@ -104,7 +104,7 @@ Usage:```pythonasync with RedisCheckpointer("redis://localhost:6379") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id="user-42")+ agent = Agent(model=…, checkpointer=cp, thread_id="user-42")await agent.prompt("hi")```@@ -146,7 +146,7 @@ class FileCheckpointer:...# Pass directly:-agent = Agent(provider=…, model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")+agent = Agent(model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")```## Testing your backend@@ -159,18 +159,18 @@ from cubepi.providers import FauxProvider, faux_assistant_messageasync def test_roundtrip():cp = MyCheckpointer(…)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("hello")])- agent1 = Agent(provider=provider, model=Model(id="t", provider="faux"),+ agent1 = Agent(model=provider.model("t"),checkpointer=cp, thread_id="t1")agent1.subscribe(lambda e, s=None: None)await agent1.prompt("hi")# Fresh agent, same thread — should restore history.- provider2 = FauxProvider()+ provider2 = FauxProvider(provider_id="faux")provider2.set_responses([faux_assistant_message("hello again")])- agent2 = Agent(provider=provider2, model=Model(id="t", provider="faux"),+ agent2 = Agent(model=provider2.model("t"),checkpointer=cp, thread_id="t1")agent2.subscribe(lambda e, s=None: None)await agent2.prompt("hi again")diff --git a/website/docs/guides/checkpointing/mysql.md b/website/docs/guides/checkpointing/mysql.mdindex a35a103..fdb82ae 100644--- a/website/docs/guides/checkpointing/mysql.md+++ b/website/docs/guides/checkpointing/mysql.md@@ -34,11 +34,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with MySQLCheckpointer("mysql://user:pass@host:3306/dbname") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)diff --git a/website/docs/guides/checkpointing/postgres.md b/website/docs/guides/checkpointing/postgres.mdindex ae346a6..05b9495 100644--- a/website/docs/guides/checkpointing/postgres.md+++ b/website/docs/guides/checkpointing/postgres.md@@ -28,11 +28,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with PostgresCheckpointer("postgresql://user:pass@host/dbname") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)diff --git a/website/docs/guides/checkpointing/sqlite.md b/website/docs/guides/checkpointing/sqlite.mdindex b687d2a..1b65c5f 100644--- a/website/docs/guides/checkpointing/sqlite.md+++ b/website/docs/guides/checkpointing/sqlite.md@@ -28,11 +28,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with SQLiteCheckpointer("agent.db") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)@@ -116,8 +115,8 @@ Process A's in-memory state will diverge from Process B's writes.```pythonasync with SQLiteCheckpointer("agent.db") as cp:- alice = Agent(provider=…, model=…, checkpointer=cp, thread_id="alice")- bob = Agent(provider=…, model=…, checkpointer=cp, thread_id="bob")+ alice = Agent(model=…, checkpointer=cp, thread_id="alice")+ bob = Agent(model=…, checkpointer=cp, thread_id="bob")# Each call only loads/appends its own thread.```diff --git a/website/docs/guides/hitl/durable.md b/website/docs/guides/hitl/durable.mdindex 47da37a..328c950 100644--- a/website/docs/guides/hitl/durable.md+++ b/website/docs/guides/hitl/durable.md@@ -24,7 +24,7 @@ async with PostgresCheckpointer("postgresql://...") as cp:channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")agent = Agent(- provider=..., model=...,+ model=...,tools=[bash_tool],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"}),@@ -63,7 +63,7 @@ async with PostgresCheckpointer("postgresql://...") as cp:channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")agent = Agent(- provider=..., model=...,+ model=...,tools=[bash_tool],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"}),diff --git a/website/docs/guides/mcp/auth.md b/website/docs/guides/mcp/auth.mdindex d40ecd5..b67f8e5 100644--- a/website/docs/guides/mcp/auth.md+++ b/website/docs/guides/mcp/auth.md@@ -168,7 +168,7 @@ async def build_agent_for_user(user_id: str) -> Agent:server_url="https://mcp.example.com/sse",headers={"Authorization": f"Bearer {token}"},)- return Agent(provider=provider, model=model, tools=tools)+ return Agent(model=model, tools=tools)```Don't cache a single `tools` list across users — the closures retaindiff --git a/website/docs/guides/mcp/loading.md b/website/docs/guides/mcp/loading.mdindex 93e1646..1c0ccd3 100644--- a/website/docs/guides/mcp/loading.md+++ b/website/docs/guides/mcp/loading.md@@ -41,8 +41,7 @@ async def main():)agent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),tools=tools, # all server tools, ready to use)agent.subscribe(lambda e, s=None: None)@@ -112,7 +111,6 @@ mcp_tools = await load_mcp_tools_stdio(command="…", args=[…])my_tools = [weather_tool, search_tool]agent = Agent(- provider=provider,model=model,tools=my_tools + mcp_tools,)diff --git a/website/docs/guides/middleware/compaction.md b/website/docs/guides/middleware/compaction.mdnew file mode 100644index 0000000..5c96073--- /dev/null+++ b/website/docs/guides/middleware/compaction.md@@ -0,0 +1,83 @@+---+title: Compaction+description: "Use CompactionMiddleware to summarize older turns while preserving full CubePi history."+---++# Compaction++`CompactionMiddleware` keeps long conversations inside a model's context window+without deleting agent history. It summarizes older turns into `ctx.extra`, then+sends the model a compressed view: one summary message plus the most recent+messages. `agent.state.messages` and checkpointer history stay complete.++## Basic setup++Use a cheaper model for the summary pass and your normal model for the agent:++```python+from cubepi import Agent+from cubepi.middleware import CompactionMiddleware++model = main_provider.model("claude-sonnet-4-5-20250929")+summary_model = cheap_provider.model("claude-haiku-4-5")++agent = Agent(+ model=model,+ checkpointer=checkpointer,+ thread_id="conv_123",+ middleware=[+ CompactionMiddleware(+ summary_model=summary_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+ ),+ ],+)+```++The summary call uses `Provider.generate(...)` with `temperature=0.0`,+`thinking="off"`, and `max_output_tokens=max_summary_tokens`.++## What gets persisted++The middleware writes two keys into `AgentContext.extra`:++- `compaction` — the summary state and the message refs it covers.+- `compaction_until_msg_index` — the history boundary summarized so far.++When a checkpointer is attached, CubePi saves `ctx.extra` at `agent_end`, so the+next process can resume with the existing summary. If the message refs no longer+match the current history, the middleware clears the stale state and starts over+rather than sending an invalid summary.++## Choosing thresholds++Start with conservative values:++```python+CompactionMiddleware(+ summary_model=cheap_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+)+```++Raise `max_tokens_before_compact` if your model has a large context window and+you want fewer summary calls. Raise `keep_recent_messages` when recent tool+outputs or user corrections are especially important. Increase+`max_summary_tokens` for long-running research or coding sessions where the+summary needs more detail.++## Failure behavior++If the summary provider fails, CubePi logs a warning and continues with the+previous compressed view or the original messages. The agent run is not failed+only because compaction could not refresh.++## When not to use it++Skip compaction for short tasks, stateless agents, or workflows where every+token of old tool output must be visible to the model. In those cases a simple+sliding-window `transform_context` hook can be easier to reason about.diff --git a/website/docs/guides/middleware/composition.md b/website/docs/guides/middleware/composition.mdindex 8d9770c..4b669b4 100644--- a/website/docs/guides/middleware/composition.md+++ b/website/docs/guides/middleware/composition.md@@ -167,7 +167,6 @@ overrides. You don't need to `pass`-implement every method.```pythonclass JustTransform(Middleware):async def transform_context(self, messages, *, ctx, signal=None):- del ctx, signalreturn messages[-10:]# No other hooks. CubePi won't call them.```diff --git a/website/docs/guides/middleware/examples.md b/website/docs/guides/middleware/examples.mdindex 120a31d..f8391f6 100644--- a/website/docs/guides/middleware/examples.md+++ b/website/docs/guides/middleware/examples.md@@ -41,7 +41,7 @@ class RateLimitMiddleware(Middleware):Use:```python-agent = Agent(provider=…, model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])+agent = Agent(model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])```When the limit hits, the model sees a tool result that says "Rate@@ -153,7 +153,6 @@ class SlidingWindow(Middleware):self.max_messages = max_messagesasync def transform_context(self, messages, *, ctx, signal=None):- del ctx, signalif len(messages) <= self.max_messages:return messagesreturn messages[-self.max_messages:]@@ -168,7 +167,6 @@ what was dropped:```pythonclass SummaryInjector(Middleware):async def transform_system_prompt(self, system_prompt, *, ctx, signal=None):- del ctx, signalsummary = "Earlier in this conversation we discussed: …"return f"{system_prompt}\n\nContext: {summary}".strip()```@@ -181,15 +179,15 @@ messages. The full conversation history remains in `agent.state`.```pythonfrom cubepi.middleware import CompactionMiddleware-from cubepi.providers import Model++main_model = main_provider.model("claude-sonnet-4-5-20250929")+summary_model = cheap_provider.model("claude-haiku-4-5")agent = Agent(- provider=main_provider,model=main_model,middleware=[CompactionMiddleware(- summary_provider=cheap_provider,- summary_model=Model(id="claude-haiku-4-5", provider="anthropic"),+ summary_model=summary_model,max_tokens_before_compact=80_000,keep_recent_messages=8,),@@ -220,12 +218,10 @@ subagents = {}agent = Agent(- provider=provider,model=model,middleware=[SubagentMiddleware(subagents=subagents,- default_provider=provider,default_model=model,shared_tools=[web_search],),@@ -315,7 +311,7 @@ from cubepi.hitl import ConfirmToolCallMiddleware, InMemoryChannelchannel = InMemoryChannel()agent = Agent(- provider=…, model=…,+ model=…,middleware=[ConfirmToolCallMiddleware(channel,@@ -344,7 +340,7 @@ def my_policy(ctx):return AskUser(timeout_seconds=180)agent = Agent(- provider=…, model=…,+ model=…,middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],)```diff --git a/website/docs/guides/middleware/hooks.md b/website/docs/guides/middleware/hooks.mdindex 28a8bb3..125840b 100644--- a/website/docs/guides/middleware/hooks.md+++ b/website/docs/guides/middleware/hooks.md@@ -252,7 +252,7 @@ class MaxTurnsMiddleware(Middleware):return self.turns >= self.max_turns-agent = Agent(provider=…, model=…, middleware=[MaxTurnsMiddleware(5)])+agent = Agent(model=…, middleware=[MaxTurnsMiddleware(5)])```## See alsodiff --git a/website/docs/guides/middleware/subagents.md b/website/docs/guides/middleware/subagents.mdnew file mode 100644index 0000000..2ce3303--- /dev/null+++ b/website/docs/guides/middleware/subagents.md@@ -0,0 +1,101 @@+---+title: Subagents+description: "Use SubagentMiddleware to delegate self-contained work to child CubePi agents."+---++# Subagents++`SubagentMiddleware` adds a `subagent` tool. When the model calls that tool,+CubePi creates an ephemeral child `Agent`, runs a self-contained prompt, and+returns the child agent's final assistant text as the tool result.++Use it when a parent agent needs to delegate bounded work such as research,+review, extraction, or a focused implementation pass.++## Define subagent specs++```python+from cubepi import Agent+from cubepi.middleware import SubagentMiddleware, SubagentSpec++subagents = {+ "researcher": SubagentSpec(+ name="researcher",+ description="Researches a narrow question and returns concise notes.",+ system_prompt="You are a precise research assistant.",+ ),+ "reviewer": SubagentSpec(+ name="reviewer",+ description="Reviews code for bugs, regressions, and missing tests.",+ system_prompt="You are a rigorous senior code reviewer.",+ ),+}++model = provider.model("claude-sonnet-4-5-20250929")++agent = Agent(+ model=model,+ middleware=[+ SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ shared_tools=[web_search],+ ),+ ],+)+```++If the model requests an unknown `subagent_type`, CubePi falls back to the+`general-purpose` subagent. If you do not define one, the middleware supplies a+basic default.++## Tool access and middleware inheritance++Subagents only receive the tools you pass in:++- `shared_tools` are available to every child agent.+- `SubagentSpec.tools` are available only to that subagent type.+- `excluded_tool_names` prevents recursive or host-only tools from being shared.++Use `inherited_middleware` for middleware every child should run. Use+`SubagentSpec.middleware` for behavior specific to one subagent type.++## Stream child events to your host++Applications often need child events in their own UI or audit log. Provide an+`event_mapper` and optional `event_handler`:++```python+def map_event(event):+ if event.type == "text_delta":+ return {"type": "subagent_text_delta", "delta": event.delta}+ return None++async def handle_event(agent_id, payload):+ await ui_stream.send({"agent_id": agent_id, **payload})++SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ event_mapper=map_event,+ event_handler=handle_event,+)+```++Mapped payloads are also stored in the parent tool result under+`details["subagent_events"]`.++## Tracing and aborts++Pass a `Tracer` via `tracer=...` to attach tracing to each child run. Nested+subagent spans share the parent trace, so `cubepi trace view <trace_id>` renders+the parent tool call and child run together.++The parent run's abort signal is forwarded to the child agent. If the parent is+aborted while a subagent is running, the child agent is aborted as well.++## Prompting guidelines++The parent model should send a self-contained `prompt` in the `subagent` tool+call. Do not rely on the child seeing the parent's full hidden context. Include+the goal, relevant facts, constraints, and desired output shape in the prompt.diff --git a/website/docs/guides/providers/anthropic.md b/website/docs/guides/providers/anthropic.mdindex 0da6971..086d8f6 100644--- a/website/docs/guides/providers/anthropic.md+++ b/website/docs/guides/providers/anthropic.md@@ -27,11 +27,9 @@ the environment.## The `Model````python-from cubepi import Model--model = Model(- id="claude-sonnet-4-5-20250929",- provider="anthropic",+provider = AnthropicProvider(provider_id="anthropic")+model = provider.model(+ "claude-sonnet-4-5-20250929",reasoning=True, # enables thinking levels (see below)max_tokens=8192, # response capcontext_window=200_000, # hard model limit@@ -39,10 +37,10 @@ model = Model()```-The `id` is the model name exactly as you'd pass it to the SDK. The-`provider` string is a free-form label used by CubePi internals — keep-it stable across your codebase but it doesn't have to match-`"anthropic"` literally.+The first argument is the model name exactly as you'd pass it to the+SDK. `provider_id` is a free-form label used by CubePi internals — keep+it stable across your codebase, and set it when you want tracing and+error messages to show a specific source label.## Extended thinking@@ -60,11 +58,11 @@ CubePi maps a `ThinkingLevel` enum onto Anthropic's `budget_tokens`:Set it per-agent:```python-agent = Agent(provider=provider, model=model, thinking="medium")+agent = Agent(model=model, thinking="medium")```To change the per-level budgets, supply a-[`CapabilityDescriptor`](./capability-and-presets) with a+[`CapabilityDescriptor`](./overview) with a`reasoning_level` of `kind="int_budget"` at construction — its`level_budgets` map is the single source of truth for budget values.@@ -117,8 +115,8 @@ Cache retention is `"short"` (5 minutes, free). Bump to `"long"` ifyour turns are slower than that:```python-AnthropicProvider(api_key=…, cache_retention="long") # 1-hour TTL-AnthropicProvider(api_key=…, cache_retention="none") # disable entirely+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="long") # 1-hour TTL+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="none") # disable entirely```The `Usage` object on each `AssistantMessage` reports@@ -139,7 +137,7 @@ async def my_payload(payload, model):payload.setdefault("metadata", {})["user_id"] = "u-42"return payload # return None or no return to keep the original-agent = Agent(provider=provider, model=model, on_payload=my_payload)+agent = Agent(model=model, on_payload=my_payload)```Use this for: adding `metadata.user_id` (for billing), forcing@@ -159,7 +157,7 @@ async def my_response(resp, model):if rate is not None:metrics.gauge("rate_remaining", int(rate))-agent = Agent(provider=provider, model=model, on_response=my_response)+agent = Agent(model=model, on_response=my_response)```Both callbacks may be sync or async.@@ -196,7 +194,7 @@ and inject it via a [custom provider](./custom).## See also- [OpenAI Provider](./openai) — same protocol, different shape.-- [Capabilities & Preset Catalog](./capability-and-presets) — tune+- [Providers Overview](./overview) — tunereasoning budgets and temperature handling as data.- [Custom Provider](./custom) — wrap a non-built-in API.- [Recipes → Multi-Provider Failover](../../recipes/multi-provider-failover)diff --git a/website/docs/guides/providers/custom.md b/website/docs/guides/providers/custom.mdindex 7047f4f..278dbec 100644--- a/website/docs/guides/providers/custom.md+++ b/website/docs/guides/providers/custom.md@@ -79,7 +79,7 @@ class MyProvider(BaseProvider):content=[TextContent(text="")],usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)ms.push(StreamEvent(type="start", partial=partial.model_copy(deep=True)))@@ -186,7 +186,7 @@ from cubepi.providers import FauxProvider, faux_assistant_message, faux_text, fadef test_my_agent():- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message([faux_tool_call("search", {"query": "python"}),@@ -195,8 +195,7 @@ def test_my_agent():])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),tools=[my_search_tool],)events = []@@ -233,8 +232,9 @@ Helpers:iteration creates very hard-to-debug aliasing.## See also+- [Image Generation](./image-generation) — generate images with `openai-images` and image editing.-- [Capabilities & Preset Catalog](./capability-and-presets) — before+- [Providers Overview](./overview) — beforewriting a class from scratch, check whether your backend is just anOpenAI/Anthropic-compatible endpoint a `CapabilityDescriptor` orbundled preset already covers.diff --git a/website/docs/guides/providers/image-generation.md b/website/docs/guides/providers/image-generation.mdnew file mode 100644index 0000000..687d1a1--- /dev/null+++ b/website/docs/guides/providers/image-generation.md@@ -0,0 +1,141 @@+---+title: Image Generation+description: "Generate images with CubePi image providers."+---++# Image Generation++This page documents CubePi's separate image generation path. It is intentionally+independent from `Agent`: chat models use the `Model` path, while image models+use `ImagesModel` and run through an image provider instance directly.++## `ImagesModel` types++Image generation uses three small types from `cubepi.providers.images.types`:++```python+from cubepi.providers.images.types import AssistantImages, ImagesContext, ImagesModel+```++- `ImagesModel`: minimal model binding for image APIs (`id`, `provider`, `api`).+- `ImagesContext`: request body for generation (`prompt`, optional `input_images`).+- `AssistantImages`: provider response with `output`, `stop_reason`, and+ optional `error_message`.++`ImageContent` (from `cubepi.providers.base`) is used in `input_images` and+`AssistantImages.output`.++## Create image models++For OpenAI image models, use `create_images_provider("openai-images", ...)` and+an `ImagesModel`:++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++image_provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")+```++`api` is an identifier used by downstream systems for routing and reporting.++## Generate text-to-image++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")++resp = await provider.generate_images(+ model=model,+ context=ImagesContext(prompt="A cute robot at sunrise"),+ options={"size": "1024x1024", "quality": "high", "n": 2},+)++if resp.stop_reason == "error":+ raise RuntimeError(resp.error_message or "image generation failed")++for block in resp.output:+ print(block.type, block.source[:30], block.media_type)+```++If `options` is omitted, the provider sends defaults (`n=1`) and no optional+image-only params.++## Edit with input images++Passing `input_images` switches to the edit path automatically:++```python+import base64+from cubepi.providers.base import ImageContent+from cubepi.providers.images.types import ImagesContext++with open("source.png", "rb") as fh:+ source_b64 = base64.b64encode(fh.read()).decode("ascii")++ctx = ImagesContext(+ prompt="Make it brighter and warmer.",+ input_images=[ImageContent(source=source_b64, media_type="image/png")],+)+```++`generate_images(model, context=ctx)` calls `edit` instead of `generate`.++## `options` forwarding++`generate_images(..., options)` is a free-form dict passed straight into the+backend call (after adding the required `model`, `prompt`, and default `n`).+The OpenAI image provider currently supports common keys like:++- `size`+- `quality`+- `n`+- `output_format` (`png`, `jpeg`, `webp`)+- `background`+- any backend extension fields accepted by your gateway++The provider only filters output media type for `output_format`:+`png` -> `image/png`, `jpeg` -> `image/jpeg`, `webp` -> `image/webp`.++## Error handling++On backend failures, `generate_images(...)` returns `AssistantImages` with:++- `stop_reason="error"`+- `output=[]`+- `error_message=<provider error>`++This keeps the API failure-in-band and lets callers render a single+success/error branch.++## Testing and stubs++For local tests, `FauxImagesProvider` provides a deterministic image output:++```python+from cubepi.providers.images.faux import FauxImagesProvider++provider = FauxImagesProvider(png_b64="iVBORw0KGgoAAAANSUh...")+```++It is useful for end-to-end UI tests where you only need a stable `ImageContent`.++## See also++- [Providers Overview](./overview) — when model-level wiring differs from defaults.+- [OpenAI Provider](./openai) — shared OpenAI config patterns.+- [API Reference → Image types in providers](../../api/cubepi-providers).diff --git a/website/docs/guides/providers/openai.md b/website/docs/guides/providers/openai.mdindex 8c764cc..71661f3 100644--- a/website/docs/guides/providers/openai.md+++ b/website/docs/guides/providers/openai.md@@ -20,19 +20,18 @@ Both implement the same `Provider` protocol; pick one per agent.## Chat Completions: `OpenAIProvider````python-from cubepi import Modelfrom cubepi.providers.openai import OpenAIProviderprovider = OpenAIProvider(+ provider_id="openai",api_key="sk-…", # or reads OPENAI_API_KEYbase_url=None, # set for OpenAI-compatible serversextra_body=None, # merged into every requestextra_headers=None,)-model = Model(- id="gpt-5",- provider="openai",+model = provider.model(+ "gpt-5",reasoning=True, # enables thinking level mappingmax_tokens=8192,context_window=128_000,@@ -78,9 +77,8 @@ Wire-shape differences between OpenAI and OpenAI-compatible backendstemperature handling) are configured through a[`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html)passed at construction. For example, `max_tokens_field="max_completion_tokens"`-renames the key on the way out. See-[Capability Descriptors](./capability-and-presets) for the full set of-knobs (CubePi `0.5+`).+renames the key on the way out. See [Providers Overview](./overview)+for the full set of knobs (CubePi `0.5+`).### Pointing at vLLM / LiteLLM / DeepSeek@@ -102,15 +100,15 @@ provider = OpenAIProvider(```For the wire quirks of these backends (reasoning toggles, token-field-renames, …), see [Capability Descriptors](./capability-and-presets).+renames, …), see [Providers Overview](./overview).## Responses API: `OpenAIResponsesProvider````pythonfrom cubepi.providers.openai_responses import OpenAIResponsesProvider-provider = OpenAIResponsesProvider(api_key="sk-…")-model = Model(id="gpt-5", provider="openai_responses", reasoning=True)+provider = OpenAIResponsesProvider(provider_id="openai_responses", api_key="sk-…")+model = provider.model("gpt-5", reasoning=True)```The Responses API keeps state server-side (referenced by@@ -139,7 +137,7 @@ async def add_user_metadata(payload, model):payload["user"] = "u-42" # billable user attributionreturn payload-agent = Agent(provider=provider, model=model, on_payload=add_user_metadata)+agent = Agent(model=model, on_payload=add_user_metadata)```## Tool calling@@ -167,8 +165,8 @@ provider.- Set `include_usage=False` in `on_payload` (the field still goesout, but is usually accepted as a no-op even by strictbackends).- - Use a [`CapabilityDescriptor`](./capability-and-presets) (CubePi- `0.5+`) to describe your backend's reasoning wiring declaratively.+ - Use a [`CapabilityDescriptor`](./overview) (CubePi `0.5+`) to+ describe your backend's reasoning wiring declaratively.- **Thinking events but no `thinking_*` events** — Your backendsurfaces reasoning under a non-standard field. Either add a fourthbranch via PR or transcode it with `on_payload`.@@ -181,6 +179,7 @@ provider.best-effort on those backends.## See also+- [Image Generation](./image-generation) — generate images with `openai-images` and image editing.- [Anthropic Provider](./anthropic) — the other built-in.- [Custom Provider](./custom) — write your own from scratch.diff --git a/website/docs/guides/providers/overview.md b/website/docs/guides/providers/overview.mdnew file mode 100644index 0000000..5a9ebab--- /dev/null+++ b/website/docs/guides/providers/overview.md@@ -0,0 +1,296 @@+---+title: Providers Overview+description: "Provider setup, capabilities, and presets in CubePi."+---++# Providers Overview++_Start here for provider setup in CubePi._++This page is the entry point for provider configuration. It covers the+default path for Anthropic and OpenAI, then explains how to describe+wire-level differences and preset overrides when you need to reach a+different backend without writing per-vendor glue. The progression is+deliberate:++1. **The default is zero config.** For Anthropic and OpenAI you write a+ provider and a model. Nothing on this page is required.+2. **For an off-default endpoint, describe the quirks as data.** A+ `CapabilityDescriptor` captures the differences declaratively — no+ subclassing, no forking.++## `provider.model(...)` parameters++Use `provider.model(model_id, ...)` to create a bound model for an agent.+`model_id` is required and positional; everything else is optional keyword+arguments:++- `api: str` — alternate API name/route tag for downstream integrations.+- `reasoning: bool` — enable reasoning mode and thinking-level negotiation.+- `context_window: int` — context-capacity hint used for validation and prompt planning.+- `max_tokens: int` — default max generation cap for this model.+- `temperature: float` — default sampling temperature for this model.+- `cost: ModelCost | None` — optional cost metadata object.+- `thinking_level_map: dict[str, str | None] | None` — optional map for level+ overrides and unsupported levels (`None` disables a level).++## `CapabilityDescriptor` is what to use when behavior differs by backend++`CapabilityDescriptor` is passed to a provider to express wire differences for+all models served by that provider:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ reasoning_off_payload={"extra_body": {"thinking": False}},+ max_tokens_field="max_completion_tokens",+ ),+)+```++If only one model needs an override, use+`model_capability_overrides`:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://openrouter.ai/api/v1",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ ),+ model_capability_overrides={+ "deepseek-r1": CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": "enabled"}},+ ),+ },+)+```++`model_capability_overrides` is matched by exact `model_id`.++`CapabilityDescriptor` supports these fields:++- `reasoning_on_payload / reasoning_off_payload` — payload merged when+ reasoning is on/off.+- `reasoning_level` (`ReasoningLevelSpec`) — map `off`/`minimal`/... to backend+ payload paths.+- `temperature` (`TemperatureSpec`) — clip, force, or strip temperature.+- `max_tokens_field` — pick `max_tokens` or `max_completion_tokens`.+- `supports_tools` / `supports_images` / `supports_streaming` — metadata consumed+ by host UI and product code.++:::note Preset catalogs live in the host application+CubePi ships the **mechanism** (the `CapabilityDescriptor` and the wire+runtime that applies it), not a catalog of vendors. A ready-made list of+providers — base URLs, auth, regional/coding-plan endpoints, model lists —+is product data and belongs to the application embedding CubePi (for+example, cubebox maintains its own provider catalog). To reach a specific+vendor, build the provider with the right `base_url` + `CapabilityDescriptor`+as shown below.+:::++## 1. The simple case — no config at all++Most users never touch capabilities. The built-in providers ship with+sensible defaults:++```python+import cubepi+from cubepi import Agent+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(provider_id="anthropic") # reads ANTHROPIC_API_KEY+agent = Agent(model=provider.model("claude-sonnet-4-6"))+await agent.prompt("Hello!")+```++That's the whole setup. A provider built without `capability=` produces+byte-identical output to CubePi `0.4` — the machinery below only kicks in+when you ask for it.++## 2. Off-default endpoints — the CapabilityDescriptor++When you want a model that isn't OpenAI or Anthropic — DeepSeek, Qwen,+Doubao, an OpenRouter route, a local server — the awkward part is each+one's wire dialect (does it want `max_tokens` or `max_completion_tokens`?+how is reasoning toggled?). You don't subclass a provider; you describe the+quirks as a [`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html)+and pass it in, along with the right `base_url` and provider class for the+endpoint's wire shape:++```python+import os+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key=os.environ["DEEPSEEK_API_KEY"],+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_off_payload={"extra_body": {"reasoning": {"exclude": True}}},+ reasoning_on_payload={"extra_body": {"reasoning": {"exclude": False}}},+ ),+)+```++Pick the provider class by the endpoint's wire dialect:++| Wire dialect | Provider class |+| --- | --- |+| `anthropic-messages` | `AnthropicProvider` |+| `openai-completions` | `OpenAIProvider` |+| `openai-responses` | `OpenAIResponsesProvider` |++Each field maps to one wire behavior, and an unset field does nothing — so+you only declare what's actually different.++### `max_tokens_field`++`"max_tokens"` (default) or `"max_completion_tokens"`. Some+OpenAI-compatible servers accept only one spelling; this renames the key+on the way out. **Effect:** wrong choice → the server ignores your output+cap or 400s.++### `temperature`++A `TemperatureSpec` controlling how the caller's temperature is treated:++```python+from cubepi import TemperatureSpec++TemperatureSpec(mode="free", min=0.0, max=2.0, default=1.0) # clamp into [min, max]+TemperatureSpec(mode="fixed", fixed_value=1.0) # always overwrite+TemperatureSpec(mode="ignored") # drop the key+```++- **`free`** — the caller's value is clamped into `[min, max]`; if none was+ sent, nothing is written. **Effect:** protects against an out-of-range+ value the backend would reject.+- **`fixed`** — `fixed_value` always wins. **Effect:** use for models that+ permit only one temperature (e.g. some o-series reasoning models).+- **`ignored`** — the key is stripped entirely. **Effect:** for backends+ that 400 on any `temperature` while reasoning.++### Reasoning toggle: `reasoning_off_payload` / `reasoning_on_payload`++When thinking is off, `reasoning_off_payload` is deep-merged into the+request; when it's on, `reasoning_on_payload` is. **Effect:** this is how+"turn reasoning on/off" becomes whatever field the vendor expects:++```python+CapabilityDescriptor(+ reasoning_off_payload={"extra_body": {"enable_thinking": False}},+ reasoning_on_payload={"extra_body": {"enable_thinking": True}},+)+```++The merge recurses into nested dicts; arrays are atomic; on a collision+the capability value wins.++### Reasoning level: `reasoning_level` (three shapes)++Beyond on/off, CubePi maps a `ThinkingLevel`+(`off`/`minimal`/`low`/`medium`/`high`/`xhigh`) onto a concrete wire value+written at a dotted `path`. `kind` picks the shape:++`ReasoningLevelSpec` only changes how that level is serialized. You still need+two call-site controls:++- set `reasoning=True` when binding the model (enable reasoning for that model)+- set the Agent's `thinking` argument to one of `off|minimal|low|medium|high|xhigh`+ (defaults to `off`).++```python+from cubepi import CapabilityDescriptor, ReasoningLevelSpec+from cubepi.providers.openai import OpenAIProvider+from cubepi import Agent++provider = OpenAIProvider(+ api_key="...",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"reasoning": {"enabled": True}}},+ reasoning_level=ReasoningLevelSpec(+ path="reasoning.effort",+ kind="effort",+ level_to_effort={+ "off": "low",+ "minimal": "low",+ "low": "low",+ "medium": "medium",+ "high": "high",+ "xhigh": "high",+ },+ ),+ ),+)++agent = Agent(model=provider.model("deepseek-r1", reasoning=True), thinking="high")+```++```python+from cubepi import ReasoningLevelSpec++# int_budget — a token budget (Anthropic).+ReasoningLevelSpec(+ path="thinking.budget_tokens", kind="int_budget",+ level_budgets={"off": 0, "minimal": 1024, "low": 2048,+ "medium": 8192, "high": 16384, "xhigh": 16384},+)++# effort — an effort string (OpenAI Responses).+ReasoningLevelSpec(+ path="reasoning.effort", kind="effort",+ level_to_effort={"minimal": "minimal", "low": "low",+ "medium": "medium", "high": "high", "xhigh": "high"},+)++# enum — a vendor-specific state (Doubao's 3-state thinking).+ReasoningLevelSpec(+ path="thinking.type", kind="enum",+ level_to_enum={"off": "disabled", "low": "enabled", "high": "enabled"},+)+```++**Effect:** a level missing from the map is simply not written, so the+endpoint keeps its own default for that level.++### `supports_tools` / `supports_images` / `supports_streaming`++Declarative flags read by host applications and frontends (for example, to+grey out image upload). The providers themselves don't gate on them.++### Per-model overrides on a shared endpoint++One gateway (OpenRouter, LiteLLM, an internal proxy) often serves both+reasoning and non-reasoning models. `model_capability_overrides` maps a+`model_id` to a descriptor that **replaces** the base one for that model:++```python+provider = OpenAIProvider(+ api_key="…",+ base_url="https://openrouter.ai/api/v1",+ capability=base_cap, # default for unlisted models+ model_capability_overrides={+ "deepseek/deepseek-r1": reasoning_cap, # this model only+ },+)+```++Resolution is exact-match on `model_id`; anything not listed falls back to+`capability`.++## See also++- [OpenAI Provider](./openai) — concrete OpenAI / OpenAI-compatible setup.+- [Anthropic Provider](./anthropic) — the `int_budget` reasoning shape in practice.+- [Writing a Custom Provider](./custom) — when the endpoint isn't even OpenAI/Anthropic-shaped.+- [API Reference → `cubepi.providers`](../../api/cubepi-providers).diff --git a/website/docs/guides/tracing/getting-started.md b/website/docs/guides/tracing/getting-started.mdindex 738fcc6..04413ab 100644--- a/website/docs/guides/tracing/getting-started.md+++ b/website/docs/guides/tracing/getting-started.md@@ -32,8 +32,7 @@ from cubepi.tracing.exporters import JsonlSpanExporterasync def main() -> None:agent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),system_prompt="Be helpful.",)@@ -235,8 +234,7 @@ runs.```pythonasync with tracer.oneshot(- provider=provider,- model=model,+ model=model,operation="consolidate_memory", # labelled in the tracemetadata={"conversation_id": conv_id}, # queryable via --meta) as session:diff --git a/website/docs/intro.mdx b/website/docs/intro.mdxindex c4940d4..362337e 100644--- a/website/docs/intro.mdx+++ b/website/docs/intro.mdx@@ -35,10 +35,10 @@ working tool-using agent in under five minutes.conversation length — SQLite for laptops, Postgres for production.- **Native multi-provider.** Anthropic and OpenAI ship in the box,through a `Provider` Protocol. Add a new provider in one class.-- **Seven-hook middleware.** `transform_context`, `convert_to_llm`,+- **Eight-hook middleware.** `transform_context`, `convert_to_llm`,`transform_system_prompt`, `before_tool_call`, `after_tool_call`,- `should_stop_after_turn`, `after_model_response` — each with explicit- composition rules. No mystery node ordering.+ `should_stop_after_turn`, `after_model_response`, `on_run_end` — each+ with explicit composition rules. No mystery node ordering.- **MCP loaders.** Point at any[Model Context Protocol](https://modelcontextprotocol.io) server(HTTP or stdio) and get back a list of `AgentTool`s.@@ -70,13 +70,10 @@ You are reading the **Next 🚧** channel — unreleased docs that trackthe `main` branch. APIs documented here can still change before theyship in a tagged release.-The latest released version is `v<PackageVersion/>` ([switch to it-from the version picker, top-right](pathname:///)). Human-in-the-Loop-([guide →](./guides/hitl/overview)) — `HitlChannel`, `CheckpointedChannel`,-`ask_user`, `ConfirmToolCallMiddleware` — landed in 0.6. The native-OpenTelemetry tracing module ([guide →](./guides/tracing/overview))-— `Tracer`, `Meter`, content recording, MCP CLIENT spans, per-run-`tracing_context` tags — landed in 0.4.+The latest released version is v<PackageVersion /> ([switch to it from the+version picker, top-right](pathname:///)). CubePi 0.7 adds provider-level+`generate()`, compaction and subagent middleware, `on_run_end`, typed provider+errors, `Tracer.oneshot()`, and checkpointed HITL run IDs.Source, issues, and discussion live on[GitHub](https://github.com/cubeplexai/cubepi).diff --git a/website/docs/migration/from-langgraph.md b/website/docs/migration/from-langgraph.mdindex d67abdb..a39538e 100644--- a/website/docs/migration/from-langgraph.md+++ b/website/docs/migration/from-langgraph.md@@ -86,8 +86,7 @@ async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, onagent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),tools=[AgentTool(name="get_weather",description="Get current weather for a city.",diff --git a/website/docs/recipes/ask-user-form.md b/website/docs/recipes/ask-user-form.mdindex e0f1eee..2da7bbf 100644--- a/website/docs/recipes/ask-user-form.md+++ b/website/docs/recipes/ask-user-form.md@@ -17,7 +17,6 @@ from cubepi.hitl import InMemoryChannel, ask_user_toolchannel = InMemoryChannel()agent = Agent(- provider=...,model=...,system_prompt=("When you need the user to choose among options, use the ask_user tool. "@@ -160,7 +159,6 @@ from cubepi.hitl import InMemoryChannel, ask_user_toolchannel = InMemoryChannel()agent = Agent(- provider=...,model=...,tools=[ask_user_tool(channel)],channel=channel,diff --git a/website/docs/recipes/multi-provider-failover.md b/website/docs/recipes/multi-provider-failover.mdindex 5b61895..bc01a58 100644--- a/website/docs/recipes/multi-provider-failover.md+++ b/website/docs/recipes/multi-provider-failover.md@@ -22,10 +22,11 @@ from typing import Sequencefrom cubepi.providers.base import (AssistantMessage,+ BaseProvider,+ BoundModel,Message,MessageStream,Model,- Provider,StreamEvent,StreamOptions,ToolDefinition,@@ -37,7 +38,7 @@ from cubepi.providers.openai import OpenAIProviderlog = logging.getLogger(__name__)-class FailoverProvider:+class FailoverProvider(BaseProvider):"""Try providers in order; fall over on construction or first-event errors.Built-in providers swallow API/network errors and surface them as@@ -51,8 +52,9 @@ class FailoverProvider:require buffering the whole turn — out of scope here."""- def __init__(self, primary_pair: tuple[Provider, Model], *fallbacks: tuple[Provider, Model]) -> None:- self._chain: list[tuple[Provider, Model]] = [primary_pair, *fallbacks]+ def __init__(self, primary: BoundModel, *fallbacks: BoundModel) -> None:+ super().__init__(provider_id=primary.spec.provider_id)+ self._chain: list[BoundModel] = [primary, *fallbacks]async def stream(self,@@ -65,7 +67,9 @@ class FailoverProvider:) -> MessageStream:last_error: str | None = None- for provider, mapped_model in self._chain:+ for bound_model in self._chain:+ provider = bound_model.provider+ mapped_model = bound_model.spec# Construction-time failures (rare — most stay inside the producer task).try:inner = await provider.stream(@@ -76,7 +80,7 @@ class FailoverProvider:options=options,)except Exception as e:- log.warning("provider %s failed at construction: %s", mapped_model.provider, e)+ log.warning("provider %s failed at construction: %s", mapped_model.provider_id, e)last_error = repr(e)continue@@ -90,7 +94,7 @@ class FailoverProvider:if first.type == "error":log.warning("provider %s errored on first event: %s",- mapped_model.provider, first.error_message)+ mapped_model.provider_id, first.error_message)last_error = first.error_message or "stream error"continue@@ -129,30 +133,31 @@ class FailoverProvider:import asyncioimport os-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.openai import OpenAIProviderfrom failover import FailoverProviderasync def main():+ anthropic = AnthropicProvider(+ provider_id="anthropic",+ api_key=os.environ["ANTHROPIC_API_KEY"],+ )+ openai = OpenAIProvider(+ provider_id="openai",+ api_key=os.environ["OPENAI_API_KEY"],+ )failover = FailoverProvider(- (- AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),- Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),- ),- (- OpenAIProvider(api_key=os.environ["OPENAI_API_KEY"]),- Model(id="gpt-5", provider="openai"),- ),+ anthropic.model("claude-sonnet-4-5-20250929"),+ openai.model("gpt-5"),)# The model passed here is overridden inside FailoverProvider; pass any# placeholder. We use the primary's so usage tracking labels match the# happy path.agent = Agent(- provider=failover,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=failover.model("claude-sonnet-4-5-20250929"),system_prompt="You answer concisely.",)agent.subscribe(lambda e, s=None: None)@@ -181,7 +186,7 @@ NON_RETRYABLE_HINTS = ("bad request", "invalid_request_error", "401", "403")if first.type == "error":msg = (first.error_message or "").lower()if any(h in msg for h in NON_RETRYABLE_HINTS):- raise RuntimeError(f"non-retryable error from {mapped_model.provider}: {msg}")+ raise RuntimeError(f"non-retryable error from {mapped_model.provider_id}: {msg}")last_error = first.error_messagecontinue```diff --git a/website/docs/recipes/persistent-chat.md b/website/docs/recipes/persistent-chat.mdindex a6c6fb3..b788bf3 100644--- a/website/docs/recipes/persistent-chat.md+++ b/website/docs/recipes/persistent-chat.md@@ -24,12 +24,11 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main(thread_id: str):- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])async with SQLiteCheckpointer("chat.db") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a concise, friendly assistant.",checkpointer=cp,thread_id=thread_id,@@ -94,7 +93,6 @@ In a web service, derive `thread_id` from the authenticated user:```pythonagent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id=f"user-{user_id}",@@ -129,12 +127,10 @@ class SlidingWindow(Middleware):self.n = nasync def transform_context(self, messages, *, ctx, signal=None):- del ctx, signalreturn messages[-self.n:] if len(messages) > self.n else messagesagent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id=thread_id,@@ -154,7 +150,7 @@ Same code, different checkpointer:from cubepi.checkpointer import PostgresCheckpointerasync with PostgresCheckpointer("postgresql://…") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id=…)+ agent = Agent(model=…, checkpointer=cp, thread_id=…)```Postgres is the right choice for multi-instance services or manydiff --git a/website/docs/recipes/postgres-fastapi.md b/website/docs/recipes/postgres-fastapi.mdindex 55963d4..0eee2b8 100644--- a/website/docs/recipes/postgres-fastapi.md+++ b/website/docs/recipes/postgres-fastapi.md@@ -81,7 +81,7 @@ from cubepi.providers.anthropic import AnthropicProvider# --- App lifecycle -------------------------------------------------------_provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+_provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])_checkpointer: PostgresCheckpointer | None = None@@ -123,8 +123,7 @@ async def post_message(async def event_generator() -> AsyncIterator[dict]:agent = Agent(- provider=_provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=_provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a helpful assistant.",checkpointer=_checkpointer,thread_id=thread_id,diff --git a/website/docs/recipes/resumable-tasks.md b/website/docs/recipes/resumable-tasks.mdindex 6b52af2..7887b8c 100644--- a/website/docs/recipes/resumable-tasks.md+++ b/website/docs/recipes/resumable-tasks.md@@ -112,8 +112,7 @@ from tools import transcode_tool # the wrapped AgentTool from aboveasync def main(thread_id: str, initial_prompt: str | None):async with SQLiteCheckpointer("jobs.db") as cp:agent = Agent(- provider=AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"]).model("claude-sonnet-4-5-20250929"),system_prompt="You orchestrate video transcoding jobs.",tools=[transcode_tool],checkpointer=cp,diff --git a/website/docs/recipes/sandbox-confirm.md b/website/docs/recipes/sandbox-confirm.mdindex 3bd336a..01cbb3e 100644--- a/website/docs/recipes/sandbox-confirm.md+++ b/website/docs/recipes/sandbox-confirm.md@@ -57,8 +57,7 @@ async def main():channel = CheckpointedChannel(checkpointer=cp, thread_id="session-1")agent = Agent(- provider=anthropic,- model=Model(id="claude-sonnet-4-6", provider="anthropic"),+ model=anthropic.model("claude-sonnet-4-6"),system_prompt="You are a helpful assistant with access to a bash shell.",tools=[bash_tool],middleware=[diff --git a/website/docs/recipes/weather-agent.md b/website/docs/recipes/weather-agent.mdindex 3253a23..b13ad5f 100644--- a/website/docs/recipes/weather-agent.md+++ b/website/docs/recipes/weather-agent.md@@ -82,10 +82,9 @@ weather_tool = AgentTool(# --- The agent ----------------------------------------------------------async def main():- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a concise weather assistant. Always use the tool; don't guess.",tools=[weather_tool],)diff --git a/website/docusaurus.config.ts b/website/docusaurus.config.tsindex ae65737..e38cf18 100644--- a/website/docusaurus.config.ts+++ b/website/docusaurus.config.ts@@ -28,10 +28,11 @@ const classicOptions: ClassicOptions = {docs: {sidebarPath: './sidebars.ts',editUrl: 'https://github.com/cubeplexai/cubepi/edit/main/website/',- lastVersion: '0.6',+ lastVersion: '0.7',versions: {current: { label: 'Next 🚧', path: 'next', banner: 'unreleased', noIndex: true },- '0.6': { label: '0.6 (latest)', path: '' },+ '0.7': { label: '0.7 (latest)', path: '' },+ '0.6': { label: '0.6', path: '0.6', noIndex: true },'0.5': { label: '0.5', path: '0.5', noIndex: true },'0.4': { label: '0.4', path: '0.4', noIndex: true },'0.3': { label: '0.3', path: '0.3', noIndex: true },@@ -47,6 +48,7 @@ const classicOptions: ClassicOptions = {'/docs/0.3/**','/docs/0.4/**','/docs/0.5/**',+ '/docs/0.6/**',],},};diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current.json b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current.jsonindex b610fd2..06a974b 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current.json+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current.json@@ -27,6 +27,10 @@"message": "中间件","description": "The label for category 'Middleware' in sidebar 'docs'"},+ "sidebar.docs.category.Human-in-the-Loop": {+ "message": "人机协同",+ "description": "The label for category 'Human-in-the-Loop' in sidebar 'docs'"+ },"sidebar.docs.category.MCP": {"message": "MCP","description": "The label for category 'MCP' in sidebar 'docs'"diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/core-concepts.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/core-concepts.mdindex d86c151..f278146 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/core-concepts.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/core-concepts.md@@ -154,7 +154,7 @@ from cubepi.hitl import InMemoryChannel, ConfirmToolCallMiddleware, ask_user_toochannel = InMemoryChannel()agent = Agent(- provider=…, model=…,+ model=…,tools=[bash_tool, ask_user_tool(channel)],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],channel=channel,diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/installation.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/installation.mdindex e05b887..1511562 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/installation.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/installation.md@@ -64,8 +64,8 @@ import osfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.openai import OpenAIProvider-anthropic = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])-openai = OpenAIProvider(api_key=os.environ["OPENAI_API_KEY"])+anthropic = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+openai = OpenAIProvider(provider_id="openai", api_key=os.environ["OPENAI_API_KEY"])```你也可以传 `base_url=...` 指向自托管端点或兼容代理(如 Anthropic Bedrock、diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.mdindex 490deac..0b7924f 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md@@ -40,11 +40,10 @@ async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, onasync def main():- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="你是一个简洁的天气助手。",tools=[AgentTool(@@ -101,9 +100,7 @@ CubePi 跑了一个概念上长这样的循环:- **必须在 prompt 之前订阅。** Listener 只会收到 `subscribe` 之后发出的事件。先 prompt 再 subscribe,早期事件就丢了。-- **`Model(id=..., provider=...)` 是必填的。** Agent 使用 `model.provider`- 来 clamp 思考等级、给响应打标签。`id` 必须是你的 provider 支持的- 模型名。+- **`provider.model(...)` 会把模型绑定到 provider。** provider 保存凭证和可选的 `provider_id` 元数据;模型 id 必须是这个 provider 支持的模型名。- **`execute` 签名是固定的。** 哪怕你不用 keyword-only 参数,签名也得保留 `(tool_call_id, params, *, signal, on_update)`。CubePi 总是会传它们。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.mdindex 9166798..a959a90 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md@@ -19,7 +19,7 @@ import osfrom cubepi import Modelfrom cubepi.providers.anthropic import AnthropicProvider-provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])model = Model(id="claude-sonnet-4-5-20250929",provider="anthropic",@@ -77,7 +77,6 @@ weather_tool = AgentTool(from cubepi import Agentagent = Agent(- provider=provider,model=model,system_prompt="你是一个简洁的天气助手。",tools=[weather_tool],diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/multi-turn.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/multi-turn.mdindex f648dae..8a23890 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/multi-turn.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/multi-turn.md@@ -65,7 +65,7 @@ agent.follow_up(UserMessage(content=[TextContent(text="And what about Osaka?")])```pythonasync with SQLiteCheckpointer("conv.db") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id="conv-1")+ agent = Agent(model=…, checkpointer=cp, thread_id="conv-1")await agent.prompt("hello") # loads existing history firstawait agent.prompt("how are you?")```@@ -98,7 +98,6 @@ from cubepi.checkpointer import SQLiteCheckpointerasync with SQLiteCheckpointer("conv.db") as cp:agent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id="user-42",diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/tool-use.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/tool-use.mdindex 6e50422..ffe86eb 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/tool-use.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/tool-use.md@@ -48,7 +48,6 @@ search_tool = AgentTool(```pythonagent = Agent(- provider=provider,model=model,tools=[search_tool, fetch_url_tool, summarise_tool],)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/custom.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/custom.mdindex 61811db..4aad50c 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/custom.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/custom.md@@ -101,7 +101,7 @@ class RedisCheckpointer:```pythonasync with RedisCheckpointer("redis://localhost:6379") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id="user-42")+ agent = Agent(model=…, checkpointer=cp, thread_id="user-42")await agent.prompt("hi")```@@ -131,7 +131,7 @@ class FileCheckpointer:...# Pass directly:-agent = Agent(provider=…, model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")+agent = Agent(model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")```## 测试你的后端@@ -144,18 +144,18 @@ from cubepi.providers import FauxProvider, faux_assistant_messageasync def test_roundtrip():cp = MyCheckpointer(…)- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message("hello")])- agent1 = Agent(provider=provider, model=Model(id="t", provider="faux"),+ agent1 = Agent(model=provider.model("t"),checkpointer=cp, thread_id="t1")agent1.subscribe(lambda e, s=None: None)await agent1.prompt("hi")# Fresh agent, same thread — should restore history.- provider2 = FauxProvider()+ provider2 = FauxProvider(provider_id="faux")provider2.set_responses([faux_assistant_message("hello again")])- agent2 = Agent(provider=provider2, model=Model(id="t", provider="faux"),+ agent2 = Agent(model=provider2.model("t"),checkpointer=cp, thread_id="t1")agent2.subscribe(lambda e, s=None: None)await agent2.prompt("hi again")diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/mysql.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/mysql.mdindex f231247..74d1f2b 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/mysql.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/mysql.md@@ -27,11 +27,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with MySQLCheckpointer("mysql://user:pass@host:3306/dbname") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/postgres.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/postgres.mdindex 2164f23..eb39196 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/postgres.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/postgres.md@@ -27,11 +27,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with PostgresCheckpointer("postgresql://user:pass@host/dbname") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/sqlite.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/sqlite.mdindex 7f3a7f7..a893b22 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/sqlite.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/checkpointing/sqlite.md@@ -27,11 +27,10 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main():- provider = AnthropicProvider(api_key="…")+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")async with SQLiteCheckpointer("agent.db") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),checkpointer=cp,thread_id="user-42",)@@ -107,8 +106,8 @@ CREATE TABLE IF NOT EXISTS thread_pending_request (```pythonasync with SQLiteCheckpointer("agent.db") as cp:- alice = Agent(provider=…, model=…, checkpointer=cp, thread_id="alice")- bob = Agent(provider=…, model=…, checkpointer=cp, thread_id="bob")+ alice = Agent(model=…, checkpointer=cp, thread_id="alice")+ bob = Agent(model=…, checkpointer=cp, thread_id="bob")# 每个调用只 load/append 自己的 thread。```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/hitl/durable.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/hitl/durable.mdindex 5d70035..c4b79b6 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/hitl/durable.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/hitl/durable.md@@ -15,7 +15,7 @@ async with PostgresCheckpointer("postgresql://...") as cp:channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")agent = Agent(- provider=…, model=…,+ model=…,tools=[bash_tool],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],channel=channel, checkpointer=cp, thread_id="conv-42",@@ -42,7 +42,7 @@ async with PostgresCheckpointer("postgresql://...") as cp:channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")agent = Agent(- provider=…, model=…,+ model=…,tools=[bash_tool],middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],channel=channel, checkpointer=cp, thread_id="conv-42",diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/auth.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/auth.mdindex bf3da50..6044539 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/auth.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/auth.md@@ -154,7 +154,7 @@ async def build_agent_for_user(user_id: str) -> Agent:server_url="https://mcp.example.com/sse",headers={"Authorization": f"Bearer {token}"},)- return Agent(provider=provider, model=model, tools=tools)+ return Agent(model=model, tools=tools)```不要跨用户缓存同一份 `tools` 列表——闭包在加载时已保留了各自的认证请求头。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/loading.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/loading.mdindex c9d120f..3fc4d97 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/loading.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/mcp/loading.md@@ -36,8 +36,7 @@ async def main():)agent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),tools=tools, # all server tools, ready to use)agent.subscribe(lambda e, s=None: None)@@ -97,7 +96,6 @@ mcp_tools = await load_mcp_tools_stdio(command="…", args=[…])my_tools = [weather_tool, search_tool]agent = Agent(- provider=provider,model=model,tools=my_tools + mcp_tools,)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.mdnew file mode 100644index 0000000..5b5bff5--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md@@ -0,0 +1,75 @@+---+title: 上下文压缩+description: "使用 CompactionMiddleware 总结较早轮次,同时保留完整 CubePi 历史。"+---++# 上下文压缩++`CompactionMiddleware` 让长对话保持在模型上下文窗口内,同时不删除 agent+历史。它把较早轮次总结进 `ctx.extra`,再把压缩后的视图发给模型:一条摘要+消息加最近消息。`agent.state.messages` 和 checkpointer 历史仍然完整。++## 基本设置++用便宜模型做摘要,用正常模型运行 agent:++```python+from cubepi import Agent+from cubepi.middleware import CompactionMiddleware++agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=checkpointer,+ thread_id="conv_123",+ middleware=[+ CompactionMiddleware(+ summary_model=summary_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+ ),+ ],+)+```++摘要调用使用 `Provider.generate(...)`,并设置 `temperature=0.0`、+`thinking="off"`、`max_output_tokens=max_summary_tokens`。++## 持久化内容++middleware 会向 `AgentContext.extra` 写入两个键:++- `compaction` —— 摘要状态,以及它覆盖的消息引用。+- `compaction_until_msg_index` —— 已总结到的历史边界。++绑定 checkpointer 时,CubePi 会在 `agent_end` 通过 `save_extra` 保存+`ctx.extra`,所以下一个进程可以带着已有摘要继续。如果消息引用与当前历史不再+匹配,middleware 会清除旧状态并重新开始,而不是发送无效摘要。++## 阈值选择++先用保守值:++```python+CompactionMiddleware(+ summary_model=cheap_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+)+```++如果模型上下文很大、希望减少摘要调用,可以提高+`max_tokens_before_compact`。如果最近工具输出或用户修正很重要,可以提高+`keep_recent_messages`。长时间研究或编码会话可提高 `max_summary_tokens`。++## 失败行为++如果摘要 provider 失败,CubePi 会记录 warning,并继续使用之前的压缩视图或+原始消息。agent 不会仅仅因为压缩刷新失败而失败。++## 什么时候不用++短任务、无状态 agent、或需要模型看到旧工具输出每个 token 的流程,不适合使用+compaction。这些场景里,简单的滑动窗口 `transform_context` hook 更容易推理。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/composition.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/composition.mdindex 353b343..7cd66c7 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/composition.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/composition.md@@ -147,7 +147,7 @@ agent = Agent(```pythonclass JustTransform(Middleware):- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):return messages[-10:]# 没有其他 hook。CubePi 不会调用它们。```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/examples.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/examples.mdindex 89a9de2..164c0db 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/examples.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/examples.md@@ -39,7 +39,7 @@ class RateLimitMiddleware(Middleware):使用:```python-agent = Agent(provider=…, model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])+agent = Agent(model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])```当达到限制时,模型会看到一个工具结果说"Rate limit exceeded…"并通常会等待或询问用户。@@ -145,7 +145,7 @@ class SlidingWindow(Middleware):def __init__(self, max_messages: int = 20) -> None:self.max_messages = max_messages- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):if len(messages) <= self.max_messages:return messagesreturn messages[-self.max_messages:]@@ -158,7 +158,7 @@ class SlidingWindow(Middleware):```pythonclass SummaryInjector(Middleware):- async def transform_system_prompt(self, system_prompt, *, signal=None):+ async def transform_system_prompt(self, system_prompt, *, ctx, signal=None):summary = "Earlier in this conversation we discussed: …"return f"{system_prompt}\n\nContext: {summary}".strip()```@@ -239,7 +239,7 @@ from cubepi.hitl import ConfirmToolCallMiddleware, InMemoryChannelchannel = InMemoryChannel()agent = Agent(- provider=…, model=…,+ model=…,middleware=[ConfirmToolCallMiddleware(channel,@@ -266,7 +266,7 @@ def my_policy(ctx):return AskUser(timeout_seconds=180)agent = Agent(- provider=…, model=…,+ model=…,middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],)```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/hooks.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/hooks.mdindex 2adf752..a30d06c 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/hooks.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/hooks.md@@ -12,7 +12,7 @@ description: "CubePi 的 8 个中间件 hook 参考——transform_context、beffrom cubepi import Middlewareclass MyMiddleware(Middleware):- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):...```@@ -21,11 +21,19 @@ class MyMiddleware(Middleware):## `transform_context````python-async def transform_context(self, messages: list[Message], *, signal=None) -> list[Message]:+async def transform_context(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+ signal=None,+) -> list[Message]:...```-**每次调用模型之前**触发,作用于完整的消息列表。用于:+**每次调用模型之前**触发,作用于完整的消息列表。`ctx` 是当前+`AgentContext`;使用 `ctx.extra` 存放每次运行或 checkpointer 可持久化的+middleware 状态。用于:- 截断或总结以适配上下文窗口。- 注入系统提醒(更好用 `transform_system_prompt`)。@@ -38,7 +46,12 @@ async def transform_context(self, messages: list[Message], *, signal=None) -> li## `convert_to_llm````python-async def convert_to_llm(self, messages: list[Message]) -> list[Message]:+async def convert_to_llm(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+) -> list[Message]:...```@@ -53,7 +66,13 @@ async def convert_to_llm(self, messages: list[Message]) -> list[Message]:## `transform_system_prompt````python-async def transform_system_prompt(self, system_prompt: str, *, signal=None) -> str:+async def transform_system_prompt(+ self,+ system_prompt: str,+ *,+ ctx: AgentContext,+ signal=None,+) -> str:...```@@ -213,7 +232,7 @@ class MaxTurnsMiddleware(Middleware):return self.turns >= self.max_turns-agent = Agent(provider=…, model=…, middleware=[MaxTurnsMiddleware(5)])+agent = Agent(model=…, middleware=[MaxTurnsMiddleware(5)])```## 另请参阅diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/subagents.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/subagents.mdnew file mode 100644index 0000000..da95d07--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/subagents.md@@ -0,0 +1,99 @@+---+title: 子 Agent+description: "使用 SubagentMiddleware 把自包含任务委派给 CubePi 子 Agent。"+---++# 子 Agent++`SubagentMiddleware` 会添加一个 `subagent` 工具。模型调用该工具时,CubePi+创建一个临时子 `Agent`,运行一段自包含 prompt,并把子 agent 最终 assistant+文本作为工具结果返回。++适合父 agent 需要委派有边界的工作:研究、审查、抽取、聚焦实现等。++## 定义子 agent++```python+from cubepi import Agent+from cubepi.middleware import SubagentMiddleware, SubagentSpec++subagents = {+ "researcher": SubagentSpec(+ name="researcher",+ description="Researches a narrow question and returns concise notes.",+ system_prompt="You are a precise research assistant.",+ ),+ "reviewer": SubagentSpec(+ name="reviewer",+ description="Reviews code for bugs, regressions, and missing tests.",+ system_prompt="You are a rigorous senior code reviewer.",+ ),+}++model = provider.model("claude-sonnet-4-5-20250929")++agent = Agent(+ model=model,+ middleware=[+ SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ shared_tools=[web_search],+ ),+ ],+)+```++如果模型请求未知 `subagent_type`,CubePi 回退到 `general-purpose` 子 agent。+如果你没有定义它,middleware 会提供一个基础默认版本。++## 工具访问和 middleware 继承++子 agent 只获得你传入的工具:++- `shared_tools` 对所有子 agent 可用。+- `SubagentSpec.tools` 只对该类型子 agent 可用。+- `excluded_tool_names` 防止递归或 host 专用工具被共享。++用 `inherited_middleware` 传入所有子 agent 都应运行的 middleware。用+`SubagentSpec.middleware` 定义某个子 agent 类型专属行为。++## 把子事件流式传给 host++应用通常需要把子事件写入自己的 UI 或审计日志。传入 `event_mapper` 和可选+`event_handler`:++```python+def map_event(event):+ if event.type == "text_delta":+ return {"type": "subagent_text_delta", "delta": event.delta}+ return None++async def handle_event(agent_id, payload):+ await ui_stream.send({"agent_id": agent_id, **payload})++SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ event_mapper=map_event,+ event_handler=handle_event,+)+```++映射后的 payload 也会存入父工具结果的 `details["subagent_events"]`。++## Tracing 和 abort++通过 `tracer=...` 传入 `Tracer`,即可给每个子运行附加 tracing。嵌套子+agent span 共享父 trace,所以 `cubepi trace view <trace_id>` 会把父工具调用+和子运行一起渲染出来。++父运行的 abort signal 会转发给子 agent。如果父运行在子 agent 执行中被+abort,子 agent 也会被 abort。++## Prompt 约定++父模型在 `subagent` 工具调用里应发送自包含的 `prompt`。不要假设子 agent 能+看到父 agent 的完整隐藏上下文。prompt 里应包含目标、相关事实、约束和期望+输出格式。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/anthropic.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/anthropic.mdindex 82f13ed..a50f96f 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/anthropic.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/anthropic.md@@ -25,11 +25,9 @@ provider = AnthropicProvider(## `Model````python-from cubepi import Model--model = Model(- id="claude-sonnet-4-5-20250929",- provider="anthropic",+provider = AnthropicProvider(provider_id="anthropic")+model = provider.model(+ "claude-sonnet-4-5-20250929",reasoning=True, # 启用思考等级(见下)max_tokens=8192, # 回复 token 上限context_window=200_000, # 模型硬上限@@ -37,8 +35,9 @@ model = Model()```-`id` 就是你传给 SDK 的模型名。`provider` 字符串是 CubePi 内部用的-自由标签 —— 保持稳定即可,不必精确匹配 `"anthropic"`。+第一个参数就是你传给 SDK 的模型名。`provider_id` 是 CubePi 内部用的+自由标签 —— 保持稳定即可,需要时可以让 tracing 和错误信息显示一个+明确的来源标签。## 扩展思考@@ -56,7 +55,7 @@ CubePi 把 `ThinkingLevel` 枚举映射到 Anthropic 的 `budget_tokens`:在 Agent 上设置:```python-agent = Agent(provider=provider, model=model, thinking="medium")+agent = Agent(model=model, thinking="medium")```要自定义 budget,可以通过自己的 `on_payload` hook 传入@@ -82,8 +81,8 @@ API 在扩展思考模式下不接受非默认的 temperature([兼容性文档切到 `"long"`:```python-AnthropicProvider(api_key=…, cache_retention="long") # 1 小时 TTL-AnthropicProvider(api_key=…, cache_retention="none") # 完全禁用+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="long") # 1 小时 TTL+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="none") # 完全禁用```每个 `AssistantMessage` 上的 `Usage` 对象会报告 `cache_read_tokens`@@ -102,7 +101,7 @@ async def my_payload(payload, model):payload.setdefault("metadata", {})["user_id"] = "u-42"return payload # 返回 None 或不返回保留原 payload-agent = Agent(provider=provider, model=model, on_payload=my_payload)+agent = Agent(model=model, on_payload=my_payload)```典型用法:加 `metadata.user_id`(计费)、强制 beta header,或留一份@@ -120,7 +119,7 @@ async def my_response(resp, model):if rate is not None:metrics.gauge("rate_remaining", int(rate))-agent = Agent(provider=provider, model=model, on_response=my_response)+agent = Agent(model=model, on_response=my_response)```两个回调都可以是同步或异步。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/custom.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/custom.mdindex f2e6216..6463d87 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/custom.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/custom.md@@ -20,7 +20,7 @@ class Provider(Protocol):) -> MessageStream: ...```-这就是完整的接口。实现它,`Agent(provider=…)` 就可以接受你的类。+这就是完整的接口。实现它,`Agent(model=…)` 就可以接受你的类。本页涵盖两种场景:@@ -69,7 +69,7 @@ class MyProvider:content=[TextContent(text="")],usage=Usage(),timestamp=time.time(),- provider_id=model.provider,+ provider_id=model.provider_id,model_id=model.id,)ms.push(StreamEvent(type="start", partial=partial.model_copy(deep=True)))@@ -164,7 +164,7 @@ from cubepi.providers import FauxProvider, faux_assistant_message, faux_text, fadef test_my_agent():- provider = FauxProvider()+ provider = FauxProvider(provider_id="faux")provider.set_responses([faux_assistant_message([faux_tool_call("search", {"query": "python"}),@@ -173,8 +173,7 @@ def test_my_agent():])agent = Agent(- provider=provider,- model=Model(id="test", provider="faux"),+ model=provider.model("test"),tools=[my_search_tool],)events = []@@ -202,8 +201,9 @@ def test_my_agent():- **推送后修改 `partial`** —— 推送时使用 `deep=True` 拷贝;调用方异步迭代事件,在迭代过程中发生变更会产生极难调试的别名问题。## 参见+- [图片生成](./image-generation) —— 使用 `openai-images` 与 OpenAI 图片模型。-- [能力描述符与预设目录](./capability-and-presets) —— 在从头编写类之前,先检查你的后端是否只是一个 `CapabilityDescriptor` 或内置预设已经覆盖的 OpenAI/Anthropic 兼容端点。+- [Providers Overview](./overview) —— 在从头编写类之前,先检查你的后端是否只是一个 `CapabilityDescriptor` 或内置预设已经覆盖的 OpenAI/Anthropic 兼容端点。- [API 参考 → providers/base](../../api/cubepi-providers) —— 完整类型列表。- [Anthropic Provider 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/anthropic.py) —— 一个真实完整的示例。- [`FauxProvider` 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py) —— 测试原语,包含流式真实性的细节。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/image-generation.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/image-generation.mdnew file mode 100644index 0000000..07ea601--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/image-generation.md@@ -0,0 +1,137 @@+---+title: 图片生成+description: "使用 CubePi 的图片生成能力。"+---++# 图片生成++这一页介绍 CubePi 的独立图片生成链路。它与 `Agent` 有意分离:对话场景仍然走+`Model`,图片场景走 `ImagesModel`,并直接调用图片 provider。++## `ImagesModel` 类型++图片生成使用 `cubepi.providers.images.types` 中的三个基础类型:++```python+from cubepi.providers.images.types import AssistantImages, ImagesContext, ImagesModel+```++- `ImagesModel`:图片模型绑定(`id`、`provider`、`api`)。+- `ImagesContext`:生成请求体(`prompt`、可选 `input_images`)。+- `AssistantImages`:图片 provider 的返回值,包含 `output`、`stop_reason`,+ 以及可选的 `error_message`。++`input_images` 和 `AssistantImages.output` 中用到的是+`ImageContent`(定义在 `cubepi.providers.base`)。++## 创建图片模型++OpenAI 图片模型一般通过 `create_images_provider("openai-images", ...)` 创建+provider,并用 `ImagesModel` 绑定:++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++image_provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")+```++`api` 是给上游系统做路由与归因的标识,可按需自定义。++## 文生图++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")++resp = await provider.generate_images(+ model=model,+ context=ImagesContext(prompt="一只戴帽子的猫在日落中"),+ options={"size": "1024x1024", "quality": "high", "n": 2},+)++if resp.stop_reason == "error":+ raise RuntimeError(resp.error_message or "图片生成失败")++for block in resp.output:+ print(block.type, block.source[:30], block.media_type)+```++如果不传 `options`,provider 会按默认参数发送(`n=1`),且不带可选的图片参数。++## 图像编辑++`input_images` 非空时自动走编辑链路:++```python+import base64+from cubepi.providers.base import ImageContent+from cubepi.providers.images.types import ImagesContext++with open("source.png", "rb") as fh:+ source_b64 = base64.b64encode(fh.read()).decode("ascii")++ctx = ImagesContext(+ prompt="让它更亮一些,色温更暖。",+ input_images=[ImageContent(source=source_b64, media_type="image/png")],+)+```++`generate_images(model, context=ctx)` 会调用 `edit`,而不是 `generate`。++## `options` 下发行为++`generate_images(..., options)` 是一个透传字典(在补齐 `model` / `prompt` /+`n` 后)直接传给后端。OpenAI 图片 provider 常见参数:++- `size`+- `quality`+- `n`+- `output_format`(`png` / `jpeg` / `webp`)+- `background`+- 以及网关允许的其他扩展参数++provider 只对 `output_format` 做输出类型映射:+`png` -> `image/png`,`jpeg` -> `image/jpeg`,`webp` -> `image/webp`。++## 错误处理++后端失败时 `generate_images(...)` 会返回 `AssistantImages`,并包含:++- `stop_reason="error"`+- `output=[]`+- `error_message=<后端报错>`++这样可以把调用端的成功 / 失败逻辑统一到一个返回分支中处理。++## 测试与模拟++本地测试可以直接使用 `FauxImagesProvider` 产出稳定图像结果:++```python+from cubepi.providers.images.faux import FauxImagesProvider++provider = FauxImagesProvider(png_b64="iVBORw0KGgoAAAANSUh...")+```++适合不依赖真实图片 API 的端到端或 UI 测试。++## 参见++- [Providers Overview](./overview) —— 默认行为和能力描述符。+- [OpenAI Provider](./openai) —— OpenAI 与 OpenAI 兼容网关的配置方式。+- [API Reference → providers(图片类型)](../../api/cubepi-providers)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/openai.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/openai.mdindex d69b1c4..601ab2b 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/openai.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/openai.md@@ -15,19 +15,18 @@ CubePi 内置两个 OpenAI provider,分别覆盖两种 API 接口:## Chat Completions:`OpenAIProvider````python-from cubepi import Modelfrom cubepi.providers.openai import OpenAIProviderprovider = OpenAIProvider(+ provider_id="openai",api_key="sk-…", # or reads OPENAI_API_KEYbase_url=None, # set for OpenAI-compatible serversextra_body=None, # merged into every requestextra_headers=None,)-model = Model(- id="gpt-5",- provider="openai",+model = provider.model(+ "gpt-5",reasoning=True, # enables thinking level mappingmax_tokens=8192,context_window=128_000,@@ -62,7 +61,7 @@ provider = OpenAIProvider(### 能力描述符-OpenAI 与 OpenAI 兼容后端之间的 wire 格式差异(如 `max_tokens` vs `max_completion_tokens`、推理字段名称、temperature 处理方式)可通过构造时传入的 [`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html) 来配置。例如,`max_tokens_field="max_completion_tokens"` 会在请求发出前重命名该 key。详见 [能力描述符](./capability-and-presets) 了解完整配置项(CubePi `0.5+`)。+OpenAI 与 OpenAI 兼容后端之间的 wire 格式差异(如 `max_tokens` vs `max_completion_tokens`、推理字段名称、temperature 处理方式)可通过构造时传入的 [`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html) 来配置。例如,`max_tokens_field="max_completion_tokens"` 会在请求发出前重命名该 key。详见 [能力描述符](./overview) 了解完整配置项(CubePi `0.5+`)。### 指向 vLLM / LiteLLM / DeepSeek@@ -83,15 +82,15 @@ provider = OpenAIProvider()```-关于这些后端的 wire 差异(推理开关、token 字段重命名……),参见 [能力描述符](./capability-and-presets)。+关于这些后端的 wire 差异(推理开关、token 字段重命名……),参见 [能力描述符](./overview)。## Responses API:`OpenAIResponsesProvider````pythonfrom cubepi.providers.openai_responses import OpenAIResponsesProvider-provider = OpenAIResponsesProvider(api_key="sk-…")-model = Model(id="gpt-5", provider="openai_responses", reasoning=True)+provider = OpenAIResponsesProvider(provider_id="openai_responses", api_key="sk-…")+model = provider.model("gpt-5", reasoning=True)```Responses API 在服务端维护状态(通过 `previous_response_id` 引用)。CubePi 追踪 `AssistantMessage.response_id` 并自动回传——你的代码看起来与 Chat Completions 路径完全相同。@@ -112,7 +111,7 @@ async def add_user_metadata(payload, model):payload["user"] = "u-42" # billable user attributionreturn payload-agent = Agent(provider=provider, model=model, on_payload=add_user_metadata)+agent = Agent(model=model, on_payload=add_user_metadata)```## 工具调用@@ -126,12 +125,13 @@ agent = Agent(provider=provider, model=model, on_payload=add_user_metadata)- **`stream_options.include_usage` 被拒绝** —— 部分兼容服务器会拒绝整个 `stream_options` 字段。**`on_payload` 无法修复此问题**:CubePi 0.3 在你的回调执行**之后**才调用 `kwargs.setdefault("stream_options", {})`,因此在 `on_payload` 中删除该 key 会被静默地撤销。解决方案:- 子类化 `OpenAIProvider` 并覆盖 `stream()`,对你的后端跳过 `setdefault`。- 在 `on_payload` 中设置 `include_usage=False`(字段仍会发出,但通常被严格后端接受为无操作)。- - 使用 [`CapabilityDescriptor`](./capability-and-presets)(CubePi `0.5+`)以声明式方式描述你的后端推理配置。+ - 使用 [`CapabilityDescriptor`](./overview)(CubePi `0.5+`)以声明式方式描述你的后端推理配置。- **有推理内容但没有 `thinking_*` 事件** —— 你的后端在非标准字段下暴露推理内容。可通过 PR 添加第四个分支,或使用 `on_payload` 进行转码。- **同一进程中混用多个 provider** —— 每个 provider 持有自己的 HTTP 客户端。按 `(base_url, api_key)` 对复用单个实例,而非每个 agent 各创建一个。- **usage 显示 0 个输入 token** —— 大多数兼容服务器完全省略 usage,或只在最后一个 chunk 中发出。可在 `on_payload` 中检查尾部 chunk 获取提示,或将 token 计数视为这些后端的尽力而为值。## 参见+- [图片生成](./image-generation) —— 使用 `openai-images` 与 OpenAI 图片模型。- [Anthropic Provider](./anthropic) —— 另一个内置 provider。- [自定义 Provider](./custom) —— 从头编写你自己的 provider。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/capability-and-presets.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/overview.mdsimilarity index 60%rename from website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/capability-and-presets.mdrename to website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/overview.mdindex d9c70e2..1c247f8 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/capability-and-presets.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/providers/overview.md@@ -1,17 +1,82 @@----title: 能力描述符-description: "CubePi 中的 provider 能力与模型预设——跨 provider 特性比较。"+title: Providers Overview+description: "CubePi 的 provider 配置、能力和预设总览。"----# 能力描述符+# Providers Overview-_能力描述符在 CubePi `0.5` 中引入。_+_从这里开始看 provider 配置。_-本页介绍如何访问**任意**模型——大型 SaaS API、区域性端点、代码规划层级,或自托管的 vLLM 实例——而无需为每个厂商编写专属胶水代码。设计思路是循序渐进的:+本页是 provider 配置的入口。先讲 Anthropic / OpenAI 的默认路径,+再讲当你需要接入别的后端时,如何用数据的方式描述 wire 差异和+预设覆盖,而不是写各家厂商 glue code。整体顺序是:1. **默认零配置。** 对于 Anthropic 和 OpenAI,只需创建 provider 和 model,本页的内容一概不需要。2. **对于非默认端点,以数据方式描述差异。** `CapabilityDescriptor` 以声明式方式捕获差异——无需子类化,无需 fork。+## `provider.model(...)` 参数++用 `provider.model(model_id, ...)` 为 Agent 创建绑定模型。`model_id` 为必填位置参数,其他参数均为可选关键字参数:++- `api: str` — 覆盖接口名/路由标签,供后续集成读取。+- `reasoning: bool` — 启用推理模式与 reasoning-level 协商。+- `context_window: int` — 上下文窗口提示(主要用于校验与提示词规划)。+- `max_tokens: int` — 该模型默认最大生成 token 上限。+- `temperature: float` — 该模型默认采样温度。+- `cost: ModelCost | None` — 可选的成本元数据对象。+- `thinking_level_map: dict[str, str | None] | None` — 推理等级映射;将某级别设为 `None` 表示该 level 不可用。++## CapabilityDescriptor:当后端模型线不同时该怎么配++`CapabilityDescriptor` 在初始化 provider 时传入,用于统一描述这个 provider+后端在 wire 上的差异。++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ reasoning_off_payload={"extra_body": {"thinking": False}},+ max_tokens_field="max_completion_tokens",+ ),+)+```++如果只有部分模型例外,使用 `model_capability_overrides`:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://openrouter.ai/api/v1",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ ),+ model_capability_overrides={+ "deepseek-r1": CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": "enabled"}},+ ),+ },+)+```++`model_capability_overrides` 使用精确 `model_id` 匹配。++`CapabilityDescriptor` 常用字段:++- `reasoning_on_payload / reasoning_off_payload` — 在 reasoning 开/关时,深度+ 合并到最终 payload。+- `reasoning_level`(`ReasoningLevelSpec`)— 将 `off`/`minimal`/... 映射到后端字段。+- `temperature`(`TemperatureSpec`)— 裁剪、固定或去掉温度参数。+- `max_tokens_field` — 选 `max_tokens` 或 `max_completion_tokens`。+- `supports_tools` / `supports_images` / `supports_streaming` — 供宿主应用或前端消费的能力元数据。+:::note 预设目录由宿主应用维护CubePi 提供的是**机制**(`CapabilityDescriptor` 及其 wire 运行时),而非厂商目录。包含 base URL、认证、区域/代码规划端点、模型列表的现成 provider 列表属于产品数据,应由嵌入 CubePi 的应用自行维护(例如,cubebox 维护自己的 provider 目录)。如需接入特定厂商,请按下文所示,使用正确的 `base_url` + `CapabilityDescriptor` 构建 provider。:::@@ -22,13 +87,11 @@ CubePi 提供的是**机制**(`CapabilityDescriptor` 及其 wire 运行时)```pythonimport cubepi-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.providers.anthropic import AnthropicProvider-agent = Agent(- provider=AnthropicProvider(), # reads ANTHROPIC_API_KEY- model=Model(id="claude-sonnet-4-6", provider="anthropic"),-)+provider = AnthropicProvider(provider_id="anthropic") # reads ANTHROPIC_API_KEY+agent = Agent(model=provider.model("claude-sonnet-4-6"))await agent.prompt("Hello!")```@@ -100,6 +163,37 @@ CapabilityDescriptor(在开/关之外,CubePi 将 `ThinkingLevel`(`off`/`minimal`/`low`/`medium`/`high`/`xhigh`)映射到通过点路径 `path` 写入的具体 wire 值。`kind` 决定形状:+`ReasoningLevelSpec` 只负责「`thinking`/`minimal`/`low`/... 具体映射成后端字段」;要真正生效,还要配两个参数:++- 在 `provider.model(...)` 时把 `reasoning=True`(把这个模型设为推理模型)+- 在 `Agent(...)` 初始化时把 `thinking` 设成 `off|minimal|low|medium|high|xhigh`(默认 `off`)++```python+from cubepi import Agent, CapabilityDescriptor, ReasoningLevelSpec+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"reasoning": {"enabled": True}}},+ reasoning_level=ReasoningLevelSpec(+ path="reasoning.effort",+ kind="effort",+ level_to_effort={+ "off": "low",+ "minimal": "low",+ "low": "low",+ "medium": "medium",+ "high": "high",+ "xhigh": "high",+ },+ ),+ ),+)++agent = Agent(model=provider.model("deepseek-r1", reasoning=True), thinking="high")+```+```pythonfrom cubepi import ReasoningLevelSpecdiff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/getting-started.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/getting-started.mdindex 62013f1..35d6020 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/getting-started.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/getting-started.md@@ -31,8 +31,7 @@ from cubepi.tracing.exporters import JsonlSpanExporterasync def main() -> None:agent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),system_prompt="Be helpful.",)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/intro.mdx b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/intro.mdxindex cc6c5d2..a4abd20 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/intro.mdx+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/intro.mdx@@ -35,8 +35,9 @@ pip install cubepi生产环境。- **原生多 provider。** Anthropic 和 OpenAI 开箱即用,通过 `Provider`Protocol 接入。要新增一个 provider 只需要写一个类。-- **五个 Hook 的中间件。** `transform_context`、`convert_to_llm`、- `before_tool_call`、`after_tool_call`、`should_stop_after_turn`——+- **八个 Hook 的中间件。** `transform_context`、`convert_to_llm`、+ `transform_system_prompt`、`before_tool_call`、`after_tool_call`、+ `should_stop_after_turn`、`after_model_response`、`on_run_end`——每一个都有明确的组合规则。没有"节点执行顺序之谜"。- **MCP 加载器。** 指向任意[Model Context Protocol](https://modelcontextprotocol.io) 服务器@@ -66,12 +67,10 @@ pip install cubepi你正在浏览的是 **Next 🚧** 频道 —— 跟踪 `main` 分支的未发布文档。这里描述的 API 在打 tag 发布之前还可能变动。-最新已发布版本是 `v<PackageVersion/>`([从右上角版本选择器切换](pathname:///))。-人机协同([指南 →](./guides/hitl/overview))—— `HitlChannel`、-`CheckpointedChannel`、`ask_user`、`ConfirmToolCallMiddleware` ——-已在 0.6 发布。原生 OpenTelemetry 追踪模块([指南 →](./guides/tracing/overview))——-`Tracer`、`Meter`、内容录制、MCP CLIENT span、按 run 打标签的-`tracing_context` —— 已在 0.4 发布。+最新已发布版本是 v<PackageVersion />([从右上角版本选择器切换](pathname:///))。+CubePi 0.7 新增 provider 级 `generate()`、压缩和子 Agent 中间件、+`on_run_end`、类型化 provider 错误、`Tracer.oneshot()`,以及带 `run_id`+的 checkpointed HITL 恢复能力。源代码、Issue 和讨论都在[GitHub](https://github.com/cubeplexai/cubepi)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/migration/from-langgraph.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/migration/from-langgraph.mdindex 445e6fa..93e0b2f 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/migration/from-langgraph.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/migration/from-langgraph.md@@ -85,8 +85,7 @@ async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, onagent = Agent(- provider=AnthropicProvider(api_key="…"),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),tools=[AgentTool(name="get_weather",description="Get current weather for a city.",diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/ask-user-form.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/ask-user-form.mdindex 3afeb77..a9d299b 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/ask-user-form.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/ask-user-form.md@@ -17,7 +17,6 @@ from cubepi.hitl import InMemoryChannel, ask_user_toolchannel = InMemoryChannel()agent = Agent(- provider=...,model=...,system_prompt=("当您需要用户在选项中选择时,请使用 ask_user 工具。"@@ -158,7 +157,6 @@ from cubepi.hitl import InMemoryChannel, ask_user_toolchannel = InMemoryChannel()agent = Agent(- provider=...,model=...,tools=[ask_user_tool(channel)],channel=channel,diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/multi-provider-failover.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/multi-provider-failover.mdindex dea93f6..65357ee 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/multi-provider-failover.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/multi-provider-failover.md@@ -22,10 +22,11 @@ from typing import Sequencefrom cubepi.providers.base import (AssistantMessage,+ BaseProvider,+ BoundModel,Message,MessageStream,Model,- Provider,StreamEvent,StreamOptions,ToolDefinition,@@ -37,7 +38,7 @@ from cubepi.providers.openai import OpenAIProviderlog = logging.getLogger(__name__)-class FailoverProvider:+class FailoverProvider(BaseProvider):"""按顺序尝试各 provider;在构造时或首个事件出错时进行故障转移。内置 provider 会吞掉 API/网络错误,并将其作为@@ -50,8 +51,9 @@ class FailoverProvider:需要缓冲整轮内容 —— 超出本 recipe 范围。"""- def __init__(self, primary_pair: tuple[Provider, Model], *fallbacks: tuple[Provider, Model]) -> None:- self._chain: list[tuple[Provider, Model]] = [primary_pair, *fallbacks]+ def __init__(self, primary: BoundModel, *fallbacks: BoundModel) -> None:+ super().__init__(provider_id=primary.spec.provider_id)+ self._chain: list[BoundModel] = [primary, *fallbacks]async def stream(self,@@ -64,7 +66,9 @@ class FailoverProvider:) -> MessageStream:last_error: str | None = None- for provider, mapped_model in self._chain:+ for bound_model in self._chain:+ provider = bound_model.provider+ mapped_model = bound_model.spec# 构造时失败(罕见 —— 大多数错误留在生产者任务内部)。try:inner = await provider.stream(@@ -75,7 +79,7 @@ class FailoverProvider:options=options,)except Exception as e:- log.warning("provider %s failed at construction: %s", mapped_model.provider, e)+ log.warning("provider %s failed at construction: %s", mapped_model.provider_id, e)last_error = repr(e)continue@@ -89,7 +93,7 @@ class FailoverProvider:if first.type == "error":log.warning("provider %s errored on first event: %s",- mapped_model.provider, first.error_message)+ mapped_model.provider_id, first.error_message)last_error = first.error_message or "stream error"continue@@ -127,29 +131,30 @@ class FailoverProvider:import asyncioimport os-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.providers.anthropic import AnthropicProviderfrom cubepi.providers.openai import OpenAIProviderfrom failover import FailoverProviderasync def main():+ anthropic = AnthropicProvider(+ provider_id="anthropic",+ api_key=os.environ["ANTHROPIC_API_KEY"],+ )+ openai = OpenAIProvider(+ provider_id="openai",+ api_key=os.environ["OPENAI_API_KEY"],+ )failover = FailoverProvider(- (- AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),- Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),- ),- (- OpenAIProvider(api_key=os.environ["OPENAI_API_KEY"]),- Model(id="gpt-5", provider="openai"),- ),+ anthropic.model("claude-sonnet-4-5-20250929"),+ openai.model("gpt-5"),)# 此处传入的 model 会被 FailoverProvider 内部覆盖;可传任意占位符。# 我们使用主 provider 的 model,以便用量统计标签与正常路径保持一致。agent = Agent(- provider=failover,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=failover.model("claude-sonnet-4-5-20250929"),system_prompt="You answer concisely.",)agent.subscribe(lambda e, s=None: None)@@ -177,7 +182,7 @@ NON_RETRYABLE_HINTS = ("bad request", "invalid_request_error", "401", "403")if first.type == "error":msg = (first.error_message or "").lower()if any(h in msg for h in NON_RETRYABLE_HINTS):- raise RuntimeError(f"non-retryable error from {mapped_model.provider}: {msg}")+ raise RuntimeError(f"non-retryable error from {mapped_model.provider_id}: {msg}")last_error = first.error_messagecontinue```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/persistent-chat.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/persistent-chat.mdindex 480e3c4..91bc33f 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/persistent-chat.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/persistent-chat.md@@ -24,12 +24,11 @@ from cubepi.providers.anthropic import AnthropicProviderasync def main(thread_id: str):- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])async with SQLiteCheckpointer("chat.db") as cp:agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a concise, friendly assistant.",checkpointer=cp,thread_id=thread_id,@@ -93,7 +92,6 @@ python chat.py bob```pythonagent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id=f"user-{user_id}",@@ -127,12 +125,11 @@ class SlidingWindow(Middleware):def __init__(self, n: int) -> None:self.n = n- async def transform_context(self, messages, *, signal=None):+ async def transform_context(self, messages, *, ctx, signal=None):return messages[-self.n:] if len(messages) > self.n else messagesagent = Agent(- provider=provider,model=model,checkpointer=cp,thread_id=thread_id,@@ -151,7 +148,7 @@ agent = Agent(from cubepi.checkpointer import PostgresCheckpointerasync with PostgresCheckpointer("postgresql://…") as cp:- agent = Agent(provider=…, model=…, checkpointer=cp, thread_id=…)+ agent = Agent(model=…, checkpointer=cp, thread_id=…)```Postgres 适合多实例服务或大量并发用户 ——diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/postgres-fastapi.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/postgres-fastapi.mdindex d02147a..67194d8 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/postgres-fastapi.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/postgres-fastapi.md@@ -79,7 +79,7 @@ from cubepi.providers.anthropic import AnthropicProvider# --- 应用生命周期 -----------------------------------------------------------_provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+_provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])_checkpointer: PostgresCheckpointer | None = None@@ -121,8 +121,7 @@ async def post_message(async def event_generator() -> AsyncIterator[dict]:agent = Agent(- provider=_provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=_provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a helpful assistant.",checkpointer=_checkpointer,thread_id=thread_id,diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/resumable-tasks.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/resumable-tasks.mdindex 8153d06..3b145c6 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/resumable-tasks.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/resumable-tasks.md@@ -107,8 +107,7 @@ from tools import transcode_tool # 上面封装好的 AgentToolasync def main(thread_id: str, initial_prompt: str | None):async with SQLiteCheckpointer("jobs.db") as cp:agent = Agent(- provider=AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"]).model("claude-sonnet-4-5-20250929"),system_prompt="You orchestrate video transcoding jobs.",tools=[transcode_tool],checkpointer=cp,diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/sandbox-confirm.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/sandbox-confirm.mdindex a36fbbf..0071388 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/sandbox-confirm.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/sandbox-confirm.md@@ -56,8 +56,7 @@ async def main():channel = CheckpointedChannel(checkpointer=cp, thread_id="session-1")agent = Agent(- provider=anthropic,- model=Model(id="claude-sonnet-4-6", provider="anthropic"),+ model=anthropic.model("claude-sonnet-4-6"),system_prompt="You are a helpful assistant with access to a bash shell.",tools=[bash_tool],middleware=[diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/weather-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/weather-agent.mdindex 07dd242..a011336 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/weather-agent.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/recipes/weather-agent.md@@ -81,10 +81,9 @@ weather_tool = AgentTool(# --- Agent ----------------------------------------------------------------async def main():- provider = AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"])+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])agent = Agent(- provider=provider,- model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),+ model=provider.model("claude-sonnet-4-5-20250929"),system_prompt="You are a concise weather assistant. Always use the tool; don't guess.",tools=[weather_tool],)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.6.json b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.6.jsonindex 7318c3b..0225566 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.6.json+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.6.json@@ -1,6 +1,6 @@{"version.label": {- "message": "0.6(最新)",+ "message": "0.6","description": "The label for version 0.6"},"sidebar.docs.category.Getting Started": {diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7.json b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7.jsonnew file mode 100644index 0000000..1d50813--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7.json@@ -0,0 +1,50 @@+{+ "version.label": {+ "message": "0.7(最新)",+ "description": "The label for version 0.7"+ },+ "sidebar.docs.category.Getting Started": {+ "message": "快速入门",+ "description": "The label for category 'Getting Started' in sidebar 'docs'"+ },+ "sidebar.docs.category.Guides": {+ "message": "指南",+ "description": "The label for category 'Guides' in sidebar 'docs'"+ },+ "sidebar.docs.category.Agents": {+ "message": "Agent",+ "description": "The label for category 'Agents' in sidebar 'docs'"+ },+ "sidebar.docs.category.Providers": {+ "message": "Provider",+ "description": "The label for category 'Providers' in sidebar 'docs'"+ },+ "sidebar.docs.category.Checkpointing": {+ "message": "检查点",+ "description": "The label for category 'Checkpointing' in sidebar 'docs'"+ },+ "sidebar.docs.category.Middleware": {+ "message": "中间件",+ "description": "The label for category 'Middleware' in sidebar 'docs'"+ },+ "sidebar.docs.category.Human-in-the-Loop": {+ "message": "人机协同",+ "description": "The label for category 'Human-in-the-Loop' in sidebar 'docs'"+ },+ "sidebar.docs.category.MCP": {+ "message": "MCP",+ "description": "The label for category 'MCP' in sidebar 'docs'"+ },+ "sidebar.docs.category.Tracing": {+ "message": "Tracing",+ "description": "The label for category 'Tracing' in sidebar 'docs'"+ },+ "sidebar.docs.category.Recipes": {+ "message": "示例",+ "description": "The label for category 'Recipes' in sidebar 'docs'"+ },+ "sidebar.docs.category.Migration": {+ "message": "迁移指南",+ "description": "The label for category 'Migration' in sidebar 'docs'"+ }+}diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/core-concepts.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/core-concepts.mdnew file mode 100644index 0000000..f278146--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/core-concepts.md@@ -0,0 +1,234 @@+---+title: 核心概念+description: "了解 CubePi 的六个核心概念:Agent、Tool、Provider、Stream & Events、Middleware 和 Checkpointer。"+---++# 核心概念++CubePi 的全部能力可以归纳为六个概念。这一页读一遍,后面的文档基本就+变成查表了。++## Agent++`Agent` 是有状态的门面:你用 provider、model、可选的工具、可选的+middleware/checkpointer 构造它。然后通过三个方法驱动它:++- `await agent.prompt(message)` —— 用一条 user 消息开启新一轮。+- `await agent.resume()` —— 从最后一条已持久化的消息继续(配合+ checkpointer 使用)。+- `agent.steer(message)` / `agent.follow_up(message)` —— 在正在跑的+ 过程中插入消息,或为当前运行结束后排队下一条。++Agent 持有一个 `AgentState`(system prompt、tools、model、消息历史、+未结束的 tool call、流式标志)和一个 subscribers 列表:++```python+unsubscribe = agent.subscribe(my_listener)+# ...+unsubscribe()+```++Subscriber 会收到循环发出的每一个 `AgentEvent`。可以是同步或异步函数。++## Tool++`AgentTool` = name + description + Pydantic 参数模型 + 异步 `execute`:++```python+from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent++class SearchParams(BaseModel):+ query: str+ limit: int = 10++async def execute(tool_call_id, params: SearchParams, *, signal=None, on_update=None):+ # 干活;如果可以被打断,记得检查 signal+ return AgentToolResult(content=[TextContent(text=f"…")])++search = AgentTool(+ name="search",+ description="搜索语料库",+ parameters=SearchParams,+ execute=execute,+)+```++Pydantic schema 会自动转成 JSON Schema 喂给模型。参数解析、错误包装、+并行执行都由框架处理。`execution_mode`、`on_update`(增量进度)、+`terminate`(在工具里结束本轮)见 [工具使用](../guides/agents/tool-use)。++## Provider++任何匹配下面 Protocol 的对象就是 Provider:++```python+class Provider(Protocol):+ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream: ...+```++它返回一个 `MessageStream` —— 一个统一的异步迭代器,产出 `StreamEvent`,+并通过 `await stream.result()` 暴露最终的 `AssistantMessage`。内置 Provider:++- `AnthropicProvider` —— Claude(Messages API,支持思考、缓存、工具使用)。+- `OpenAIProvider` —— GPT 家族(Chat Completions API)。+- `OpenAIResponsesProvider` —— GPT 家族(Responses API,服务端状态)。+- `FauxProvider` —— 确定性测试替身(不发任何网络请求)。++实现一个方法就能写自己的。见 [Providers / 自定义](../guides/providers/custom)。++## Stream 和事件++流和事件分两层:++- **Provider 流** —— `MessageStream` 产出的是 *provider* 事件:+ `start`、`text_start`、`text_delta`、`text_end`、`thinking_*`、+ `toolcall_*`、`done`、`error`。原始 token 流。+- **Agent 事件** —— `agent.subscribe(...)` 收到的内容。十四种类型+ 覆盖整个循环 + HITL:`agent_start`、`agent_end`、`turn_start`、+ `turn_end`、`message_start`、`message_update`、`message_end`、+ `tool_execution_start`、`tool_execution_update`、+ `tool_execution_end`、`hitl_request`、`hitl_answer`、+ `agent_suspended`、`agent_aborted`。++做 UI 订阅 Agent 事件;做底层 token 路由就钻 `event.stream_event`。+见 [流式事件](../guides/agents/streaming)。++## Middleware++`Middleware` 是有最多八个类型化 hook 的类:++| Hook | 何时触发 | 组合规则 |+|---|---|---|+| `transform_context` | 每次调模型之前,处理消息列表 | 链式 —— 每个收到上一个的输出 |+| `convert_to_llm` | provider 序列化之前 | 最后一个实现生效 |+| `transform_system_prompt` | 每次调模型之前,处理 system prompt | 链式 |+| `before_tool_call` | 每个工具调用之前(在参数校验后) | 第一个 `block=True` 短路 |+| `after_tool_call` | 每个工具调用之后(在 `execute` 之后) | 后写覆盖先写 |+| `after_model_response` | assistant 消息落定之后 | 返回 `TurnAction` 控制流向 |+| `should_stop_after_turn` | 每个轮次结束时 | 任一返回 `True` 即停 |+| `on_run_end` | 所有轮次完成后一次,`agent_end` 之前 | 消息拼接;非空触发一轮额外调用 |++通过 `Agent(middleware=[...])` 传入。见+[Middleware → 组合规则](../guides/middleware/composition)。++## Checkpointer++任何匹配下面 Protocol 的对象就是 Checkpointer:++```python+class Checkpointer(Protocol):+ async def load(self, thread_id: str) -> CheckpointData | None: ...+ async def append(self, thread_id: str, messages: list[Message]) -> None: ...+ async def save_extra(self, thread_id: str, extra: dict) -> None: ...+```++通过 `Agent(checkpointer=cp, thread_id="…")` 绑定到 Agent,循环就会在+每条消息落定时追加一行,并在第一次 `prompt()` 时恢复历史。内置后端:+`MemoryCheckpointer`、`SQLiteCheckpointer`、`PostgresCheckpointer`、`MySQLCheckpointer`。+见 [Checkpointing → SQLite](../guides/checkpointing/sqlite)。++HITL 为跨进程挂起/恢复新增了两个可选方法:`save_pending_request` /+`load_pending_request`。所有第一方后���都已实现。见 [HITL 指南](../guides/hitl/overview)。++## HITL(人机协同)++CubePi 内置了 `cubepi.hitl` 模块,用于 agent 需要**暂停并等待人类输入**的+场景:++- **沙箱确认** —— 危险工具(bash、写入文件)在执行前需要人类+ approve / deny / edit。+- **运行中提问** —— agent 在运行中途向用户弹出一个结构化表单,等待回答。++```python+from cubepi.hitl import InMemoryChannel, ConfirmToolCallMiddleware, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=…,+ tools=[bash_tool, ask_user_tool(channel)],+ middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],+ channel=channel,+)+```++Channel 是一个可 `await` 的协程协作者:工具和中间件作者写+`await channel.ask(...)` 或 `await channel.confirm(...)`,channel+处理暂停。宿主代码(你的 web 应用 / TUI)订阅 `channel.subscribe()` 或+轮询 `channel.pending`,把请求渲染给用户,然后通过+`channel.answer(qid, answer)` 回填答案。++内置两种 channel 后端:+- **`InMemoryChannel`** —— 单进程(CLI、notebook、测试)。+- **`CheckpointedChannel`** —— 跨进程(web 服务)。挂起的请求持久化到+ checkpointer;另一个进程可以在数小时后通过+ `Agent.respond(question_id=, answer=)` 回答。++完整细节——三种 HITL 动词、两套内置中间件、跨进程挂起/恢复协议、事件、+追踪 span 和错误参考——见 [HITL 指南](../guides/hitl/overview)。++## Tracer(可选)++`Tracer` 输出符合 [OpenTelemetry GenAI 语义约定](https://opentelemetry.io/docs/specs/semconv/gen-ai/)+的 span,任何 OTLP 后端(Jaeger、Tempo、Honeycomb、Datadog、AWS+X-Ray 等)都能直接接收,无需额外 instrumentation。先装 extra:++```bash+pip install "cubepi[tracing]" # OTel SDK+pip install "cubepi[tracing-otlp]" # + OTLP/HTTP 导出器+```++然后用 `async with` 包住 Agent:++```python+from cubepi.tracing import Tracer+from cubepi.tracing.exporters import JsonlSpanExporter++async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ tracer.attached(agent),+):+ await agent.prompt("…")+```++每次 run 会发出一个 `invoke_agent` 根 span,其下每轮 LLM 往返对应+一个 `cubepi.turn`,再嵌套 `chat`(CLIENT)和 `execute_tool` 子+span。**默认不记录任何 prompt 内容或模型输出** —— 需要的话用+`Tracer(record_content=True)` 显式打开,搭配 `redact` 回调脱敏。配+合 `Meter(...)` 还能拿到 token / 时延 / TTFC 直方图。完整指南:+[追踪 → 概览](../guides/tracing/overview)。++## 拼起来++```+用户代码+ │+ ▼+┌──────────────────────────────────────────┐+│ Agent │+│ ├─ AgentState (messages, tools, …) │+│ ├─ Middleware ── compose_middleware() │+│ ├─ Checkpointer ── message_end 时追加 │+│ └─ run_agent_loop ◀──── 真实的循环 │+│ │ │+│ ▼ │+│ Provider.stream() → MessageStream │+│ │ │+│ └─ events → emit → subscribers │+└──────────────────────────────────────────┘+```++这张图就是整个框架。文档站的其余部分,都是细节而已。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/installation.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/installation.mdnew file mode 100644index 0000000..1511562--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/installation.md@@ -0,0 +1,93 @@+---+title: 安装+description: "通过 pip 安装 CubePi。需要 Python 3.11+,支持 Linux、macOS 和 Windows。"+---++# 安装++CubePi 需要 **Python 3.11 或以上**。核心运行时只有三个依赖:`pydantic`、+`anthropic`、`openai`。可选功能(SQLite、Postgres、MCP、OpenTelemetry+追踪)通过 extras 按需安装,不用的话不会被拉进来。++## 使用 pip++```bash+pip install cubepi+```++可选 extras:++```bash+pip install "cubepi[sqlite]" # 安装 aiosqlite,启用 SQLiteCheckpointer+pip install "cubepi[postgres]" # 安装 asyncpg + sqlalchemy + msgpack+pip install "cubepi[mcp]" # 安装 MCP SDK,启用 MCP 工具加载器+pip install "cubepi[tracing]" # 安装 opentelemetry-sdk,启用 Tracer / Meter+pip install "cubepi[tracing-otlp]" # 加上 OTLP/HTTP 导出器+pip install "cubepi[sqlite,mcp,tracing]" # 组合+```++## 使用 uv++[`uv`](https://github.com/astral-sh/uv) 比 pip 快很多,是推荐的工作流:++```bash+uv add cubepi+uv add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+```++在已有 uv 项目里,改完 `pyproject.toml` 后 `uv sync` 会重新锁定环境。++## 使用 Poetry++```bash+poetry add cubepi+poetry add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+```++## 验证安装++```bash+python -c "import cubepi; print(cubepi.__doc__)"+# cubepi — Pythonic async-native agent framework.+```++如果报 `ImportError`,大概率是解释器版本低于 3.11——用 `python --version`+确认一下。++## 配置 provider 凭据++CubePi 的 provider 从构造函数参数读取凭据。大多数部署会从环境变量+取出来:++```python+import os+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider++anthropic = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+openai = OpenAIProvider(provider_id="openai", api_key=os.environ["OPENAI_API_KEY"])+```++你也可以传 `base_url=...` 指向自托管端点或兼容代理(如 Anthropic Bedrock、+LiteLLM、vLLM)。++[FauxProvider](../guides/providers/custom#using-fauxprovider-in-tests)+(用于测试)不需要任何凭据。++## extras 选择指南++| Extra | 拉入什么 | 什么时候装 |+|---|---|---|+| (无) | 仅核心 | 只需要内存里的状态,不用 MCP,不用追踪 |+| `[sqlite]` | `aiosqlite` | 单进程应用需要落盘 |+| `[postgres]` | `asyncpg`、`sqlalchemy`、`msgpack` | 多实例 / 生产环境——见 [Postgres 指南](../guides/checkpointing/postgres) |+| `[mcp]` | `mcp` | 想把 MCP server 工具挂到 Agent 上 |+| `[tracing]` | `opentelemetry-sdk` | 想要 OpenTelemetry 追踪(含可选指标)——见 [追踪指南](../guides/tracing/overview) |+| `[tracing-otlp]` | `opentelemetry-exporter-otlp-proto-http` | 把 trace 发往 OTLP/HTTP 后端(Jaeger ≥1.50、Tempo、Honeycomb、Datadog 等)|+| `[docs]` | `griffe` | 仅文档站构建(贡献者用) |++## 下一步++- [快速开始](./quick-start) —— 五分钟内跑通你的第一个 Agent。+- [核心概念](./core-concepts) —— 在动手前先弄清 `Agent` / `Tool` /+ `Provider` / `Checkpointer` 各自是什么。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/quick-start.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/quick-start.mdnew file mode 100644index 0000000..0b7924f--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/getting-started/quick-start.md@@ -0,0 +1,120 @@+---+title: 快速开始+description: "在几分钟内构建你的第一个 CubePi agent。"+---++# 快速开始++五分钟内跑通一个能流式输出、会调用工具的 Agent。我们写一个天气 Agent:+它把一个 Python 函数当工具调用、把 Claude 的回复逐字符流式打出来,+然后干净退出。++## 前置条件++- Python 3.11++- 已安装 `cubepi`(`pip install cubepi`)+- 环境变量中有 `ANTHROPIC_API_KEY`++## 完整脚本++存为 `weather_agent.py`:++```python title="weather_agent.py"+import asyncio+import os+from pydantic import BaseModel++from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # 真实应用里:调一个 HTTP 天气 API。这里直接返回一段假数据。+ return AgentToolResult(+ content=[TextContent(text=f"{params.city} 现在 72°F,晴")]+ )+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])++ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="你是一个简洁的天气助手。",+ tools=[+ AgentTool(+ name="get_weather",+ description="获取一个城市的当前天气",+ parameters=GetWeatherParams,+ execute=get_weather,+ ),+ ],+ )++ # 在 prompt() 之前订阅 —— 这是看到流式事件的关键。+ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "agent_end":+ print() # 末尾换行++ agent.subscribe(on_event)+ await agent.prompt("东京现在天气怎么样?")+++asyncio.run(main())+```++跑起来:++```bash+python weather_agent.py+```++你会看到 Claude 流式输出类似+*"东京当前天气 72°F,晴..."* 的句子,工具结果已经被串进去。++## 刚刚发生了什么++CubePi 跑了一个概念上长这样的循环:++1. `agent.prompt("东京现在天气怎么样?")` 把一条 `UserMessage` 入队,+ 然后调用模型。+2. 模型决定调用 `get_weather(city="Tokyo")`——CubePi 用你的 Pydantic+ 模型解析 JSON 参数,调用你的 `async def`,把结果作为+ `ToolResultMessage` 反馈回去。+3. 模型产生最终的 assistant 回复,以 `text_delta` 事件流的形式回来。+4. 循环发出 `agent_end` 然后返回。++`agent.subscribe(...)` 注册了一个回调,接收运行时发出的每一个事件:+`agent_start`、`turn_start`、`message_start`、`text_delta`、+`tool_execution_start`、`tool_execution_end`、`message_end`、+`turn_end`、`agent_end`。这个脚本只关心 `text_delta`,但你可以基于+任意其他事件渲染 UI。++## 调试之前请先读:常见困惑++- **必须在 prompt 之前订阅。** Listener 只会收到 `subscribe` 之后发出+ 的事件。先 prompt 再 subscribe,早期事件就丢了。+- **`provider.model(...)` 会把模型绑定到 provider。** provider 保存凭证和可选的 `provider_id` 元数据;模型 id 必须是这个 provider 支持的模型名。+- **`execute` 签名是固定的。** 哪怕你不用 keyword-only 参数,签名也得+ 保留 `(tool_call_id, params, *, signal, on_update)`。CubePi 总是+ 会传它们。+- **`agent.prompt()` 一次只能跑一个 prompt。** 跑的过程中,用+ [`agent.steer()`](../guides/agents/multi-turn) 插入修正,或+ `agent.follow_up()` 把后续消息排队。++## 下一步++- [核心概念](./core-concepts) —— `Agent`、`Tool`、`Provider`、`Stream`、+ `Middleware`、`Checkpointer` 背后的心智模型。+- [构建第一个 Agent](../guides/agents/first-agent) —— 工具、流式、错误+ 处理的完整走查。+- [工具使用与并行执行](../guides/agents/tool-use) —— 让 Agent 同时+ 扇出多个工具。+- [Recipes → Weather Agent](../recipes/weather-agent) —— 上面这个脚本+ 的加强版,真的会发 HTTP 请求。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/first-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/first-agent.mdnew file mode 100644index 0000000..a959a90--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/first-agent.md@@ -0,0 +1,180 @@+---+title: 第一个 Agent+description: "从零开始创建并运行你的第一个 CubePi agent。"+---++# 构建你的第一个 Agent++这是 [快速开始](../../getting-started/quick-start) 的"加长版"。我们+端到端构建一个单工具 Agent,然后逐层加上你后续大概率会想要的东西:+流式 UI、错误处理、取消按钮。++## 第 1 步 —— 配 provider 和 model++provider 是到 LLM API 的连接;`Model` 描述你想调哪个模型 + 一些能力+开关。++```python+import os+from cubepi import Model+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+model = Model(+ id="claude-sonnet-4-5-20250929",+ provider="anthropic",+ max_tokens=4096, # 回复 token 上限+ context_window=200_000, # 模型硬上限;通常默认就够+ temperature=0.7,+)+```++`Model.provider` 是一个字符串标签(比如 `"anthropic"`、`"openai"`)。+框架内部用来 clamp 思考等级和打 tag —— 在你的代码库里保持稳定即可,+不必和 provider 内部命名一致。++## 第 2 步 —— 声明一个工具++每个工具 = Pydantic 参数模型 + 一个异步 `execute` 函数:++```python+from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # 真实工作放在这里 —— 调 HTTP API、查 DB 等。+ return AgentToolResult(+ content=[TextContent(text=f"{params.city} 现在 72°F,晴")]+ )+++weather_tool = AgentTool(+ name="get_weather",+ description="获取一个城市的当前天气,返回简短文字。",+ parameters=GetWeatherParams,+ execute=get_weather,+)+```++注意几点:++- Pydantic 模型自动转成 JSON Schema,作为工具定义发给模型。+- `execute` 签名是固定的:`(tool_call_id, params, *, signal, on_update)`。+ 最后两个 keyword-only 参数一定会被传入 —— 哪怕你不用,也得在签名里。+- `signal` 是用户取消时会被 set 的 `asyncio.Event`。在长时间运行的+ 代码里检查它,及时退出。+- `on_update(partial)` 让你流式回报进度(见+ [工具使用](./tool-use))。++## 第 3 步 —— 组装 Agent++```python+from cubepi import Agent++agent = Agent(+ model=model,+ system_prompt="你是一个简洁的天气助手。",+ tools=[weather_tool],+)+```++不需要工具时传 `tools=[]`(或干脆省掉)。++## 第 4 步 —— 订阅事件++`agent.subscribe(listener)` 是观察运行过程的入口。Listener 会收到+每一个 `AgentEvent`:++```python+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "tool_execution_start":+ print(f"\n→ 调用 {event.tool_name}({event.args})")+ elif event.type == "tool_execution_end":+ print(f" ✓ 完成")++agent.subscribe(on_event)+```++可以注册多个 listener,它们都会收到所有事件。**在 `prompt()` 之前+订阅** —— 事件在 run 一开始就会发出。++## 第 5 步 —— prompt 然后跑++```python+import asyncio++async def main():+ await agent.prompt("东京天气怎么样?")++asyncio.run(main())+```++`agent.prompt()` 不返回任何值。结果存在 `agent.state.messages`+(完整历史)和 `agent.state.streaming_message`(当前正在产生的消息,+在两轮之间为 `None`)上。++## 加上错误处理++当 `provider.stream()` 抛异常时,Agent 循环仍然会产出一条+`AssistantMessage`,其 `stop_reason="error"`、`error_message` 已填好。+事件序列是:`message_start` → `message_end` → `turn_end` → `agent_end`。++两种处理方式:++1. 在 subscriber 里捕获 `event.type == "agent_end"`,看最后一条消息+ 的 `stop_reason`:++ ```python+ def on_event(event, signal=None):+ if event.type == "agent_end":+ last = event.messages[-1]+ if getattr(last, "stop_reason", "") == "error":+ print(f"\nerror: {last.error_message}")+ ```++2. 或者在 `await agent.prompt(...)` 返回之后,检查+ `agent.state.error_message`。++## 加一个"取消按钮"++`agent.abort()` 会 set run 级别的 signal。Provider 流短路成+`"aborted"`,正在运行的工具看到 `signal.is_set() == True`,+循环干净地发出 `agent_end`。++```python+async def main():+ task = asyncio.create_task(agent.prompt("搜索…"))+ await asyncio.sleep(0.5)+ agent.abort()+ await task # 一定会 return —— 不会抛异常+ await agent.wait_for_idle()+```++## 常见坑++- **`RuntimeError: Agent is already processing a prompt.`** ——+ 上一次 `prompt()` 没 await 完你又调了一次。用 `await+ agent.wait_for_idle()`,或用 `steer()` / `follow_up()` 排队。+- **没有 `text_delta` 事件** —— 是不是在 `prompt()` 之后才订阅的?+ Listener 只看得到注册之后的事件。+- **Tool not found** —— 模型调了一个 `name` 不在 `tools=[...]` 里的+ 工具。CubePi 把这种情况包装成一个 `is_error=True` 的工具结果,+ 不会崩 —— 在 `tool_execution_end` 事件的 `result` 里能看到。+- **Pydantic ValidationError 被吞掉了** —— 如果模型产出格式错误的+ JSON,CubePi 会把 validation error 也包成工具的 error result 喂+ 回去,模型通常下一轮自动纠正。++## 下一步++- [工具使用与并行执行](./tool-use) —— 同时多个工具、sequential 模式、+ `terminate`、增量进度。+- [流式事件](./streaming) —— 完整的事件分类。+- [多轮会话](./multi-turn) —— 跨轮保留状态、`steer`、`follow_up`、+ `resume`。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/multi-turn.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/multi-turn.mdnew file mode 100644index 0000000..8a23890--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/multi-turn.md@@ -0,0 +1,122 @@+---+title: 多轮对话+description: "使用 CubePi 的有状态 agent 循环和消息历史构建多轮对话 agent。"+---++# 多轮对话++在 CubePi 中,一个"轮次"(turn)是:用户输入 → 模型响应(可能包含工具调用)→ 可选的对工具结果的后续模型响应。agent 的 `_messages` 列表在多轮对话中不断增长。本指南介绍如何正确驱动多轮对话流程,以及如何在 agent 处于思考过程中注入输入。++## 基本模式++前一次调用返回后,再次调用 `prompt` 即可:++```python+await agent.prompt("Hi, my name is Sam.")+await agent.prompt("What's my name?")+# → "Your name is Sam."+```++历史消息保存在 `agent.state.messages` 中。CubePi 会追加每条用户消息、每条 assistant 消息以及每个工具结果。每次调用时 provider 都会收到完整的消息列表,因此 context window 的大小至关重要(参见下方的[上下文管理](#context-management))。++## 运行中修正:`agent.steer()`++有时用户希望在模型仍处于当前轮次时进行补充或修正。此时可使用 `steer()`:++```python+import asyncio++async def main():+ task = asyncio.create_task(agent.prompt("Plan a 5-day trip to Kyoto."))+ await asyncio.sleep(2)+ # User changed their mind:+ agent.steer(UserMessage(content=[TextContent(text="Make it 3 days, not 5.")]))+ await task+```++修正消息会被加入队列,循环在轮次之间(实际上是在一批工具调用与下一次模型调用之间)取出该消息。agent 会在下次响应前看到它,消息不会丢失。++`Agent` 上的 `steering_mode` 控制消息的排空方式:++- `"one-at-a-time"`(默认)—— 每个取出点只处理一条队列消息。+- `"all"` —— 一次排空所有队列消息。++## 排队后续消息:`agent.follow_up()`++`follow_up` 用于"当前运行结束后,以此内容开启新的轮次"。这是聊天 UI 的典型模式:用户在 assistant 还在响应时便开始输入。++```python+agent.follow_up(UserMessage(content=[TextContent(text="And what about Osaka?")]))+# When the current prompt() finishes, the loop picks this up+# automatically and starts a new turn.+```++如果调用 `follow_up` 时 agent 处于空闲状态,仍需触发一次运行——大多数应用会在 `prompt()` 返回后调用 `await agent.resume()` 来排空队列。++## `resume()` —— 从上次消息继续 {#resume--continue-from-the-last-message}++`resume()` 是"从上次中断处继续"的入口。有两种用途:++1. **从 checkpointer 加载后。** 状态中有消息但没有进行中的 prompt。`resume()` 会查看最后一条消息并采取行动:+ - assistant → 期待一条排队的 steer/follow_up 以转换为新的用户轮次;否则抛出异常。+ - tool_result → 用工具输出重新调用模型。+ - user → 用该用户消息重新调用模型。+2. **abort 之后。** 一旦 `agent.abort()` 完成清理,即可 resume。++```python+async with SQLiteCheckpointer("conv.db") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id="conv-1")+ await agent.prompt("hello") # loads existing history first+ await agent.prompt("how are you?")+```++实例化后的第一次 `prompt()` 会加载已有的 thread。后续调用只是追加。++## 上下文管理 {#context-management}++CubePi **不会**代替你截断或摘要上下文。每次轮次都会将完整消息列表发送给模型。几种应对策略:++- **手动截断** —— 实现一个 [`transform_context`](../middleware/hooks#transform_context) middleware,返回一个滑动窗口。+- **摘要 pass** —— 定期注入摘要消息,并通过 `transform_context` 丢弃旧消息。+- **自定义 `convert_to_llm`** —— 在序列化前(最后一个时机)重塑历史,而不修改 `agent.state.messages`。用户可见的历史保持完整,模型看到的则更少。++参见 [Middleware → Examples](../middleware/examples#sliding-window-truncation) 中的完整示例。++## 取消与等待空闲++```python+agent.abort() # signals the current run to stop+await agent.wait_for_idle() # awaits the run-cleanup+```++如果 agent 已经处于空闲状态,`wait_for_idle()` 是空操作。可以在任何地方安全调用。++## 从磁盘恢复状态++```python+from cubepi.checkpointer import SQLiteCheckpointer++async with SQLiteCheckpointer("conv.db") as cp:+ agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id="user-42",+ )+ # First prompt() restores the saved history if any.+ await agent.prompt("continue our chat")+```++`_extra` 槽(一个任意的 `dict[str, Any]`)也会被恢复。希望持久化 per-thread 状态的 middleware 应将数据写入 `context.extra`;checkpointer 的 `save_extra` 会在 `agent_end` 时被调用。++## 常见陷阱++- **在另一个 `prompt()` 进行中时调用 `prompt()`** 会抛出 `RuntimeError`。请改用 `steer()` 或 `follow_up()`,或先调用 `wait_for_idle()`。+- **`resume()` 时最后一条消息是 assistant 且队列为空** 会抛出 `"Cannot continue from message role: assistant"`。请先排队一条后续消息,或改用 `prompt()`。+- **历史无限增长** —— 若没有 `transform_context` middleware,最终会触达 context 限制。请尽早规划截断/摘要策略。+- **多个 agent 使用相同的 `thread_id`** —— 仅追加写入对顺序是安全的,但两个 agent 同时写入同一 thread 会导致消息交错。每个 thread 使用一个 agent 实例,或在应用层进行协调。++## 另请参阅++- [Streaming Events](./streaming) —— steering/follow_up 的精确事件顺序。+- [Checkpointing → SQLite](../checkpointing/sqlite) —— 持久化历史记录。+- [Recipes → Persistent Chat](../../recipes/persistent-chat) —— 带历史重载的完整多轮对话应用。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/streaming.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/streaming.mdnew file mode 100644index 0000000..d8451b6--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/streaming.md@@ -0,0 +1,152 @@+---+title: 流式输出+description: "通过 subscriber 和 MessageStream 实时流式接收 CubePi agent 的事件和 token。"+---++# 流式事件++CubePi 暴露两层流:++1. **Provider 流** —— `provider.stream(...)` 返回的 `MessageStream`,+ 产出 `StreamEvent` 描述原始线缆输出:文本 delta、思考 delta、+ 工具调用 delta,最后是 `done` 或 `error`。+2. **Agent 事件流** —— `agent.subscribe(...)` 注册的 listener 看到的+ 内容。十一种事件覆盖 `prompt()` 调用的完整生命周期,包括把 provider+ 事件包在 `MessageUpdateEvent` 里。++大部分应用代码只需要 Agent 事件流。++## 十一种 Agent 事件++| 事件 | 何时触发 |+|---|---|+| `agent_start` | `prompt()` / `resume()` 一开始 |+| `turn_start` | 每次调模型之前(一次 `prompt` 可能调多次) |+| `message_start` | 一条新消息(user / assistant / tool result)即将加入历史 |+| `message_update` | 每个 provider `StreamEvent`(deltas 等);`event.stream_event` 字段会带原始事件 |+| `message_end` | 一条消息已 finalize |+| `tool_execution_start` | 一个工具调用被派发(在并行 `asyncio.gather` 之前各发一次) |+| `tool_execution_update` | 一个工具通过 `on_update(...)` 报告了局部进度 |+| `tool_execution_end` | 一个工具结束(成功或失败) |+| `turn_end` | 当前批次的工具都尘埃落定,或一条无工具的 assistant 回复结束 |+| `agent_end` | 整个 `prompt()` 调用结束 —— 正常结束、abort、或 error 都会发 |++`MessageStartEvent` 和 `MessageEndEvent` 对 *每种* 消息都触发,不只是+assistant。User 消息和 tool result 消息也有。++## 工具调用一轮的事件顺序++典型 "用户提问 → 模型调一个工具 → 模型回答" 的事件序列:++```+agent_start+turn_start+ message_start (从 prompt 来的 UserMessage)+ message_end (UserMessage)+ message_start (空的 AssistantMessage partial)+ message_update × N (text_delta、toolcall_delta、…)+ message_end (finalize 后的 AssistantMessage)+ tool_execution_start+ tool_execution_end+ message_start (ToolResultMessage)+ message_end (ToolResultMessage)+turn_end+turn_start (循环带着工具结果再进模型)+ message_start+ message_update × N+ message_end+turn_end+agent_end+```++## 订阅++```python+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)++unsubscribe = agent.subscribe(on_event)+```++`agent.subscribe(...)` 永远不会收到 `event.type == "text_delta"`+的事件 —— 那是 *provider* 事件的 type。Agent 把每个 provider 事件+都包成 `MessageUpdateEvent`,原事件挂在 `event.stream_event` 上。+所以要同时匹配外层和内层。++Listener 可以是同步或异步,异步的会被 await。第二个参数是 run 级别+的 `asyncio.Event`(abort signal)—— 你可以查 `signal.is_set()` 判断+本次运行是否被取消。++要取消订阅,调用 `subscribe` 返回的那个函数。++## 过滤文本增量(最常见的用法)++```python+def on_event(event, signal=None):+ if event.type == "message_update":+ sub = event.stream_event+ if sub.type == "text_delta":+ print(sub.delta, end="", flush=True)+```++CubePi 保证的稳定结构是上面表格里那一种(`message_update.stream_event.delta`)。+代码里对内部类型做防御性判断。++## Provider 的 `StreamEvent` 类型++在 `MessageUpdateEvent.stream_event` 内部,type 字段告诉你模型正在+吐什么:++| `stream_event.type` | 含义 | 关键字段 |+|---|---|---|+| `start` | assistant 消息开始 | `partial` |+| `text_start` | 一个文本块开始 | `content_index` |+| `text_delta` | token 片段 | `delta` |+| `text_end` | 文本块结束 | `content_index` |+| `thinking_start` / `thinking_delta` / `thinking_end` | 扩展思考块 | `delta` |+| `toolcall_start` / `toolcall_delta` / `toolcall_end` | 工具调用的流式 JSON 参数 | `delta`(partial JSON) |+| `done` | 流正常结束 | — |+| `error` | 流出错 | `error_message` |++每个事件的 `partial` 字段是当前 `AssistantMessage` 的深拷贝快照,+方便 UI 不追 delta、每个事件直接重渲染。++## 直接迭代 provider 流++如果你完全跳过 `Agent`(罕见,通常意味着你在写测试或自定义编排),+直接迭代流即可:++```python+stream = await provider.stream(+ model=model,+ messages=[UserMessage(content=[TextContent(text="hello")])],+)+async for event in stream:+ if event.type == "text_delta":+ print(event.delta, end="", flush=True)+final = await stream.result() # 拿最终的 AssistantMessage+```++`stream.result()` 在迭代结束后也能调用 —— 这是拿最终消息的标准+方式。++## 常见坑++- **`prompt()` 之后才订阅** —— 早期事件已经发完了。先 subscribe,+ 再 prompt。+- **Listener 抛异常会让循环崩吗?** —— 不会,但异常会向上传播到下一个+ `await`。把风险代码包在 try/except 里。+- **并行工具的事件顺序** —— `tool_execution_start` 按模型发出顺序,+ `tool_execution_end` 按完成顺序。别依赖事件成对相邻。+- **`message_update` 触发太频繁** —— 高频 token 可能压垮慢消费者+ (比如 websocket)。消费端做批处理。+- **思考块会触发 `text_delta` 吗?** —— 不会,思考块发 `thinking_*`+ 事件。只想看可见文本就过滤 `event.stream_event.type`。++## 另请参阅++- [工具使用](./tool-use) —— 详解 `tool_execution_*` 三联事件。+- [多轮会话](./multi-turn) —— steering 和 resume 周边的事件顺序。+- [API Reference → StreamEvent](../../api/cubepi-providers#streamevent)+ 里有字段级 schema。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/tool-use.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/tool-use.mdnew file mode 100644index 0000000..ffe86eb--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/agents/tool-use.md@@ -0,0 +1,179 @@+---+title: 工具调用+description: "注册工具、并行或顺序执行、并通过 Pydantic 校验处理结果的完整指南。"+---++# 工具使用与并行执行++工具是 Agent 影响世界的方式。CubePi 把每个 `AgentTool` 转成模型可用+的 JSON Schema、用 Pydantic 校验参数、执行你的代码、把结果作为+`ToolResultMessage` 喂回。默认情况下,只要模型在一轮里发出多个工具+调用,它们就会并行执行。++## 工具的结构++```python+from pydantic import BaseModel, Field+from cubepi import AgentTool, AgentToolResult, TextContent+++class SearchParams(BaseModel):+ query: str = Field(..., description="自然语言查询")+ limit: int = Field(10, ge=1, le=100)+++async def search(tool_call_id, params: SearchParams, *, signal=None, on_update=None):+ results = await my_search_backend(params.query, params.limit)+ return AgentToolResult(+ content=[TextContent(text="\n".join(results))],+ details={"raw_results": results}, # 透传到 ToolResultMessage.details+ )+++search_tool = AgentTool(+ name="search",+ description="在内部知识库里搜索。",+ parameters=SearchParams,+ execute=search,+)+```++`description` 是直接展示给模型的 —— 给模型写,不是给人看。Pydantic+的 `Field(description=…)` 会进 JSON Schema,帮模型理解每个参数。++## 默认并行++模型在一条 assistant 消息里发多个工具调用时,CubePi 会用+`asyncio.create_task()` 调度它们并 gather。这通常就是你想要的。++```python+agent = Agent(+ model=model,+ tools=[search_tool, fetch_url_tool, summarise_tool],+)+```++事件流会先一次性发出所有 `tool_execution_start`,中间穿插每个工具+报告进度的 `tool_execution_update`,最后按完成顺序发+`tool_execution_end`。++## 强制顺序执行++两种方式:++1. **整个 agent 级别** —— `Agent(tool_execution="sequential")`。所有+ 工具批次按模型发出的顺序逐个执行。++2. **单个工具级别** —— 在 `AgentTool` 上设+ `execution_mode="sequential"`。一旦当前批次里 *任意* 一个工具是+ sequential,整个批次都退化为顺序执行。++ ```python+ write_db_tool = AgentTool(+ name="write_db",+ description="持久化一条记录。",+ parameters=WriteDbParams,+ execute=write_db,+ execution_mode="sequential", # 出于安全考虑放弃并行+ )+ ```++内置的 `ask_user` HITL 工具(见 [HITL 指南](../hitl/overview))设置了+`execution_mode="sequential"` —— 它会暂停 agent 等待人类输入,因此+工具批次会逐个运行。++工具会修改共享状态(DB、计数器)且你需要确定顺序时,选 sequential。++## 流式回报工具进度++长耗时工具可以推送增量更新,以 `tool_execution_update` 事件呈现:++```python+async def slow_search(tool_call_id, params, *, signal=None, on_update=None):+ for i, page in enumerate(await fetch_pages(params.query)):+ if signal and signal.is_set():+ break+ if on_update:+ on_update({"progress": i, "total": len(pages), "url": page.url})+ await process(page)+ return AgentToolResult(content=[TextContent(text="done")])+```++事件里的 `partial_result` 就是你传给 `on_update` 的对象。用小 dict 就好,+它不会进模型的 context。++## 取消正在跑的工具++`signal` 就是 `agent.abort()` set 的那个 `asyncio.Event`。在任何循环+里检查它:++```python+async def long_running(tool_call_id, params, *, signal=None, on_update=None):+ for chunk in big_dataset:+ if signal and signal.is_set():+ return AgentToolResult(content=[TextContent(text="cancelled")])+ await process_chunk(chunk)+```++如果工作是一个大 `await`,用 `asyncio.wait_for(..., timeout=…)` 包一下,+或调用底层库自己的取消方法。++## 返回错误++两种姿势:++1. **抛异常。** CubePi 捕获后转成 `is_error=True` 的 `AgentToolResult`,+ 异常字符串作为 `TextContent`。+2. **显式返回 `is_error=True`。** 适合你想给结构化错误体的场景:++ ```python+ return AgentToolResult(+ content=[TextContent(text="超出限流,60 秒后再试")],+ is_error=True,+ )+ ```++不管哪种方式,模型都会收到一个明确标记错误的工具结果,通常会自适应+(换参数重试、问用户等)。++## 从工具结束本轮:`terminate`++工具可以声明 *"这次之后,不要再循环到模型了。"* 设+`terminate=True`:++```python+async def submit_final_answer(tool_call_id, params, *, signal=None, on_update=None):+ save_answer(params.answer)+ return AgentToolResult(+ content=[TextContent(text="submitted")],+ terminate=True,+ )+```++CubePi 仅在当前批次中 *每个* 工具结果都是 `terminate=True` 时才终止。+然后循环发 `turn_end`、`agent_end`,退出。++## 常见坑++- **忘了 keyword-only 参数** —— 开发时 `execute(tool_call_id, params)`+ 能跑,但框架传 `signal=` 时会崩。签名一定保留+ `*, signal=None, on_update=None`。+- **`details` 塞太大** —— `details` 透传到 agent 事件里,但 **不会**+ 给模型看。除非你下游有消费者,否则别堆大 blob。+- **Pydantic 严格度的意外** —— `Field(..., min_length=1)` 让模型通过+ JSON Schema 看到约束 —— 约束有帮助,但模型仍然偶尔发坏 JSON。+ CubePi 把 `ValidationError` 转成工具的 error result,你不用自己包。+- **`tools=[]` 但模型还是想用工具** —— 一般是 system prompt 里提到了+ 工具。要么删掉提示,要么真的把工具给它。++## 另请参阅++- [流式事件](./streaming) —— `tool_execution_*` 事件如何嵌入到事件+ 分类体系。+- [Middleware → before_tool_call](../middleware/hooks#before_tool_call)+ 和 [after_tool_call](../middleware/hooks#after_tool_call) ——+ 拦截、策略、重试。+- [Recipes → Weather Agent](../../recipes/weather-agent) —— 一个真发+ HTTP 请求的工具,端到端。+- [MCP 加载](../mcp/loading) —— 一次性把一个 MCP server 的整套工具+ 拉下来。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/custom.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/custom.mdnew file mode 100644index 0000000..4aad50c--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/custom.md@@ -0,0 +1,177 @@+---+title: 自定义后端+description: "使用 Checkpointer protocol 为 CubePi 实现自定义 checkpointer 后端。"+---++# 自定义 Checkpointing 后端++`Checkpointer` protocol 只有三个 async 方法:++```python+class Checkpointer(Protocol):+ async def load(self, thread_id: str) -> CheckpointData | None: ...+ async def append(self, thread_id: str, messages: list[Message]) -> None: ...+ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None: ...+```++这就是全部契约。你可以为 Redis、DynamoDB、S3、文件系统、内存字典实现它——任何支持追加和列表操作的存储均可。++## agent 何时调用各方法++| 方法 | 调用时机 |+|---|---|+| `load(thread_id)` | agent 构造后第一次调用 `prompt()` 时,调用一次 |+| `append(thread_id, messages)` | 在 `message_end` 内,每次消息完成时调用 |+| `save_extra(thread_id, extra)` | 在 `agent_end` 时,携带当前的 `_extra` 字典调用 |++对未知的 thread,`load` 返回 `None`。若还没有该 thread,`append` 和 `save_extra` 不会做任何有意义的事;请在第一次调用时创建对应的记录行。++## 最小化 Redis 示例++```python+import json+from typing import Any+import redis.asyncio as aredis+from cubepi.checkpointer.base import CheckpointData+from cubepi.providers.base import AssistantMessage, Message, ToolResultMessage, UserMessage+++_ROLE_TO_CLS: dict[str, type[Message]] = {+ "user": UserMessage,+ "assistant": AssistantMessage,+ "tool_result": ToolResultMessage,+}+++class RedisCheckpointer:+ def __init__(self, redis_url: str, prefix: str = "cubepi:") -> None:+ self._url = redis_url+ self._prefix = prefix+ self._r: aredis.Redis | None = None++ async def __aenter__(self):+ self._r = aredis.from_url(self._url)+ return self++ async def __aexit__(self, *args):+ if self._r is not None:+ await self._r.aclose()+ self._r = None++ def _msgs_key(self, thread_id: str) -> str:+ return f"{self._prefix}msgs:{thread_id}"++ def _extra_key(self, thread_id: str) -> str:+ return f"{self._prefix}extra:{thread_id}"++ async def load(self, thread_id: str) -> CheckpointData | None:+ raw_msgs = await self._r.lrange(self._msgs_key(thread_id), 0, -1)+ raw_extra = await self._r.get(self._extra_key(thread_id))++ if not raw_msgs and raw_extra is None:+ return None++ messages: list[Message] = []+ for item in raw_msgs:+ data = json.loads(item)+ cls = _ROLE_TO_CLS.get(data.get("role"))+ if cls is not None:+ messages.append(cls.model_validate(data))++ extra: dict[str, Any] = json.loads(raw_extra) if raw_extra else {}+ return CheckpointData(messages=messages, extra=extra)++ async def append(self, thread_id: str, messages: list[Message]) -> None:+ if not messages:+ return+ pipe = self._r.pipeline()+ for m in messages:+ pipe.rpush(self._msgs_key(thread_id), json.dumps(m.model_dump(mode="json")))+ await pipe.execute()++ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None:+ # Merge-style: load existing, update, write back.+ raw = await self._r.get(self._extra_key(thread_id))+ existing = json.loads(raw) if raw else {}+ existing.update(extra)+ await self._r.set(self._extra_key(thread_id), json.dumps(existing))+```++用法:++```python+async with RedisCheckpointer("redis://localhost:6379") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id="user-42")+ await agent.prompt("hi")+```++## 需要遵守的不变量++1. **仅追加。** 不要修改过去的消息。agent 假定它追加的历史就是你在 `load` 中返回的内容。+2. **保持顺序。** `load` 按追加顺序返回消息。使用列表、排序键或序列列。+3. **`load` 幂等。** 对同一 thread 两次调用 `load` 应返回相同结果。(CubePi 只调用一次,但工具往往也需要调用。)+4. **`extra` 是合并语义。** 先调用 `save_extra({"a": 1})` 后调用 `save_extra({"b": 2})` 应得到 `{"a": 1, "b": 2}`,而非仅 `{"b": 2}`。agent 携带完整字典调用,但 middleware 会分多次写入。+5. **用 `model_validate` 重建消息。** 使用 role 判别符(`UserMessage` / `AssistantMessage` / `ToolResultMessage`)选择正确的类。++## 不使用 async context manager 的自定义后端++`Checkpointer` Protocol 不要求 `__aenter__` / `__aexit__`。内置 checkpointer 使用它是因为需要管理网络资源,但纯内存或本地文件后端可以省略:++```python+class FileCheckpointer:+ def __init__(self, dir_path: str) -> None:+ self._dir = pathlib.Path(dir_path)+ self._dir.mkdir(parents=True, exist_ok=True)++ async def load(self, thread_id: str) -> CheckpointData | None:+ ...+ async def append(self, thread_id: str, messages: list[Message]) -> None:+ ...+ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None:+ ...++# Pass directly:+agent = Agent(model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")+```++## 测试你的后端++使用 `FauxProvider` 的即插即用测试模式:++```python+from cubepi import Agent, Model+from cubepi.providers import FauxProvider, faux_assistant_message++async def test_roundtrip():+ cp = MyCheckpointer(…)+ provider = FauxProvider(provider_id="faux")+ provider.set_responses([faux_assistant_message("hello")])++ agent1 = Agent(model=provider.model("t"),+ checkpointer=cp, thread_id="t1")+ agent1.subscribe(lambda e, s=None: None)+ await agent1.prompt("hi")++ # Fresh agent, same thread — should restore history.+ provider2 = FauxProvider(provider_id="faux")+ provider2.set_responses([faux_assistant_message("hello again")])+ agent2 = Agent(model=provider2.model("t"),+ checkpointer=cp, thread_id="t1")+ agent2.subscribe(lambda e, s=None: None)+ await agent2.prompt("hi again")++ assert len(agent2.state.messages) == 4 # 2 user + 2 assistant+```++## 常见陷阱++- **修改返回的 `CheckpointData`** —— 要么在传入时深拷贝,要么在文档中说明 agent 拥有该列表的所有权。CubePi 内置实现会进行拷贝。+- **丢失 `metadata`** —— `model_dump(mode="json")` 会保留 `metadata`。若通过 `__dict__` 序列化则会丢失。+- **`save_extra` 合并的竞态问题** —— 读-改-写模式在并发写入时可能丢失数据。若有针对同一 thread 的并发写入者,请使用 SQL `JSONB ||` 或 Redis Lua 脚本。+- **忘记注册 tool result 的 role** —— 容易只映射 `"user"` 和 `"assistant"` 而忘记 `"tool_result"`。三者都需要。++## 另请参阅++- [`Checkpointer` Protocol API](../../api/cubepi-checkpointer) —— 完整签名。+- [SQLiteCheckpointer 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py) —— 完整参考实现。+- [PostgresCheckpointer 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py) —— 生产级参考实现。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/mysql.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/mysql.mdnew file mode 100644index 0000000..74d1f2b--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/mysql.md@@ -0,0 +1,162 @@+---+title: MySQL 检查点+description: "在 CubePi 中使用 MySQLCheckpointer 通过 MySQL 后端实现 agent 状态持久化。"+---++# MySQL Checkpointing++`MySQLCheckpointer` 是面向 MySQL 部署的生产级持久化后端。它与 [`PostgresCheckpointer`](./postgres) 保持一致:使用 `aiomysql` 连接池、`msgpack` 负载、可查询的 JSON `metadata` 列,以及由宿主管理、在连接时进行校验的 schema。每个 thread 的 `seq` 分配通过 `SELECT … FOR UPDATE` 行锁串行化,因此多个进程可以安全地向同一 `thread_id` 追加消息。++**需要 MySQL 8.0.13+**(支持 JSON 表达式默认值)及 InnoDB 引擎。++安装额外依赖:++```bash+pip install "cubepi[mysql]"+```++这会安装 `aiomysql`、`sqlalchemy` 和 `msgpack`。++## 基本用法++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import MySQLCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with MySQLCheckpointer("mysql://user:pass@host:3306/dbname") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("hello")+++asyncio.run(main())+```++连接通过 `mysql://user:pass@host:port/db` 格式的 DSN 配置。连接池大小:++```python+async with MySQLCheckpointer(+ "mysql://…",+ min_pool_size=2,+ max_pool_size=20,+) as cp:+ …+```++## Schema++Checkpointer 需要三张表:`cubepi_threads`、`cubepi_messages` 和 `cubepi_schema_version`。与 Postgres 一样(与 SQLite 不同),CubePi **不会为你创建这些表**——它会在 `__aenter__` 时验证这些表是否存在并具有预期的 `schema_version`。++若表不存在,会抛出 `CubepiSchemaUninitialized`;若版本与当前 CubePi 版本不匹配,会抛出 `CubepiSchemaMismatch`。生产数据库应由宿主应用的迁移系统(Alembic 等)管理,而非由第三方库管理。++### 通过 Alembic 初始化 Schema++CubePi 暴露了 SQLAlchemy 的 `MetaData`,供你的迁移脚本采用该 schema:++```python+# alembic/env.py+from cubepi.checkpointer.mysql import cubepi_metadata, EXPECTED_SCHEMA_VERSION++target_metadata = [my_app_metadata, cubepi_metadata]+```++然后自动生成一个 revision:++```bash+alembic revision --autogenerate -m "add cubepi checkpointer"+```++`cubepi_messages` 表使用 `KEY` 分区,SQLAlchemy 的自动生成无法表达这一点,因此需要使用提供的辅助函数手动追加分区子句和 schema 版本写入:++```python+# In a migration's upgrade():+from cubepi.checkpointer.mysql.alembic_helpers import (+ messages_partition_clause,+ write_schema_version_op,+)++def upgrade():+ op.create_table("cubepi_threads", ...)+ op.create_table(+ "cubepi_messages", ...,+ mysql_engine="InnoDB",+ )+ # KEY(thread_id) partitioning is not autogenerated — apply it explicitly:+ op.execute(+ "ALTER TABLE cubepi_messages " + messages_partition_clause()+ )+ op.create_table("cubepi_schema_version", ...)+ # write_schema_version_op() returns two ';'-separated statements+ # (DELETE then INSERT). MySQL/pymysql runs one statement per execute,+ # so split and execute each:+ for stmt in write_schema_version_op().split(";"):+ if stmt.strip():+ op.execute(stmt)+```++`write_schema_version_op()` 是幂等的:它会删除旧 CubePi 版本的记录行并插入当前版本。当 CubePi 之后更新 `EXPECTED_SCHEMA_VERSION` 时,生成新的 revision 并再次运行即可。++## 数据模型++```+cubepi_threads+ thread_id (PK) -- VARCHAR(255) utf8mb4_bin(大小写敏感)+ parent_thread_id -- 自引用外键,用于 fork+ forked_at_seq -- fork 时的 seq 编号+ extra -- JSON+ created_at / updated_at++cubepi_messages+ thread_id, seq -- 复合主键;按 KEY(thread_id) 分区为 64 个分区+ role -- "user" | "assistant" | "tool"+ metadata -- JSON(未索引,见下文)+ payload -- LONGBLOB(msgpack)+ created_at++cubepi_schema_version+ version (PK)+```++重要特性:++- **`(thread_id, seq)` 是消息的唯一标识。** `seq` 在每个 thread 内单调递增,在对 thread 行持有 `SELECT … FOR UPDATE` 行锁的情况下分配。同一 thread 的两个并发写入者可以干净地串行化。+- **`payload` 是 msgpack 编码的 `model.model_dump(mode="json")`。** CubePi 在读取时重建 Pydantic 模型。+- **`metadata` 是 JSON,可通过 MySQL JSON 函数查询。** 它**未**建立索引——MySQL 无法直接对 JSON 列建索引。若需要按 metadata 查询消息,请在宿主侧添加生成列索引。+- **消息按 `KEY(thread_id)` 分区为 64 个分区。** 分布均匀,无 per-thread 瓶颈。该表对 `cubepi_threads` **没有外键**(MySQL 禁止对分区表使用外键);完整性由 `append` 时的惰性 thread 行插入来保证。++## 与 Postgres 后端的差异++| | Postgres | MySQL |+|---|---|---|+| 驱动 | `asyncpg` | `aiomysql` |+| `seq` 锁 | `pg_advisory_xact_lock` | `SELECT … FOR UPDATE` |+| 消息分区 | `HASH(thread_id)` + FK | `KEY(thread_id)`,无 FK |+| `metadata` 索引 | GIN | 无(添加生成列) |+| Thread ID | `TEXT` | `VARCHAR(255) utf8mb4_bin` |+| 最低版本 | — | MySQL 8.0.13+,InnoDB |++## `save_extra` 语义++`save_extra` 执行浅层顶级合并,而非替换——与 Postgres 和 SQLite 的行为相同。先写入 `{"foo": 1}` 再写入 `{"bar": 2}` 后,结果为 `{"foo": 1, "bar": 2}`。(内部实现是在行锁保护下读取当前 `extra`,然后写入合并后的字典,而非使用 `JSON_MERGE_PATCH`——后者的 null 删除和深度合并语义与 `dict.update` 不同。)++## 常见陷阱++- **`CubepiSchemaUninitialized`** —— 数据库为空、迁移未执行,或 `cubepi_schema_version` 表结构有误。请先应用宿主的 alembic upgrade。+- **`CubepiSchemaMismatch`** —— 升级了 CubePi 但未生成新的迁移。生成迁移、应用后 CubePi 即可启动。+- **旧版 MySQL 上的 JSON 默认值错误** —— JSON 列使用 `DEFAULT (JSON_OBJECT())`,需要 MySQL 8.0.13+。+- **大小写不敏感的 thread ID 冲突** —— Thread ID 使用 `utf8mb4_bin` 排序规则,因此 `UserA` 和 `usera` 保持区分。若手写 DDL,请保持该排序规则。++## 另请参阅++- [Postgres Checkpointing](./postgres) —— Postgres 后端的对应文档。+- [SQLite Checkpointing](./sqlite) —— 单进程替代方案。+- [Custom Backends](./custom) —— Protocol 详情。+- [Package README](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/README.md) —— 与代码同处的完整宿主集成手册。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/postgres.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/postgres.mdnew file mode 100644index 0000000..eb39196--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/postgres.md@@ -0,0 +1,194 @@+---+title: Postgres 检查点+description: "使用 PostgresCheckpointer 实现生产级的 agent 状态持久化。"+---++# Postgres 检查点++`PostgresCheckpointer` 是生产级持久化后端。它使用 `asyncpg` 搭配连接池、+`msgpack` 编码 payload、以及每线程的 Postgres advisory lock,让多个进程+可以安全地写入同一个 `thread_id` 而不会互相干扰。++安装 extra:++```bash+pip install "cubepi[postgres]"+```++会拉入 `asyncpg`、`sqlalchemy` 和 `msgpack`。++## 基本用法++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import PostgresCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with PostgresCheckpointer("postgresql://user:pass@host/dbname") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("hello")+++asyncio.run(main())+```++DSN 接受 `asyncpg.create_pool(...)` 支持的任何格式。连接池大小:++```python+async with PostgresCheckpointer(+ "postgresql://…",+ min_pool_size=2,+ max_pool_size=20,+) as cp:+ …+```++## Schema++checkpointer 需要三张表:`cubepi_threads`、`cubepi_messages` 和+`cubepi_schema_version`。与 SQLite 不同,CubePi **不会自动创建这些表**——+它只在 `__aenter__` 时验证它们是否存在且 `schema_version` 是否匹配预期。++如果表不存在,你会收到 `CubepiSchemaUninitialized`。如果版本与本版+CubePi 不匹配,你会收到 `CubepiSchemaMismatch`。++原因:生产数据库属于宿主应用的迁移系统(Alembic、Atlas……),+不属于一个可能与你现有迁移冲突的第三方库。++### 通过 Alembic 引导 {#bootstrapping-via-alembic}++CubePi 暴露了 SQLAlchemy `MetaData`,让你的迁移可以采用其 schema:++```python+# alembic/env.py+from cubepi.checkpointer.postgres import cubepi_metadata, EXPECTED_SCHEMA_VERSION++target_metadata = [my_app_metadata, cubepi_metadata]+```++然后生成一个 revision 并执行。迁移还必须在 `cubepi_schema_version` 中+INSERT schema 版本。使用辅助函数:++```python+# 在迁移的 upgrade() 中:+from cubepi.checkpointer.postgres.alembic_helpers import (+ create_message_partitions_op,+ write_schema_version_op,+)++def upgrade():+ op.create_table(...) # 从 cubepi_metadata 自动生成+ op.execute(create_message_partitions_op()) # 创建 64 个哈希分区+ op.execute(write_schema_version_op()) # 记录 EXPECTED_SCHEMA_VERSION+```++两个辅助函数都返回 SQL 字符串——你需要传入 `op.execute(...)`。+`write_schema_version_op()` 是幂等的:它会删除之前 CubePi 版本的所有行,+然后插入当前版本。++当 CubePi 后续升级并提升了 `EXPECTED_SCHEMA_VERSION` 时,生成一个新的+revision,再次调用 `op.execute(write_schema_version_op())`。++## 数据模型++```+cubepi_threads+ thread_id (PK)+ parent_thread_id -- 用于 fork+ forked_at_seq -- fork 点处的序列号+ extra -- JSONB+ created_at / updated_at++cubepi_messages+ thread_id, seq -- 复合 PK;按 HASH(thread_id) 分为 64 个分区+ role -- "user" | "assistant" | "tool"+ metadata -- JSONB(通过 GIN 索引)+ payload -- bytea (msgpack)+ created_at++cubepi_schema_version+ version (PK)+```++重要属性:++- **`(thread_id, seq)` 是消息标识。** `seq` 在每线程中单调递增,+ 在 `pg_advisory_xact_lock(hashtext(thread_id))` 下分配。+ 两个对同一线程的并发写入者会干净地序列化。+- **`payload` 是 msgpack 编码的 `model.model_dump(mode="json")`。**+ CubePi 在读取时重建 Pydantic 模型。+- **`metadata` 是 JSONB,可查询。** 完整消息的 payload 内部也包含+ `metadata`,但这一列是 SQL 查询的规范视图。+- **表按 `HASH(thread_id)` 分为 64 个分区。** 跨分区均匀分布,+ 无每线程瓶颈。++## 并发++advisory lock 让同一线程上的追加操作跨进程安全:++```python+# 进程 A 和进程 B 同时追加到线程 "user-42"。+# 它们通过 pg_advisory_xact_lock 序列化,各自获得连续的 seq。+```++读取(`load`)不取锁——它们在事务内是快照一致性的。++默认连接池 `min=1, max=10` 对大多数应用足够;如果你的并发 agent 数+很高,请调大 `max_pool_size`。++## `save_extra` 语义++`save_extra` 做的是 JSONB 合并,而不是替换:++```sql+extra = cubepi_threads.extra || EXCLUDED.extra+```++所以先写 `{"foo": 1}` 再写 `{"bar": 2}` 会得到 `{"foo": 1, "bar": 2}`。+中间件可以安全地写入部分 dict 而不会丢失先前的键。++## Fork++`parent_thread_id` + `forked_at_seq` 列是为将来的 fork 支持预留的。+CubePi v0.3 尚未暴露 fork API——现在写入它们是为了保持 schema+的前向兼容性。++## 常见坑++- **`CubepiSchemaUninitialized`** —— 数据库为空或迁移未运行。+ 先执行宿主 alembic upgrade。+- **`CubepiSchemaMismatch`** —— 你升级了 CubePi 但未生成新的迁移。+ 生成一个、执行它,CubePi 就会启动。++ :::info Schema v2(HITL)++ CubePi ≥ HITL 版本将 `EXPECTED_SCHEMA_VERSION` 从 1 提升到 2,+ 并在 `cubepi_threads` 表中新增 `pending_request JSONB NULL` 列。+ 宿主 alembic upgrade 必须在提升 schema_version 行之前调用+ `add_pending_request_column_op()`(来自+ `cubepi.checkpointer.postgres.alembic_helpers`)。+ 完整的跨进程流程请参阅 [HITL 指南](../hitl/overview)。+ :::+- **负载下连接池耗尽** —— 默认 `max_pool_size=10`。+ 如果应用的并发 agent 数超过此值,请调大。+- **`asyncpg.exceptions.UndefinedTableError` 在 `__aenter__` 外部** ——+ 表示你在 `async with` 之外使用了 checkpointer。连接池尚未连接。+ 请用 context manager 包裹。+- **混用宿主 SQLAlchemy `MetaData`** —— CubePi 自带独立的+ `MetaData` 实例,正是为了能与你的应用模型共存而不冲突。+ 不要将它们合并到你的全局 metadata 中——分别传给 Alembic。++## 另请参阅++- [SQLite 检查点](./sqlite) —— 单进程替代方案。+- [自定义后端](./custom) —— Protocol 详情。+- [配方 → Postgres + FastAPI 服务](../../recipes/postgres-fastapi)+ —— 一个可部署的 HTTP 前端 agent。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/sqlite.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/sqlite.mdnew file mode 100644index 0000000..a893b22--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/checkpointing/sqlite.md@@ -0,0 +1,175 @@+---+title: SQLite 检查点+description: "使用 SQLiteCheckpointer 实现轻量级单进程 agent 状态持久化。"+---++# SQLite 检查点++`SQLiteCheckpointer` 是轻量级持久化后端:单个本地文件、无 server、+追加式消息日志。它是个人电脑、单进程应用、桌面工具,以及任何"单+进程独占会话"场景下的默认选择。++安装 extra:++```bash+pip install "cubepi[sqlite]"+```++会拉入 `aiosqlite`。++## 基本用法++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with SQLiteCheckpointer("agent.db") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("记一下:我最爱的颜色是青色。")++ # 之后再次启动脚本 —— 文件还在。+ await agent.prompt("我说过我最爱什么颜色?")+ # → "你说过是青色。"+++asyncio.run(main())+```++两件事必须记住:++1. **`thread_id`** 是会话标识符 —— 一般是用户 id 或 session id。+ 两个 Agent 用同一个 `thread_id` 共享同一段历史。+2. **`async with SQLiteCheckpointer(...)`** 是必需的:context manager+ 在 `__aenter__` 里打开连接、做一次性建表。不用 context manager+ 会抛 `AssertionError`。++## 会持久化什么++`SQLiteCheckpointer` 首次使用时建两张表:++```sql+CREATE TABLE messages (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ thread_id TEXT NOT NULL,+ message_json TEXT NOT NULL,+ created_at REAL NOT NULL DEFAULT (julianday('now'))+);++CREATE TABLE thread_extra (+ thread_id TEXT PRIMARY KEY,+ extra_json TEXT NOT NULL DEFAULT '{}'+);+```++- 每条 `UserMessage`、`AssistantMessage`、`ToolResultMessage` 各占+ `messages` 一行。JSON payload 通过 Pydantic `model_dump()` 生成。+- `AgentContext` 的 `extra` dict 在 `agent_end` 时持久化到 `thread_extra`。+ Middleware 需要持久化线程级状态时,往 `context.extra` 写。++这个 schema 是追加式的。CubePi 从不 update 或 delete 行。++## HITL 挂起表++当使用 [HITL](../hitl/overview) 模块时,每次 `__aenter__` 会自动创建一个额外的表:++```sql+CREATE TABLE IF NOT EXISTS thread_pending_request (+ thread_id TEXT PRIMARY KEY,+ request_json TEXT NOT NULL,+ created_at REAL NOT NULL DEFAULT (julianday('now'))+);+```++无需手动迁移 —— `CREATE TABLE IF NOT EXISTS` 是零等的。++## CubePi 什么时候读++构造 Agent 后的 **第一次** `prompt()`,CubePi 会调+`load(thread_id)`。如果线程存在,历史恢复到 `agent.state.messages`,+`extra` 恢复到 agent 私有的 `_extra` dict。++后续 `prompt()` 不再读取 —— 内存里的状态是权威。++这意味着:**不要让一个 `Agent` 实例跨进程共享**。进程 A 的内存状态+会和进程 B 的写入发散。++## 多线程隔离++```python+async with SQLiteCheckpointer("agent.db") as cp:+ alice = Agent(model=…, checkpointer=cp, thread_id="alice")+ bob = Agent(model=…, checkpointer=cp, thread_id="bob")+ # 每个调用只 load/append 自己的 thread。+```++你可以让多个用户共用一个 checkpointer —— `thread_id` 负责隔离。++## 并发模型++Checkpointer 内部对每次读写都用 `asyncio.Lock`。SQLite 本身允许+多进程写,但 CubePi 的假设是单 Agent 实例独占一个 thread。多进程+同时写同一个 `agent.db`:++- 读是安全的。+- 并发写 **不同** thread 是安全的。+- 并发写 **同一** thread 会交错 —— 你会看到两条 assistant 消息背靠背+ 之类的怪事。++如果你需要跨进程共享 thread 写入,请用 [Postgres](./postgres),+它对每个 thread 都用 advisory lock。++## 文件放哪里++生产环境用绝对路径:++```python+SQLiteCheckpointer("/var/lib/myapp/agent.db")+```++相对路径在 `__aenter__` 时按 `os.getcwd()` 解析。目录必须存在,+请预先创建。++## 备份与查看++文件就是普通 SQLite 数据库:++```bash+# 看一个 thread 的历史+sqlite3 agent.db "SELECT message_json FROM messages WHERE thread_id='user-42' ORDER BY id"++# 备份+cp agent.db agent.db.bak++# VACUUM 回收空间(可选 —— 文件大小随历史线性增长)+sqlite3 agent.db "VACUUM"+```++## 常见坑++- **没用 `async with`** —— `AssertionError: self._db is not None`。+ 一定要用 `async with` 包。+- **两个进程写同一个 thread** —— 交错历史。要么用 Postgres,要么在+ 应用层协调。+- **未启用 WAL 模式** —— CubePi 走默认 journal mode 以确保可移植性。+ 对单写多读应用,一次性 `sqlite3 agent.db "PRAGMA journal_mode=WAL"`+ 能显著提升并发读。+- **忘传 `thread_id`** —— 不传时,Agent 没有持久化绑定。checkpointer+ 会被静默忽略。一定要同时传两个。++## 另请参阅++- [Postgres 检查点](./postgres) —— 多实例部署。+- [自定义后端](./custom) —— 为 Redis、DynamoDB 等实现 Protocol。+- [Recipes → Persistent Chat](../../recipes/persistent-chat) —— 端到端+ 的 SQLite 应用。+- [Recipes → 可恢复的长任务](../../recipes/resumable-tasks) —— 工具+ 执行中途崩溃也能恢复的 Agent。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/durable.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/durable.mdnew file mode 100644index 0000000..c4b79b6--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/durable.md@@ -0,0 +1,87 @@+---+title: 跨进程与持久化+sidebar_position: 3+description: "用 CheckpointedChannel 在一个进程挂起 HITL 请求、在另一个进程恢复;持久化范围规则。"+---++# 跨进程流程与持久化++## 跨进程(web 服务)流程++```python+# ───── 进程 1:HTTP POST /chat ─────++async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")++ agent = Agent(+ model=…,+ tools=[bash_tool],+ middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],+ channel=channel, checkpointer=cp, thread_id="conv-42",+ )++ task = asyncio.create_task(agent.prompt("删除临时文件"))++ # 轮询挂起(或订阅 channel 用于 SSE 推送)+ for _ in range(1000):+ pending = channel.pending+ if pending is not None:+ break+ await asyncio.sleep(0.1)++ # 优雅挂起 — 持久化 assistant message + 未决 tool_calls,+ # pending_request 留在 DB, 发射 AgentSuspendedEvent.+ await agent.detach()+ await task+++# ───── 进程 2:HTTP POST /respond ─────++async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")++ agent = Agent(+ model=…,+ tools=[bash_tool],+ middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],+ channel=channel, checkpointer=cp, thread_id="conv-42",+ )++ await agent.respond(+ question_id=request.json["call_id"],+ answer=ApproveAnswer(decision="approve"),+ )+ # Bash 工具执行,模型收到 tool_result,产生下一个 assistant turn。+```++**用户关闭 tab 未回答时:**++```python+await agent.abort_pending(reason="用户关闭了 tab")+# Phase 1:发信号给进行中的 HITL await(如有),触发 HitlAborted。+# Phase 2:为未决的 tool_calls 追加合成 deny ToolResultMessage,+# 追加终止 AssistantMessage(stop_reason="aborted"),+# 清除持久化 pending, 发射 AgentAbortedEvent。+# 不调用模型。对话关闭。+```+++## 持久化范围++持久的跨进程恢复(进程死亡后仍能继续)在两个定义明确的安全暂停点支持:++1. **`before_tool_call` 确认门** —— 确认中间件在工具的 `execute()` body+ 运行*之前*调用 `channel.approve(...)`。此时不存在工具副作用。恢复时+ 重新进入循环,执行(可能被编辑过的)工具体或替换为合成 deny+ tool_result。+2. **`ask_user` 工具体** —— 其整个 `execute()` body 就是+ `return await channel.ask(...)`。恢复时不会重放任何内容。++**默认情况下,在 `execute()` 内将 HITL 与其他工作混合的自定义工具不+支持跨进程持久化。** 如果此类工具的进程在执行中途死亡,channel 调用+前运行的所有内容都会丢失。除非使用 `allow_inside_custom_tool=True`+构造 `CheckpointedChannel`,否则将抛出 `HitlDurabilityNotGuaranteed` —+调用者必须承认等幂性契约(工具体在这一点必须是纯 HITL 等待,前面没有+可观察的副作用)。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/middlewares.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/middlewares.mdnew file mode 100644index 0000000..4076a0f--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/middlewares.md@@ -0,0 +1,99 @@+---+title: 中间件与 ask_user+sidebar_position: 2+description: "用 ConfirmToolCallMiddleware / ApprovalPolicyMiddleware 把关工具调用,并用 ask_user 工具让模型发起结构化提问。"+---++# 中间件与 工具++## 内置中间件++### `ConfirmToolCallMiddleware`++"在集合中的工具名一律询问人类。"++```python+from cubepi.hitl import ConfirmToolCallMiddleware++agent = Agent(+ ...,+ middleware=[+ ConfirmToolCallMiddleware(+ channel,+ require_confirm={"bash", "write_file"},+ timeout_seconds=180,+ ),+ ],+)+```++`require_confirm` 选项:++| 值 | 行为 |+|---|---|+| `None`(默认) | 确认**所有**工具 |+| `set[str]` | 仅当 `tool_call.name` 在集合中时确认 |+| `Callable[[BeforeToolCallContext], bool]` | 自定义断言 —— 检查参数、上下文等 |++### `ApprovalPolicyMiddleware`++适用于需要**策略引擎**将工具调用分为 auto-allow、hard-deny 和+human-confirm 三类的宿主。++```python+from cubepi.hitl import Approve, ApprovalPolicyMiddleware, AskUser, Deny++def my_policy(ctx):+ if ctx.tool_call.name == "read_file":+ return Approve() # 直接放行+ if ctx.tool_call.name.startswith("dangerous_"):+ return Deny(reason="策略阻止") # 硬阻止,不询问人类+ return AskUser(timeout_seconds=180) # 人类确认++agent = Agent(+ ...,+ middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],+)+```++策略函数可以是同步或异步(可 `await`)。返回值:++| 返回 | 效果 |+|---|---|+| `Approve()` | 工具运行;channel 从未调用 |+| `Deny(reason)` | 工具阻塞;`hitl_trace["decision"]="policy_deny"` |+| `AskUser(timeout_seconds=..., details=...)` | 调用 channel;人类选择 approve/deny/edit |++## `ask_user` 内置工具++模型在需要用户结构化输入时调用的工具。工厂函数返回一个名为+`"ask_user"` 的 `AgentTool`,`execution_mode="sequential"`。++```python+from cubepi.hitl import ask_user_tool++agent = Agent(+ ...,+ tools=[bash_tool, ask_user_tool(channel)],+)+```++工具描述明确引导模型不要拿 `ask_user` 做自由文本澄清("对于自由文本+提问,直接用文本结束 turn——用户的下一条消息就是答案")。++取消和超时以 `tool_result.is_error=True` 体现,+`details["hitl"]["outcome"]="cancelled"` / `"timed_out"` —— 模型看到+干净的���误工具结果并能做出反应。其他 HITL 控制异常(HitlDetached、+HitlAborted)传播到 Agent 层,不暴露给模型。+++## `ask_user` 对比 end of turn++| 目标 | 用法 |+|---|---|+| 对用户的自由文本追问 | 直接用文本结束 turn——用户的下一条消息就是答案 |+| 结构化选择 (N 选一) | `ask_user` 工具带 `options` |+| 多选 ("任意选择") | `ask_user` 工具带 `multi_select=True` |+| "其他" 可自由文本输入 | `ask_user` 工具选项带 `allow_input=True` |+| 确认/编辑工具参数后才运行 | `ConfirmToolCallMiddleware` 或 `ApprovalPolicyMiddleware` |+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/overview.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/overview.mdnew file mode 100644index 0000000..5ceb0f6--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/overview.md@@ -0,0 +1,80 @@+---+title: 概览+sidebar_position: 1+description: "CubePi 人机协同:channel、三种动词(confirm、approve、ask)与超时。"+---++# 人机协同 (HITL)++CubePi 的 HITL channel 让 agent 能够**暂停并等待人类输入**后再继续。+它用一个统一原语覆盖两种典型模式:++1. **沙箱工具确认** —— 危险工具(bash、文件写入、API 变更)在运行前+ 需要人类 approve / deny / edit。+2. **运行中结构化提问** —— agent 需要用户做出选择或填写表单才能继续。++Channel 是一个可 `await` 的协程协作者。工具作者写+`await channel.ask(...)`,channel 处理暂停。宿主代码订阅挂起请求并回填+答案。两种后端覆盖全场景:++- `InMemoryChannel` —— CLI、notebook、测试。进程死亡,挂起丢失。+- `CheckpointedChannel` —— web 服务。将挂起请求持久化到 `Checkpointer`,+ 让不同进程(或重启后的同一进程)在数小时后拾起并回答。++## 三种动词++### `confirm(prompt, *, details, timeout, signal) → bool`++简单的 yes/no 问题。宿主回答 `True` 或 `False`。++### `approve(tool_name, tool_call_id, args, *, details, timeout, signal) → ApproveAnswer`++沙箱确认动词。返回一个 `ApproveAnswer`,三种决策:++| 决策 | 结果 |+|---|---|+| `"approve"` | 以原始参数运行工具 |+| `"deny"` | 阻塞工具;`tool_result.is_error=True`,`details["hitl"]["decision"]="human_deny"` |+| `"edit"` | 以编辑后的参数运行(会拿工具的 pydantic 参数模型重新校验) |++对 `approve` 请求,信封的 `question_id` 设为 LLM 的 `tool_call_id` —+没有独立的 UUID,所以宿主代码可以直接用它已经在工具流中追踪的同一个+ID 来关联。++### `ask(questions, *, timeout, signal) → dict[str, str | list[str]]`++一个包含一个或多个 `Question` 对象的结构化表单。每个问题可以是:++- **自由文本** (`options=None`)+- **单选** (`options=[...]`, `multi_select=False`)+- **多选** (`options=[...]`, `multi_select=True`)+- **"其他" 可输入**(选项有 `allow_input=True` —— 用户输入自由文本)++```python+from cubepi.hitl.types import Question, Option++answers = await channel.ask([+ Question(key="framework", prompt="选择框架?", options=[+ Option(label="React", value="react"),+ Option(label="Vue", value="vue"),+ Option(label="其他", value="other", allow_input=True),+ ]),+ Question(key="features", prompt="启用功能:", multi_select=True, options=[+ Option(label="认证", value="auth"),+ Option(label="支付", value="payments"),+ ]),+])+# answers == {"framework": "react", "features": ["auth", "payments"]}+```++## 超时++两个 channel 都在构造函数接受 `default_timeout`,每个动词接受一個+per-call 的 `timeout` kwarg(per-call 覆盖默认)。++超时到期从 agent 侧的 `await` 抛出 `HitlTimedOut(BaseException)`。+周围的工具或中间件将其转换为 `tool_result.is_error=True`,+`details["hitl"]["decision"]="timed_out"`,模型看到干净的拒绝结果+并能自然反应。信封的 `HitlRequest.timeout_seconds` 会自动填写,+前端可以渲染倒计时。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/reference.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/reference.mdnew file mode 100644index 0000000..20858c7--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/hitl/reference.md@@ -0,0 +1,95 @@+---+title: API、事件与参考+sidebar_position: 4+description: "CubePi HITL 参考:Agent API、事件、追踪 span、错误参考、测试辅助与架构说明。"+---++# API、事件与参考++## Agent API++| 属性/方法 | 签名 | 说明 |+|---|---|---|+| `agent.channel` | `HitlChannel \| None` | 绑定的 channel 或 `None` |+| `agent.in_flight_hitl_request` | `HitlRequest \| None` | channel 当前 pending(同步属性) |+| `load_pending_hitl_request()` | `async → HitlRequest \| None` | 从 checkpointer 读取 pending(detach 后也能用) |+| `detach()` | `async → None` | 发射 `AgentSuspendedEvent` 然后触发 `HitlDetached` |+| `respond(*, question_id=, answer=)` | `async → None` | 恢复挂起的运行 |+| `abort_pending(reason=)` | `async → None` | 关闭对话(两阶段:发信号 + 追加合成拒绝) |++## 事件++四个新事件在 agent 事件流上发射:++| 事件 | 何时发射 | 关键字段 |+|---|---|---|+| `HitlRequestEvent` | channel 收到新的 `confirm/approve/ask` | `request: HitlRequest` |+| `HitlAnswerEvent` | `channel.answer()` 或 `channel.cancel()` 触发 | `question_id`, `answer`, `cancelled`, `timed_out` |+| `AgentSuspendedEvent` | `agent.detach()` 被调用时 HITL pending | `pending_request: HitlRequest` |+| `AgentAbortedEvent` | `agent.abort_pending()` 关闭对话 | `reason: str` |++## 追踪 span++安装 `cubepi[tracing]` extra 后,每次 HITL await 都会包装在一个+OpenTelemetry span 中:++| Span 名称 | 属性 |+|---|---|+| `hitl.approve` | `hitl.tool_name`, `hitl.tool_call_id`, `hitl.outcome`, `hitl.from_resume`, `hitl.duration_seconds` |+| `hitl.confirm` | `hitl.question_id`, `hitl.outcome`, `hitl.duration_seconds` |+| `hitl.ask` | `hitl.question_id`, `hitl.outcome`, `hitl.duration_seconds` |++`hitl.outcome` 可以是:`approved`, `denied`, `edited`, `answered`,+`cancelled`, `timed_out`, `aborted`, `detached`。++追踪导入是懒式的 —— 未安装 `opentelemetry` 时,channel 静默回退到+无操作 span。++## 错误参考++| 异常 | 基类 | 含义 |+|---|---|---|+| `HitlCancelled(reason)` | `BaseException` | 宿主调了 `channel.cancel(qid)` |+| `HitlTimedOut(seconds)` | `BaseException` | per-call 或 channel 默认超时到期 |+| `HitlDetached` | `BaseException` | HITL await 期间调了 `agent.detach()` |+| `HitlAborted` | `BaseException` | `agent.abort_pending()` 向 agent 发信号 |+| `HitlConcurrencyError` | `Exception` | channel 已有 pending 时再次调用 `confirm/approve/ask` |+| `HitlStaleAnswer` | `Exception` | `channel.answer(qid)` 的 qid 与当前 pending 不匹配 |+| `HitlNoPendingRequest` | `Exception` | 调了 `agent.respond(…)` 但线程没有 `pending_request` |+| `HitlDurabilityNotGuaranteed` | `Exception` | 自定义工具调了 `CheckpointedChannel.ask()` 但没有 `allow_inside_custom_tool=True` |++`HitlControlException`(四个 `BaseException` 子类的父类)**故意**不被+`cubepi.agent.tools._prepare_tool_call` 和 `_execute_prepared` 中现有+的广泛 `except Exception:` 处理器捕获 —— 这模仿了 `asyncio.CancelledError`+的模式。+++## 测试辅助++```python+from cubepi.hitl.testing import ScriptedChannel, NoopChannel++# ScriptedChannel:预编程答案,按顺序消费+ch = ScriptedChannel(answers=[+ ApproveAnswer(decision="approve"),+ {"color": "red"}, # ask 答案+])+assert len(ch.history) == 2 # 所有见过的 HitlRequest++# NoopChannel:自动批准一切。用于测试中的 subagent。+ch = NoopChannel()+assert (await ch.approve("bash", "tc", {})).decision == "approve"+```++## 架构说明++- **每线程单一 pending。** Agent 循环是顺序的——每个 `thread_id` 最多+ 有一个 HITL 请求。并发 `confirm/approve/ask` 会抛 `HitlConcurrencyError`。+- **Prompt-cache 前缀不变量。** 暂停与恢复之间,消息列表仅通过在末尾+ 追加 tool-result 消息和下一条 assistant turn 来改变。不插入、重排或+ 修改先前的消息——否则会使 provider 端的 prompt cache 失效。+- **`question_id == tool_call_id` 适用于 approve 请求。** 无需别名或+ 映射——已从工具流中追踪 `call_id` 的宿主可直接传递。+- **恢复不重放。** 恢复时将答案预加载到 channel 并重新进入循环。最后一条+ assistant message 中未决的 tool call 决定了接下来执行什么。没有基于+ 节点的重放语义。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/auth.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/auth.mdnew file mode 100644index 0000000..6044539--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/auth.md@@ -0,0 +1,187 @@+---+title: MCP 服务器认证+description: "为 CubePi 中的 MCP 服务器配置认证——API 密钥、OAuth 及自定义请求头。"+---++# MCP 服务器认证++MCP 传输层本身不规定认证方案——由服务器自行决定。实践中,以下三种模式几乎覆盖所有场景:++1. **`Authorization` 请求头中的 Bearer token**(HTTP 传输)。+2. **任意自定义请求头**(HTTP 传输)。+3. **环境变量**(stdio 传输——服务器从自身进程环境中读取)。++本页逐一介绍每种模式。++## HTTP:Bearer token++这是托管 MCP 服务器(GitHub、Sentry、内部服务等)的主流认证模式。向 `load_mcp_tools_http` 传递 `Authorization` 请求头:++```python+import os+from cubepi.mcp import load_mcp_tools_http++tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": f"Bearer {os.environ['MCP_TOKEN']}"},+)+```++`headers` 会随传输层的**每一次**请求发送,包括后续的 `tools/call` 调用。不存在"先登录再调用"的步骤——token 随每个连接传递。++## HTTP:自定义请求头++部分服务器使用 API Key 请求头代替 Bearer token:++```python+tools = await load_mcp_tools_http(+ server_url="https://mcp.internal/sse",+ headers={+ "X-API-Key": os.environ["MCP_API_KEY"],+ "X-Tenant-Id": "acme-corp",+ },+)+```++也可按需组合:++```python+headers = {+ "Authorization": f"Bearer {token}",+ "X-Trace-Id": str(uuid.uuid4()),+}+```++## HTTP:短期 token / 刷新++CubePi 的加载器在加载时接受静态的 `headers` 字典。对于会过期的 token(OAuth、短 TTL 的 JWT),有两种处理方案:++### 方案 A——到期后重新加载++捕获错误,重新获取 token,然后重新加载工具:++```python+async def load_with_fresh_token():+ token = await fetch_token()+ return await load_mcp_tools_http(+ server_url="…",+ headers={"Authorization": f"Bearer {token}"},+ )++tools = await load_with_fresh_token()+```++适用于生命周期长于单次 agent 运行的 token。++### 方案 B——在工具内部封装刷新逻辑++自己构建 `AgentTool`,通过闭包持有刷新逻辑:++```python+from cubepi.mcp._adapter import make_mcp_agent_tool+from cubepi.mcp import load_mcp_tools_http++async def call_remote_with_refresh(tool_name, args):+ headers = {"Authorization": f"Bearer {await fetch_token()}"}+ # Re-implement the http_loader's call_remote with fresh headers each time+ from mcp.client.sse import sse_client+ from mcp import ClientSession+ async with sse_client(server_url, headers=headers, timeout=30) as streams:+ async with ClientSession(*streams) as session:+ await session.initialize()+ resp = await session.call_tool(tool_name, args)+ return _serialize_call_tool_response(resp)++# Use the adapter directly:+my_tool = make_mcp_agent_tool(+ name="…",+ description="…",+ input_schema={…},+ call_remote=call_remote_with_refresh,+)+```++仅适合 TTL 极短的 token。对于周期较长的 token,方案 A 更简洁。++## stdio:环境变量++stdio 服务器从自身进程环境中读取凭据。传入 `env` 字典即可:++```python+import os+from cubepi.mcp import load_mcp_tools_stdio++tools = await load_mcp_tools_stdio(+ command="npx",+ args=["-y", "@modelcontextprotocol/server-github"],+ env={+ "GITHUB_PERSONAL_ACCESS_TOKEN": os.environ["GH_TOKEN"],+ **os.environ, # inherit the rest+ },+)+```++如果省略 `env`,子进程会继承**全部**父进程环境变量(标准子进程行为)。如需从干净状态启动,请传入显式字典:++```python+env = {+ "PATH": os.environ["PATH"],+ "HOME": os.environ["HOME"],+ "GITHUB_PERSONAL_ACCESS_TOKEN": token,+}+```++## stdio:基于文件的凭据++当服务器从 `~/.config/...` 读取凭据时,可传入 `cwd` 确保路径解析一致,并依赖继承的环境变量:++```python+tools = await load_mcp_tools_stdio(+ command="/usr/local/bin/my-mcp",+ args=["--config", "config.yaml"],+ cwd="/etc/myapp",+)+```++## 多用户 / 多租户凭据++在多租户服务中,每次 agent 调用需要使用不同的凭据。请**按请求加载工具**:++```python+async def build_agent_for_user(user_id: str) -> Agent:+ token = await fetch_user_token(user_id)+ tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": f"Bearer {token}"},+ )+ return Agent(model=model, tools=tools)+```++不要跨用户缓存同一份 `tools` 列表——闭包在加载时已保留了各自的认证请求头。++## 审计 / 可观测性++在 [`before_tool_call`](../middleware/hooks#before_tool_call) middleware 中记录 MCP 调用日志。从 MCP 加载的工具在事件流中与手写工具完全一致,因此现有日志 middleware 开箱即用。++若要专门标记 MCP 工具,可检查合成参数模型的名称(前缀为 `MCP_`):++```python+class MCPAuditMiddleware(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ param_name = type(ctx.args).__name__+ if param_name.startswith("MCP_"):+ log.info("mcp_call", extra={"tool": ctx.tool_call.name})+ return None+```++## 常见问题++- **调用时 `401 Unauthorized`,但 `list_tools` 正常** —— 部分服务器对单个工具设置了权限。确认 token 拥有所有待用工具的 scope。+- **Token 泄露到日志中** —— 不要记录 `headers` 字典。尤其注意包含查询字符串凭据的 URL 出现在异常消息中的情况。+- **stdio 服务器静默失败** —— 服务器会将认证错误打印到自身的 stderr。添加 `stdout`/`stderr` 重定向,或使用 MCP SDK 的诊断日志。+- **每次请求都重新加载很慢** —— 按 `(user_id, token)` 对缓存已加载的工具。记住 token 有 TTL。++## 参见++- [加载 MCP 工具](./loading) —— 基础加载器 API。+- [Middleware → 示例 → 日志记录](../middleware/examples#structured-logging) —— 与 MCP 审计需求天然配合。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/loading.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/loading.mdnew file mode 100644index 0000000..3fc4d97--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/mcp/loading.md@@ -0,0 +1,133 @@+---+title: 加载 MCP 工具+description: "将 MCP 服务器中的工具加载到 CubePi agent,包括基于 SSE 的远程服务器。"+---++# 加载 MCP 工具++[Model Context Protocol](https://modelcontextprotocol.io) 定义了一套标准方式,让工具服务器向 agent 暴露能力。CubePi 内置两个加载器,可连接 MCP 服务器、枚举其工具,并将每个工具转换为标准的 `AgentTool`,直接传给 `Agent(tools=…)` 使用。++安装额外依赖:++```bash+pip install "cubepi[mcp]"+```++这将引入 `mcp` SDK。++## stdio 传输:本地子进程++适用于以本地进程方式运行的工具(如 npm 发布的服务器、Python 模块、内部 CLI 等):++```python+import asyncio+import sys+from cubepi import Agent, Model+from cubepi.mcp import load_mcp_tools_stdio+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ # Spawn a stdio MCP server and discover its tools.+ tools = await load_mcp_tools_stdio(+ command="npx",+ args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp/sandbox"],+ timeout=30.0,+ )++ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ tools=tools, # all server tools, ready to use+ )+ agent.subscribe(lambda e, s=None: None)+ await agent.prompt("List files in /tmp/sandbox.")+++asyncio.run(main())+```++`load_mcp_tools_stdio` 会连接服务器、调用 `list_tools`、断开连接,然后返回 `AgentTool` 列表。**每次工具调用都会启动一个新子进程**——v0.3 保持简单,不维护进程池。++参数说明:++| 参数 | 用途 |+|---|---|+| `command` | 可执行文件(如 `"npx"`、`sys.executable`、`/usr/bin/uvx`) |+| `args` | 服务器的 argv |+| `env` | 环境变量(可选) |+| `cwd` | 工作目录(可选) |+| `timeout` | `initialize` / `list` / `call` 各步骤的挂钟超时时间 |++## HTTP/SSE 传输:远程服务器++适用于托管的 MCP 服务器(Sentry、GitHub、内部服务等):++```python+from cubepi.mcp import load_mcp_tools_http++tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": "Bearer <token>"},+ timeout=30.0,+)+```++`load_mcp_tools_http` 使用 MCP SDK 的 SSE 客户端。模式与 stdio 相同:一次连接用于枚举,每次工具调用建立新连接。++HTTP 服务器常用的认证模式参见 [MCP 认证](./auth)。++## 返回值说明++返回列表中的每个元素都是一个 `AgentTool`:++- `name` —— MCP 工具名称。+- `description` —— 直接来自服务器(不做改写)。+- `parameters` —— 从 MCP `inputSchema` 合成的 Pydantic 模型(JSON Schema → Pydantic,通过 `pydantic.create_model`)。+- `execute` —— 通过相同传输层调用 `tools/call` 的闭包。++合成的 Pydantic 模型支持:`string`、`integer`、`number`、`boolean`、`array`、`object`(映射为 `dict[str, Any]`)以及枚举(通过 `Literal`)。顶层约束均会保留:`description`、`pattern`、`minLength`/`maxLength`、`minimum`/`maximum`(含 exclusive 变体)、`minItems`/`maxItems`。++## 将 MCP 工具与手写工具混用++两者类型相同,直接拼接列表即可:++```python+mcp_tools = await load_mcp_tools_stdio(command="…", args=[…])+my_tools = [weather_tool, search_tool]++agent = Agent(+ model=model,+ tools=my_tools + mcp_tools,+)+```++模型看到的是合并后的统一 JSON Schema;循环会将每次调用分发给对应的实现。++## 按次连接与复用连接++CubePi 每次 `execute` 调用都会建立新的传输连接。这样做:++- ✅ 简单——无需管理连接池的生命周期。+- ✅ 健壮——挂起的连接不会污染其他工具。+- ⚠️ 对于启动开销较大的 stdio 服务器较慢(`npx` 服务器每次调用可能增加数秒延迟)。++对于高吞吐量的 stdio 服务器,建议将其作为持久 HTTP 服务运行,并改用 `load_mcp_tools_http`。++## 图片与结构化内容++如果 MCP 工具返回图片内容块,CubePi 会将其映射为 `ImageContent` 并包含在 `AgentToolResult.content` 中。Anthropic provider 会将其作为工具结果中的图片块转发;OpenAI provider 目前会将其去除(wire 格式不支持携带图片的工具结果)。++如果服务器返回 `structuredContent`,它会暴露在 `AgentToolResult.details["structuredContent"]` 下——便于下游代码访问,但不会展示给模型。++## 常见问题++- **第一次调用时立即出现 `asyncio.TimeoutError`** —— 服务器未在 `timeout` 内完成 `initialize`。将 `timeout=60` 或更高;部分服务器初始化较重。+- **每次工具调用都很慢** —— stdio 子进程启动开销。将服务器改为 HTTP 运行,或编写自定义适配器以保持子进程常驻。+- **工具列表中缺少某些工具** —— 服务器未能广播它们。单独运行服务器并用 MCP CLI 手动调用 `list_tools` 进行排查。+- **Pydantic 校验拒绝模型输出** —— MCP `inputSchema` 中有模型不遵守的约束。可在服务器端放宽 schema,或添加 `before_tool_call` middleware 进行强制转换。++## 参见++- [MCP 认证](./auth) —— Bearer token、请求头、基于环境变量的凭据。+- [工具使用](../agents/tool-use) —— 工具(MCP 或其他)的分发机制。+- [`make_mcp_agent_tool` 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/_adapter.py) —— schema → Pydantic 适配器,如需自定义可参考。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.mdnew file mode 100644index 0000000..5b5bff5--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md@@ -0,0 +1,75 @@+---+title: 上下文压缩+description: "使用 CompactionMiddleware 总结较早轮次,同时保留完整 CubePi 历史。"+---++# 上下文压缩++`CompactionMiddleware` 让长对话保持在模型上下文窗口内,同时不删除 agent+历史。它把较早轮次总结进 `ctx.extra`,再把压缩后的视图发给模型:一条摘要+消息加最近消息。`agent.state.messages` 和 checkpointer 历史仍然完整。++## 基本设置++用便宜模型做摘要,用正常模型运行 agent:++```python+from cubepi import Agent+from cubepi.middleware import CompactionMiddleware++agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=checkpointer,+ thread_id="conv_123",+ middleware=[+ CompactionMiddleware(+ summary_model=summary_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+ ),+ ],+)+```++摘要调用使用 `Provider.generate(...)`,并设置 `temperature=0.0`、+`thinking="off"`、`max_output_tokens=max_summary_tokens`。++## 持久化内容++middleware 会向 `AgentContext.extra` 写入两个键:++- `compaction` —— 摘要状态,以及它覆盖的消息引用。+- `compaction_until_msg_index` —— 已总结到的历史边界。++绑定 checkpointer 时,CubePi 会在 `agent_end` 通过 `save_extra` 保存+`ctx.extra`,所以下一个进程可以带着已有摘要继续。如果消息引用与当前历史不再+匹配,middleware 会清除旧状态并重新开始,而不是发送无效摘要。++## 阈值选择++先用保守值:++```python+CompactionMiddleware(+ summary_model=cheap_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+)+```++如果模型上下文很大、希望减少摘要调用,可以提高+`max_tokens_before_compact`。如果最近工具输出或用户修正很重要,可以提高+`keep_recent_messages`。长时间研究或编码会话可提高 `max_summary_tokens`。++## 失败行为++如果摘要 provider 失败,CubePi 会记录 warning,并继续使用之前的压缩视图或+原始消息。agent 不会仅仅因为压缩刷新失败而失败。++## 什么时候不用++短任务、无状态 agent、或需要模型看到旧工具输出每个 token 的流程,不适合使用+compaction。这些场景里,简单的滑动窗口 `transform_context` hook 更容易推理。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/composition.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/composition.mdnew file mode 100644index 0000000..7cd66c7--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/composition.md@@ -0,0 +1,164 @@+---+title: 组合规则+description: "了解 CubePi 如何通过 per-hook 规则组合多个中间件。"+---++# 组合规则++当你传入多个中间件——`Agent(middleware=[m1, m2, m3])`——CubePi 按照+**每个 hook 各自的规则**进行组合。正确的理解方式是:每个 hook 采用+最适合其工作的组合规则,你不需要记忆 "before" 或 "after" 优先级猜测。++## 规则速览++| Hook | 规则 | 顺序重要吗? |+|---|---|---|+| `transform_context` | **链式**——每个看到上一个的输出 | 是 |+| `convert_to_llm` | **最后一个胜出** | 只有一个运行 |+| `transform_system_prompt` | **链式** | 是 |+| `before_tool_call` | **第一个 block 停止**;非 block 累积 | block 胜出;`edited_args` 最后写入者胜出;`hitl_trace` 合并 |+| `after_tool_call` | **后面覆盖前面** | 最后写入胜出 |+| `should_stop_after_turn` | **任一 True 即停止**(OR) | 否 |+| `after_model_response` | **带合并语义的链式** | 见下文 |+| `on_run_end` | **消息拼接**——非空触发一轮额外调用 | 否 |++## `transform_context` 和 `transform_system_prompt`++链式:`m1` 的输出成为 `m2` 的输入,`m2` 的输出成为 `m3` 的输入。适用于分层转换:++```python+agent = Agent(+ middleware=[+ SlidingWindow(max_messages=20), # m1: 丢弃最旧的+ InjectSummary(), # m2: 前置摘要块+ ],+)+```++`m2` 看到的是截断后的列表。用户可见的 `agent.state.messages` 不受影响——+中间件只改变模型接收到的内容。++## `convert_to_llm`++有意采用最后胜出:这是发送前的最终转换。多个所有者会打架;只选一个。+CubePi 强制**列表中的最后一个**实现了 `convert_to_llm` 的中间件运行。++如果你发现自己需要两个 `convert_to_llm` 中间件,将它们合并为一个+(调用点组合:写一个调用两者的中间件)。++## `before_tool_call`++第一个 `block=True` 短路其余中间件。**非 block 返回累积:**+`edited_args` 向下游传播(每个中间件看到的是前一个编辑后的形式),+`hitl_trace` 跨链合并(当被覆盖时,旧键在 `_chain` 下归档)。++用于按从最严格到最宽松的顺序链式组合策略层:++```python+agent = Agent(+ middleware=[+ RateLimiter(), # 配额不足时阻止+ SafetyFilter(), # 参数危险时阻止;可能编辑+ AuditLogger(), # 永不阻止;记录用于可观测性+ ],+)+```++如果 `RateLimiter` 返回 `block=True`,`SafetyFilter` 和 `AuditLogger`+的 `before_tool_call` 不会运行。如果 `SafetyFilter` 返回+`edited_args={"cmd": "rm /tmp/foo"}`,工具将使用编辑后的参数运行,+`AuditLogger` 通过重建的 `ctx.args` 看到它们。+`AuditLogger.after_tool_call` 仍然触发,因为那是另一个 hook。++## `after_tool_call`++每个中间件可以返回一个设置了部分字段的 `AfterToolCallResult`;+CubePi 合并它们,后面的结果在非 `None` 字段上覆盖前面的。完整结果:++```python+class AfterToolCallResult(BaseModel):+ content: list[Content] | None = None+ details: Any = None+ is_error: bool | None = None+ terminate: bool | None = None+```++模式:早期中间件添加丰富的 `details`,后期中间件为模型净化 `content`。+两者都运行;合并后的结果组合了一个的 `details` 和另一个的脱敏 `content`。++## `should_stop_after_turn`++任一中间件返回 `True` 即结束运行。链中其余中间件不会被执行。++```python+agent = Agent(+ middleware=[+ MaxTurns(10),+ BudgetCap(usd=0.5),+ FinalAnswerSentinel(), # 当 assistant 说 "FINAL ANSWER" 时停止+ ],+)+```++## `after_model_response`++带结构化合并的链式。每个中间件看到**当前的 response**(可能已被前面的+中间件替换),并返回一个 `TurnAction`:++- `response: AssistantMessage | None` —— 若非 None,则替换当前的 response+ 供下游中间件和循环最终持久化使用。+- `inject_messages: list[Message]` —— 跨整个链追加到单个列表中,+ 然后在下一轮之前添加到上下文中。+- `decision: "natural" | "stop" | "loop_to_model"` —— **最后一个中间件的值胜出**。++```python+agent = Agent(+ middleware=[+ ProfanityRedactor(), # 重写 response+ StructuredOutputValidator(), # 可能返回 decision="loop_to_model"+ EventLogger(), # 不改变 decision+ ],+)+```++如果 `StructuredOutputValidator` 返回 `decision="loop_to_model"` 而+`EventLogger` 返回 `decision="natural"`,循环看到的是 `"natural"`——+因为最后胜出。如果这不是你想要的,请重新排序。++## 混合中间件与构造函数可调用对象++`Agent(...)` 也接受显式的 hook 可调用对象(`convert_to_llm=…`、+`before_tool_call=…` 等)。当两者都存在时,**显式可调用对象胜出**:++```python+agent = Agent(+ middleware=[LoggingMiddleware()],+ before_tool_call=my_explicit_hook, # 覆盖中间件版本+)+```++一次性 hook 使用显式形式;当行为是一个连贯的包时使用中间件类。++## 关于 `Middleware` 基类的说明++基类 `Middleware` 中未实现的方法会抛出 `NotImplementedError`。+`compose_middleware` 通过对比基方法检测到这一点,并**只连接**中间件+实际覆盖的 hook。++```python+class JustTransform(Middleware):+ async def transform_context(self, messages, *, ctx, signal=None):+ return messages[-10:]+ # 没有其他 hook。CubePi 不会调用它们。+```++## `on_run_end`++所有返回非空列表的中间件都会贡献消息;消息拼接为单一列表,在额外模型轮次+之前注入。返回 `None` 或 `[]` 的中间件被跳过。由于所有中间件都贡献,顺序+不影响消息是否被注入——只影响注入列表内的相对顺序。++## 另请参阅++- [8 个 Hook](./hooks) —— 每个 hook 的作用及触发时机。+- [示例](./examples) —— 组合在实践中的应用。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/examples.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/examples.mdnew file mode 100644index 0000000..164c0db--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/examples.md@@ -0,0 +1,284 @@+---+title: 示例+description: "CubePi 中间件实用示例:速率限制、重试、结构化日志、上下文截断和 HITL。"+---++# 中间件示例++针对四种最常见需求的实用中间件:速率限制、重试、结构化日志和上下文截断。++## 速率限制 {#rate-limiting}++当用户超过配额时阻止工具调用。将 `before_tool_call` 与外部速率限制器+(令牌桶、Redis INCR……)结合。++```python+import time+from cubepi import Middleware+from cubepi.agent.types import BeforeToolCallResult+++class RateLimitMiddleware(Middleware):+ def __init__(self, max_calls_per_min: int) -> None:+ self.max = max_calls_per_min+ self._timestamps: list[float] = []++ async def before_tool_call(self, ctx, *, signal=None):+ now = time.monotonic()+ # 丢弃超过 60 秒的记录。+ self._timestamps = [t for t in self._timestamps if now - t < 60]+ if len(self._timestamps) >= self.max:+ return BeforeToolCallResult(+ block=True,+ reason=f"Rate limit: {self.max} tool calls/min exceeded. Try again shortly.",+ )+ self._timestamps.append(now)+ return None+```++使用:++```python+agent = Agent(model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])+```++当达到限制时,模型会看到一个工具结果说"Rate limit exceeded…"并通常会等待或询问用户。++## 带退避的重试 {#retries-with-backoff}++在 `after_tool_call` 中重试失败的工具调用。最多 N 次,指数退避,+仅针对临时错误。++```python+import asyncio+from cubepi import Middleware+from cubepi.agent.types import AfterToolCallResult+++class RetryMiddleware(Middleware):+ def __init__(self, max_retries: int = 3, base_delay: float = 0.5) -> None:+ self.max_retries = max_retries+ self.base_delay = base_delay++ async def after_tool_call(self, ctx, *, signal=None):+ if not ctx.is_error:+ return None++ # 按名称查找工具并重新执行,最多 max_retries 次。+ tool = next(+ (t for t in (ctx.context.tools or []) if t.name == ctx.tool_call.name),+ None,+ )+ if tool is None:+ return None++ for attempt in range(1, self.max_retries + 1):+ await asyncio.sleep(self.base_delay * (2 ** (attempt - 1)))+ try:+ new_result = await tool.execute(+ ctx.tool_call.id,+ ctx.args,+ signal=signal,+ on_update=None,+ )+ return AfterToolCallResult(+ content=new_result.content,+ details={"retried": attempt, "original_error": ctx.result.content},+ is_error=False,+ )+ except Exception:+ continue++ return None # 放弃——保留原始错误+```++谨慎组合:重试非幂等工具(写入、发送、删除)可能造成实际损害。+将这类工具标记为 `execution_mode="sequential"` 并基于 `ctx.tool_call.name` 跳过。++## 结构化日志 {#structured-logging}++记录每一次工具调用及其参数、时长和结果。将 `before_tool_call`(记录开始时间)+与 `after_tool_call`(记录结果)配对。将开始时间存储在 `ctx.context.extra` 中。++```python+import time, logging+from cubepi import Middleware++log = logging.getLogger("cubepi.tools")+++class ToolLoggingMiddleware(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ ctx.context.extra.setdefault("_tool_starts", {})[ctx.tool_call.id] = time.monotonic()+ return None++ async def after_tool_call(self, ctx, *, signal=None):+ started = ctx.context.extra.get("_tool_starts", {}).pop(ctx.tool_call.id, None)+ duration_ms = int((time.monotonic() - started) * 1000) if started else None+ log.info(+ "tool_call",+ extra={+ "tool_name": ctx.tool_call.name,+ "args": ctx.args.model_dump() if hasattr(ctx.args, "model_dump") else ctx.args,+ "is_error": ctx.is_error,+ "duration_ms": duration_ms,+ },+ )+ return None+```++`ctx.context.extra` 是存储每次运行状态的最佳位置,因为:++- 其他中间件可通过同一个 `ctx.context` 看到。+- Checkpointer 在 `agent_end` 时通过 `save_extra` 持久化。+- 新对话开始时(新的 `thread_id`)会自动重置。++## 滑动窗口截断 {#sliding-window-truncation}++通过仅保留最近的 N 条消息(加上 system prompt)来保持模型上下文边界:++```python+from cubepi import Middleware+++class SlidingWindow(Middleware):+ def __init__(self, max_messages: int = 20) -> None:+ self.max_messages = max_messages++ async def transform_context(self, messages, *, ctx, signal=None):+ if len(messages) <= self.max_messages:+ return messages+ return messages[-self.max_messages:]+```++`transform_context` 不接触 `agent.state.messages`——用户仍然看到完整历史。+模型只看到最后 N 条。++与注入被丢弃内容摘要的 `transform_system_prompt` 配合效果很好:++```python+class SummaryInjector(Middleware):+ async def transform_system_prompt(self, system_prompt, *, ctx, signal=None):+ summary = "Earlier in this conversation we discussed: …"+ return f"{system_prompt}\n\nContext: {summary}".strip()+```++## 最大轮次 / 预算上限++在达到最大轮次数或成本上限时硬停止 agent:++```python+class MaxTurns(Middleware):+ def __init__(self, max_turns: int) -> None:+ self.max_turns = max_turns+ self.turns = 0++ async def should_stop_after_turn(self, ctx):+ self.turns += 1+ return self.turns >= self.max_turns+++class BudgetCap(Middleware):+ def __init__(self, usd: float, model_cost) -> None:+ self.cap = usd+ self.cost = model_cost # cubepi.providers.ModelCost 或类似+ self.spent = 0.0++ async def should_stop_after_turn(self, ctx):+ m = ctx.message+ if m.usage:+ self.spent += (+ (m.usage.input_tokens / 1_000_000) * self.cost.input+ + (m.usage.output_tokens / 1_000_000) * self.cost.output+ )+ return self.spent >= self.cap+```++## 用 `after_model_response` 实现结构化输出++验证 JSON 输出,如果解析失败则重新提示:++```python+import json+from cubepi import Middleware+from cubepi.middleware.base import TurnAction+from cubepi.providers.base import TextContent, UserMessage+++class JSONOutputValidator(Middleware):+ def __init__(self, schema_cls) -> None:+ self.schema = schema_cls++ async def after_model_response(self, response, ctx, *, signal=None):+ text = "".join(+ c.text for c in response.content if isinstance(c, TextContent)+ )+ try:+ obj = json.loads(text)+ self.schema.model_validate(obj)+ return None # 有效——正常进行+ except Exception as e:+ return TurnAction(+ inject_messages=[+ UserMessage(content=[TextContent(text=f"Invalid output: {e}. Return valid JSON.")]),+ ],+ decision="loop_to_model",+ )+```++Agent 将跳过工具执行,并立即用上下文中的反馈消息重新提示模型。++## 人机协同工具确认++CubePi 在 `cubepi.hitl` 中内置了两个 HITL 中间件:++**`ConfirmToolCallMiddleware`** —— "对此工具始终询问人类":++```python+from cubepi.hitl import ConfirmToolCallMiddleware, InMemoryChannel++channel = InMemoryChannel()+agent = Agent(+ model=…,+ middleware=[+ ConfirmToolCallMiddleware(+ channel,+ require_confirm={"bash", "write_file"},+ ),+ ],+)+```++Agent 在每次 `bash` 或 `write_file` 调用时暂停,等待宿主调用+`channel.answer(qid, ApproveAnswer(decision="approve"))`。结果驱动工具:+`approve` 执行,`deny` 带原因阻止,`edit` 重新校验并运行编辑后的参数。++**`ApprovalPolicyMiddleware`** —— 适用于通过策略引擎对工具调用进行分类的宿主:++```python+from cubepi.hitl import Approve, ApprovalPolicyMiddleware, AskUser, Deny++def my_policy(ctx):+ if ctx.tool_call.name in ("read_file", "grep"):+ return Approve()+ if ctx.tool_call.name.startswith("dangerous_"):+ return Deny(reason="blocked")+ return AskUser(timeout_seconds=180)++agent = Agent(+ model=…,+ middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],+)+```++`Deny` 完全跳过 channel(硬阻止)。`AskUser` 触发 channel 的 approve 流程。+`Approve` 立即返回。++完整细节——超时语义、编辑语义、事件、追踪 span、跨进程挂起/恢复——+请参见 [HITL 指南](../hitl/overview)。++## 另请参阅++- [7 个 Hook](./hooks) —— 每个 hook 的精确语义。+- [组合规则](./composition) —— 多个中间件如何组合。+- [配方](../../recipes/weather-agent) —— 中间件在真实应用中的组合。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/hooks.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/hooks.mdnew file mode 100644index 0000000..a30d06c--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/hooks.md@@ -0,0 +1,241 @@+---+title: 8 个 Hook+description: "CubePi 的 8 个中间件 hook 参考——transform_context、before_tool_call、after_tool_call、on_run_end 等。"+---++# 8 个 Hook++`Middleware` 是一个最多包含八个可选异步方法的类。每个 hook 在 agent+循环中的精确位置触发。只实现你需要的——CubePi 只会连接你重写了的方法。++```python+from cubepi import Middleware++class MyMiddleware(Middleware):+ async def transform_context(self, messages, *, ctx, signal=None):+ ...+```++将实例传入 `Agent(middleware=[MyMiddleware(), …])`。++## `transform_context`++```python+async def transform_context(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+ signal=None,+) -> list[Message]:+ ...+```++**每次调用模型之前**触发,作用于完整的消息列表。`ctx` 是当前+`AgentContext`;使用 `ctx.extra` 存放每次运行或 checkpointer 可持久化的+middleware 状态。用于:++- 截断或总结以适配上下文窗口。+- 注入系统提醒(更好用 `transform_system_prompt`)。+- 动态添加或删除特定消息。++返回(可能是新的)列表。**不要修改输入**——返回一个新列表以免其他持有原引用的代码感到意外。++组合方式:链式——每个中间件看到上一个的输出。++## `convert_to_llm`++```python+async def convert_to_llm(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+) -> list[Message]:+ ...+```++**在序列化给 provider 之前**触发。这是调整 LLM 看到的最终内容的最后机会。用于:++- 将工具结果精简为纯文本。+- 为非多模态 provider 将图片内容替换为文字描述。+- 压缩较长的工具输出。++组合方式:**最后一个实现胜出**(非链式)。当多个中间件可能冲突而你希望只有一个所有者时使用。++## `transform_system_prompt`++```python+async def transform_system_prompt(+ self,+ system_prompt: str,+ *,+ ctx: AgentContext,+ signal=None,+) -> str:+ ...+```++**每次调用模型之前**触发,作用于 system prompt 字符串。用于:++- 注入运行时信息(当前时间、用户角色)。+- 组合模块化的 system prompt 片段。+- A/B 测试 prompt 变体。++组合方式:链式。++## `before_tool_call`++```python+async def before_tool_call(self, ctx: BeforeToolCallContext, *, signal=None) -> BeforeToolCallResult | None:+ ...+```++**每个工具调用时**触发,在参数校验之后、`tool.execute` 之前。+context 提供:++- `ctx.assistant_message` —— 发起调用的消息。+- `ctx.tool_call` —— `ToolCall` 块。+- `ctx.args` —— *校验后的* Pydantic 实例。+- `ctx.context` —— 完整的 `AgentContext`。++返回 `BeforeToolCallResult(block=True, reason="…")` 以短路——+CubePi 会将该原因作为工具结果返回,并标记 `is_error=True`。+返回 `None`(或无返回)则继续执行。++用于:权限控制、速率限制、dry-run 模式、沙箱、+人机协同确认(参见 [HITL 指南](../hitl/overview))。++组合方式:**第一个 `block=True` 短路**整个链。++## `after_tool_call`++```python+async def after_tool_call(self, ctx: AfterToolCallContext, *, signal=None) -> AfterToolCallResult | None:+ ...+```++**每个工具调用时**触发,在 `tool.execute` 返回(或抛出异常)之后。+context 额外提供:++- `ctx.result` —— execute 返回的 `AgentToolResult`。+- `ctx.is_error` —— 工具是否出错。++返回 `AfterToolCallResult(content=…, details=…, is_error=…, terminate=…)`+以覆盖结果的单个字段(任何 `None` 字段保持原值)。返回 `None` 则原样通过。++用于:编辑、重试、日志记录、结果转换。++组合方式:后面的覆盖前面的(返回值中的每个非 `None` 字段覆盖前一个值)。++## `should_stop_after_turn`++```python+async def should_stop_after_turn(self, ctx: ShouldStopAfterTurnContext) -> bool:+ ...+```++**在每个轮次边界**触发(任何工具批次之后)。返回 `True` 以结束本次运行,+不进行下一次模型调用。++用于:最大轮次限制、预算上限、应用定义的停止条件。++组合方式:**任意一个返回 `True` 即停止**(逻辑 OR 跨链)。++## `after_model_response`++```python+async def after_model_response(+ self,+ response: AssistantMessage,+ ctx: AgentContext,+ *,+ signal=None,+) -> TurnAction | None:+ ...+```++**在 assistant 消息落定后立即**触发,在 `message_end` 发出**之前**、+在任何工具调用分发**之前**。该 hook 返回一个 `TurnAction`:++```python+from cubepi.middleware.base import TurnAction+from cubepi.providers.base import UserMessage, TextContent++TurnAction(+ response=modified_message, # 替换消息;None 则保留原消息+ inject_messages=[UserMessage(...)], # 在下一轮之前追加的额外消息+ decision="natural", # "natural" | "stop" | "loop_to_model"+)+```++三个控制流旋钮:++- `decision="natural"`(默认)—— 正常进入工具执行 / 下一轮。+- `decision="stop"` —— 在发出 `turn_end` 和 `agent_end` 后结束运行。+ 不执行工具,不再调用模型。+- `decision="loop_to_model"` —— 跳过工具执行,立即重新调用模型(配合+ `inject_messages` 使用以先添加上下文)。++用于:响应审核、带重新提示的结构化输出验证、条件路由。++组合方式:链式——每个中间件看到前一个中间件的 `response`;+`inject_messages` 跨链拼接;最后一个中间件的 `decision` 胜出。++## `on_run_end`++```python+async def on_run_end(+ self,+ ctx: AgentContext,+ *,+ signal=None,+) -> list[Message] | None:+ ...+```++**每次 `prompt()` 调用结束时触发一次**——所有轮次和工具调用完成后、+`AgentEndEvent` 发出前。返回非空 `list[Message]` 会将这些消息注入上下文+并运行**一轮额外的模型调用**(run-end pass)。返回 `None` 或 `[]` 不做任何操作。++额外轮次只触发一次——循环内的 `_reflection_fired` 标志阻止注入轮次再次触发+`on_run_end`。++**何时触发:**+- 正常完成(所有轮次结束后循环自然 break)。+- `should_stop_after_turn` 返回 `True`。+- `after_model_response` 返回 `decision="stop"`。++**何时不触发:**+- run 以 `stop_reason` 为 `"error"` 或 `"aborted"` 结束。+- HITL 中断(`HitlDetached` / `HitlAborted`)——run 是暂停而非结束。++用于:run 结束后的记忆提炼、对话摘要、需要完整轮次上下文的审计日志。++组合方式:所有中间件返回的消息**拼接**为单一列表,一起注入额外模型轮次。++## 中间件的构成++一个中间件不需要实现每一个 hook。只覆盖你需要的即可;基类中未实现+的 hook 会抛出 `NotImplementedError`,但 `compose_middleware` 会自动跳过它们。++```python+from cubepi import Middleware++class MaxTurnsMiddleware(Middleware):+ def __init__(self, max_turns: int) -> None:+ self.max_turns = max_turns+ self.turns = 0++ async def should_stop_after_turn(self, ctx) -> bool:+ self.turns += 1+ return self.turns >= self.max_turns+++agent = Agent(model=…, middleware=[MaxTurnsMiddleware(5)])+```++## 另请参阅++- [组合规则](./composition) —— 多个中间件定义同一 hook 时的精确语义。+- [示例](./examples) —— 速率限制、日志、重试、滑动窗口上下文、run 结束后记忆的实用中间件。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/subagents.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/subagents.mdnew file mode 100644index 0000000..da95d07--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/subagents.md@@ -0,0 +1,99 @@+---+title: 子 Agent+description: "使用 SubagentMiddleware 把自包含任务委派给 CubePi 子 Agent。"+---++# 子 Agent++`SubagentMiddleware` 会添加一个 `subagent` 工具。模型调用该工具时,CubePi+创建一个临时子 `Agent`,运行一段自包含 prompt,并把子 agent 最终 assistant+文本作为工具结果返回。++适合父 agent 需要委派有边界的工作:研究、审查、抽取、聚焦实现等。++## 定义子 agent++```python+from cubepi import Agent+from cubepi.middleware import SubagentMiddleware, SubagentSpec++subagents = {+ "researcher": SubagentSpec(+ name="researcher",+ description="Researches a narrow question and returns concise notes.",+ system_prompt="You are a precise research assistant.",+ ),+ "reviewer": SubagentSpec(+ name="reviewer",+ description="Reviews code for bugs, regressions, and missing tests.",+ system_prompt="You are a rigorous senior code reviewer.",+ ),+}++model = provider.model("claude-sonnet-4-5-20250929")++agent = Agent(+ model=model,+ middleware=[+ SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ shared_tools=[web_search],+ ),+ ],+)+```++如果模型请求未知 `subagent_type`,CubePi 回退到 `general-purpose` 子 agent。+如果你没有定义它,middleware 会提供一个基础默认版本。++## 工具访问和 middleware 继承++子 agent 只获得你传入的工具:++- `shared_tools` 对所有子 agent 可用。+- `SubagentSpec.tools` 只对该类型子 agent 可用。+- `excluded_tool_names` 防止递归或 host 专用工具被共享。++用 `inherited_middleware` 传入所有子 agent 都应运行的 middleware。用+`SubagentSpec.middleware` 定义某个子 agent 类型专属行为。++## 把子事件流式传给 host++应用通常需要把子事件写入自己的 UI 或审计日志。传入 `event_mapper` 和可选+`event_handler`:++```python+def map_event(event):+ if event.type == "text_delta":+ return {"type": "subagent_text_delta", "delta": event.delta}+ return None++async def handle_event(agent_id, payload):+ await ui_stream.send({"agent_id": agent_id, **payload})++SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ event_mapper=map_event,+ event_handler=handle_event,+)+```++映射后的 payload 也会存入父工具结果的 `details["subagent_events"]`。++## Tracing 和 abort++通过 `tracer=...` 传入 `Tracer`,即可给每个子运行附加 tracing。嵌套子+agent span 共享父 trace,所以 `cubepi trace view <trace_id>` 会把父工具调用+和子运行一起渲染出来。++父运行的 abort signal 会转发给子 agent。如果父运行在子 agent 执行中被+abort,子 agent 也会被 abort。++## Prompt 约定++父模型在 `subagent` 工具调用里应发送自包含的 `prompt`。不要假设子 agent 能+看到父 agent 的完整隐藏上下文。prompt 里应包含目标、相关事实、约束和期望+输出格式。+diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/anthropic.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/anthropic.mdnew file mode 100644index 0000000..a50f96f--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/anthropic.md@@ -0,0 +1,159 @@+---+title: Anthropic+description: "通过 AnthropicProvider 在 CubePi 中使用 Claude 模型——支持思考、缓存和工具调用。"+---++# Anthropic Provider++`AnthropicProvider` 封装了官方 `anthropic` SDK 的 Messages API。+支持流式、扩展思考、prompt caching 和工具调用。++## 构造++```python+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(+ api_key="sk-ant-…", # 也可让 SDK 自己读 ANTHROPIC_API_KEY+ base_url=None, # 指向代理 / Bedrock 兼容端点+ cache_retention="short", # "short"(5 分钟,默认) | "long"(1 小时) | "none"+)+```++`api_key=None` 时 SDK 会从环境变量读取。++## `Model`++```python+provider = AnthropicProvider(provider_id="anthropic")+model = provider.model(+ "claude-sonnet-4-5-20250929",+ reasoning=True, # 启用思考等级(见下)+ max_tokens=8192, # 回复 token 上限+ context_window=200_000, # 模型硬上限+ temperature=0.7,+)+```++第一个参数就是你传给 SDK 的模型名。`provider_id` 是 CubePi 内部用的+自由标签 —— 保持稳定即可,需要时可以让 tracing 和错误信息显示一个+明确的来源标签。++## 扩展思考++CubePi 把 `ThinkingLevel` 枚举映射到 Anthropic 的 `budget_tokens`:++| Level | 默认 budget |+|---|---|+| `"off"` | 关闭思考 |+| `"minimal"` | 1024 |+| `"low"` | 2048 |+| `"medium"` | 8192 |+| `"high"` | 16384 |+| `"xhigh"` | clamp 到 `"high"` |++在 Agent 上设置:++```python+agent = Agent(model=model, thinking="medium")+```++要自定义 budget,可以通过自己的 `on_payload` hook 传入+`StreamOptions(thinking_budgets=ThinkingBudgets(low=4096, medium=12288))`。++思考打开时,CubePi **会把 `temperature` 字段省掉** —— 因为 Anthropic+API 在扩展思考模式下不接受非默认的 temperature([兼容性文档](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#feature-compatibility))。+关闭思考时再按 `Model.temperature` 走;CubePi 自己处理切换。++思考内容以 `thinking_start` / `thinking_delta` / `thinking_end` 事件+流式产出,最终作为 `ThinkingContent` 块保存在 `AssistantMessage.content`+里 —— 后续轮次也会带着,以保证模型的思路连贯性。++## Prompt caching++默认情况下,provider 在每个请求上插入三个缓存断点:++- **system prompt**(最稳定)。+- **最后一个工具定义**(变化少)。+- **最后一条消息**(每轮往前推,把之前的历史全部缓存)。++缓存保留期默认 `"short"`(5 分钟,免费)。如果你的回合间隔较长,+切到 `"long"`:++```python+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="long") # 1 小时 TTL+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="none") # 完全禁用+```++每个 `AssistantMessage` 上的 `Usage` 对象会报告 `cache_read_tokens`+和 `cache_write_tokens`,方便你看命中率。++需要自定义缓存策略(比如换个断点策略)?实现 `CacheMarkerPolicy`+Protocol 然后 `cache_policy=…` 传入。默认策略类位于+`cubepi.providers.anthropic.DefaultCacheMarkerPolicy`。++## 用 `on_payload` 自定义请求++`on_payload` 让你在请求发出前查看 / 替换 request dict:++```python+async def my_payload(payload, model):+ payload.setdefault("metadata", {})["user_id"] = "u-42"+ return payload # 返回 None 或不返回保留原 payload++agent = Agent(model=model, on_payload=my_payload)+```++典型用法:加 `metadata.user_id`(计费)、强制 beta header,或留一份+调试面板用的 payload 副本。++## 用 `on_response` 自定义响应处理++`on_response` 在收到 HTTP 响应(状态、头)后、流式开始前触发:++```python+async def my_response(resp, model):+ if resp.status >= 400:+ logger.warning("bad status %s", resp.status)+ rate = resp.headers.get("anthropic-ratelimit-requests-remaining")+ if rate is not None:+ metrics.gauge("rate_remaining", int(rate))++agent = Agent(model=model, on_response=my_response)+```++两个回调都可以是同步或异步。++## 指向 Bedrock / Vertex / 代理++Anthropic SDK 接受 `base_url`,CubePi 透传:++```python+provider = AnthropicProvider(+ api_key="…",+ base_url="https://my-litellm.internal/v1",+)+```++对于 Bedrock,使用 `anthropic-bedrock` 适配器、通过 [自定义 provider](./custom)+注入。++## 常见坑++- **`temperature` 被忽略** —— 预期之内。思考打开时 CubePi 故意 drop+ 掉,这是 API 约束,不是 bug。+- **`xhigh` 和 `high` 看起来一样** —— Anthropic 没有更高一档的 budget,+ 所以 CubePi 把 `xhigh` clamp 到 `high`,token budget 相同。+- **缓存未命中** —— 缓存按 (内容, ttl) 索引。改 system prompt 会让+ 整块失效;改工具列表则从工具往后失效。要最大化命中,保持这两个+ 跨轮稳定。+- **`anthropic.RateLimitError`** —— 会作为 stream error 事件传出,+ 错误消息来自 SDK 的 `str(exc)`。在 `agent_end` 里捕获,决定是否+ 重试。++## 另请参阅++- [OpenAI Provider](./openai) —— 同 Protocol,不同形态。+- [自定义 Provider](./custom) —— 把不内建的 API 接进来。+- [Recipes → 多 Provider 容错](../../recipes/multi-provider-failover)+ —— Anthropic 故障时切到 OpenAI。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/custom.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/custom.mdnew file mode 100644index 0000000..6463d87--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/custom.md@@ -0,0 +1,209 @@+---+title: 自定义 Provider+description: "通过实现 Provider 协议,为 CubePi 编写自定义 provider。"+---++# 自定义 Provider++provider 是任意实现了以下单个方法的类:++```python+class Provider(Protocol):+ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream: ...+```++这就是完整的接口。实现它,`Agent(model=…)` 就可以接受你的类。++本页涵盖两种场景:++1. **新的真实 provider** —— Bedrock、Vertex、Replicate、内部 LLM 网关……+2. **`FauxProvider`** —— 内置的确定性 provider,是单元测试的利器。++## 最小化真实 provider++模式:创建一个 `MessageStream`,启动一个向其推送事件的生产者任务,然后立即返回 stream。++```python+import asyncio+import time+from cubepi.providers.base import (+ AssistantMessage,+ Message,+ MessageStream,+ Model,+ StreamEvent,+ StreamOptions,+ TextContent,+ ToolDefinition,+ Usage,+)+++class MyProvider:+ def __init__(self, *, api_key: str) -> None:+ self._api_key = api_key++ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream:+ opts = options or StreamOptions()+ ms = MessageStream()++ async def _produce():+ try:+ partial = AssistantMessage(+ content=[TextContent(text="")],+ usage=Usage(),+ timestamp=time.time(),+ provider_id=model.provider_id,+ model_id=model.id,+ )+ ms.push(StreamEvent(type="start", partial=partial.model_copy(deep=True)))++ # Call your backend. Stream tokens:+ async for token in call_my_backend(messages, model.id, signal=opts.signal):+ if opts.signal and opts.signal.is_set():+ ms.push(StreamEvent(type="error", error_message="aborted"))+ ms.set_result(partial.model_copy(update={"stop_reason": "aborted"}))+ return+ partial.content[-1] = TextContent(text=partial.content[-1].text + token)+ ms.push(StreamEvent(+ type="text_delta",+ delta=token,+ partial=partial.model_copy(deep=True),+ ))++ ms.push(StreamEvent(type="done"))+ ms.set_result(partial)++ except Exception as exc:+ error_msg = AssistantMessage(+ content=[],+ stop_reason="error",+ error_message=str(exc),+ usage=Usage(),+ timestamp=time.time(),+ )+ ms.push(StreamEvent(type="error", error_message=str(exc)))+ ms.set_result(error_msg)++ ms.attach_task(asyncio.create_task(_produce()))+ return ms+```++需要注意的要点:++1. **始终先推送 `start` 事件。** 订阅者依赖它进行 UI 初始化。+2. **始终以 `done` 或 `error` 结束。** agent 循环会等待 `MessageStream` 直到收到其中之一。+3. **始终调用 `ms.set_result(...)`**,以便 `await stream.result()` 能够完成。即使出错也需调用。+4. **如果生产者是独立任务,`ms.attach_task(...)` 是必须的** —— 它将任务的异常状态接入 stream,使崩溃表现为 `error` 而非挂起。+5. **遵守 `opts.signal`。** 在读取循环内部检查它;发出 `aborted` stop_reason,让 agent 能够干净地关闭。++## 支持工具调用++如果你的模型会产生工具调用,在流式传输时将 `ToolCall` 块追加到 `partial.content`,并发出 `toolcall_start` / `toolcall_delta` / `toolcall_end` 事件:++```python+from cubepi.providers.base import ToolCall++tc = ToolCall(id=block_id, name=tool_name, arguments={})+partial.content.append(tc)+ms.push(StreamEvent(type="toolcall_start", content_index=len(partial.content)-1,+ partial=partial.model_copy(deep=True)))+# ... as JSON args arrive:+ms.push(StreamEvent(type="toolcall_delta", delta=partial_json_chunk, …))+# ... on completion:+# replace tc.arguments with the parsed dict, push toolcall_end+```++CubePi 的 agent 循环会在收到 `done` 事件后自动分发工具调用。++## 挂钩 `on_payload` / `on_response`++如果你的 provider 发送 HTTP 请求,请调用 `cubepi.providers.base` 中的辅助函数:++```python+from cubepi.providers.base import (+ ProviderResponse,+ invoke_on_payload,+ invoke_on_response,+)++payload = await invoke_on_payload(opts.on_payload, payload, model)+http_resp = await self._client.post(..., json=payload)+await invoke_on_response(+ opts.on_response,+ ProviderResponse(status=http_resp.status_code, headers=dict(http_resp.headers)),+ model,+)+```++这与内置 provider 的做法一致;你的用户可以免费获得相同的检查点。++## 在测试中使用 `FauxProvider` {#using-fauxprovider-in-tests}++CubePi 内置 `FauxProvider`,用于确定性测试——无网络调用,无不稳定性,且有真实的流式事件:++```python+from cubepi import Agent, Model+from cubepi.providers import FauxProvider, faux_assistant_message, faux_text, faux_tool_call+++def test_my_agent():+ provider = FauxProvider(provider_id="faux")+ provider.set_responses([+ faux_assistant_message([+ faux_tool_call("search", {"query": "python"}),+ ]),+ faux_assistant_message("Here are the results: …"),+ ])++ agent = Agent(+ model=provider.model("test"),+ tools=[my_search_tool],+ )+ events = []+ agent.subscribe(lambda e, signal=None: events.append(e))+ await agent.prompt("Search for python")++ assert any(e.type == "tool_execution_start" for e in events)+ assert events[-1].type == "agent_end"+```++`set_responses` 是一个 FIFO 队列:每次模型调用弹出一个响应。faux provider 以真实的增量方式(逐 token)回放,因此你的流式代码路径会被真正执行。++辅助函数:++- `faux_text("Hello!")` —— 将字符串包装为 `TextContent` 块。+- `faux_thinking("Pondering…")` —— 一个 `ThinkingContent` 块。+- `faux_tool_call("name", {"arg": …})` —— 一个 `ToolCall` 块。+- `faux_assistant_message(content_or_text)` —— 构建完整的 `AssistantMessage`。++## 常见问题++- **缺少 `start` 事件** —— 订阅者看不到部分消息。始终先推送它。+- **忘记调用 `ms.set_result(...)`** —— `await agent.prompt()` 永远挂起。在成功路径和错误路径都要设置结果。+- **同步的 `produce`** —— `stream()` 必须**立即**返回——将工作放在任务内部。如果在 `return ms` 之前 `await` 了你的后端,就阻塞了调用方。+- **推送后修改 `partial`** —— 推送时使用 `deep=True` 拷贝;调用方异步迭代事件,在迭代过程中发生变更会产生极难调试的别名问题。++## 参见+- [图片生成](./image-generation) —— 使用 `openai-images` 与 OpenAI 图片模型。++- [Providers Overview](./overview) —— 在从头编写类之前,先检查你的后端是否只是一个 `CapabilityDescriptor` 或内置预设已经覆盖的 OpenAI/Anthropic 兼容端点。+- [API 参考 → providers/base](../../api/cubepi-providers) —— 完整类型列表。+- [Anthropic Provider 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/anthropic.py) —— 一个真实完整的示例。+- [`FauxProvider` 源码](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py) —— 测试原语,包含流式真实性的细节。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/image-generation.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/image-generation.mdnew file mode 100644index 0000000..07ea601--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/image-generation.md@@ -0,0 +1,137 @@+---+title: 图片生成+description: "使用 CubePi 的图片生成能力。"+---++# 图片生成++这一页介绍 CubePi 的独立图片生成链路。它与 `Agent` 有意分离:对话场景仍然走+`Model`,图片场景走 `ImagesModel`,并直接调用图片 provider。++## `ImagesModel` 类型++图片生成使用 `cubepi.providers.images.types` 中的三个基础类型:++```python+from cubepi.providers.images.types import AssistantImages, ImagesContext, ImagesModel+```++- `ImagesModel`:图片模型绑定(`id`、`provider`、`api`)。+- `ImagesContext`:生成请求体(`prompt`、可选 `input_images`)。+- `AssistantImages`:图片 provider 的返回值,包含 `output`、`stop_reason`,+ 以及可选的 `error_message`。++`input_images` 和 `AssistantImages.output` 中用到的是+`ImageContent`(定义在 `cubepi.providers.base`)。++## 创建图片模型++OpenAI 图片模型一般通过 `create_images_provider("openai-images", ...)` 创建+provider,并用 `ImagesModel` 绑定:++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++image_provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")+```++`api` 是给上游系统做路由与归因的标识,可按需自定义。++## 文生图++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")++resp = await provider.generate_images(+ model=model,+ context=ImagesContext(prompt="一只戴帽子的猫在日落中"),+ options={"size": "1024x1024", "quality": "high", "n": 2},+)++if resp.stop_reason == "error":+ raise RuntimeError(resp.error_message or "图片生成失败")++for block in resp.output:+ print(block.type, block.source[:30], block.media_type)+```++如果不传 `options`,provider 会按默认参数发送(`n=1`),且不带可选的图片参数。++## 图像编辑++`input_images` 非空时自动走编辑链路:++```python+import base64+from cubepi.providers.base import ImageContent+from cubepi.providers.images.types import ImagesContext++with open("source.png", "rb") as fh:+ source_b64 = base64.b64encode(fh.read()).decode("ascii")++ctx = ImagesContext(+ prompt="让它更亮一些,色温更暖。",+ input_images=[ImageContent(source=source_b64, media_type="image/png")],+)+```++`generate_images(model, context=ctx)` 会调用 `edit`,而不是 `generate`。++## `options` 下发行为++`generate_images(..., options)` 是一个透传字典(在补齐 `model` / `prompt` /+`n` 后)直接传给后端。OpenAI 图片 provider 常见参数:++- `size`+- `quality`+- `n`+- `output_format`(`png` / `jpeg` / `webp`)+- `background`+- 以及网关允许的其他扩展参数++provider 只对 `output_format` 做输出类型映射:+`png` -> `image/png`,`jpeg` -> `image/jpeg`,`webp` -> `image/webp`。++## 错误处理++后端失败时 `generate_images(...)` 会返回 `AssistantImages`,并包含:++- `stop_reason="error"`+- `output=[]`+- `error_message=<后端报错>`++这样可以把调用端的成功 / 失败逻辑统一到一个返回分支中处理。++## 测试与模拟++本地测试可以直接使用 `FauxImagesProvider` 产出稳定图像结果:++```python+from cubepi.providers.images.faux import FauxImagesProvider++provider = FauxImagesProvider(png_b64="iVBORw0KGgoAAAANSUh...")+```++适合不依赖真实图片 API 的端到端或 UI 测试。++## 参见++- [Providers Overview](./overview) —— 默认行为和能力描述符。+- [OpenAI Provider](./openai) —— OpenAI 与 OpenAI 兼容网关的配置方式。+- [API Reference → providers(图片类型)](../../api/cubepi-providers)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/openai.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/openai.mdnew file mode 100644index 0000000..601ab2b--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/openai.md@@ -0,0 +1,138 @@+---+title: OpenAI+description: "在 CubePi 中使用 OpenAI GPT 模型——OpenAIProvider 与 Chat Completions API 集成。"+---++# OpenAI Provider++CubePi 内置两个 OpenAI provider,分别覆盖两种 API 接口:++- **`OpenAIProvider`** —— Chat Completions API(`/v1/chat/completions`)。适用于 GPT-4/5 系列以及大多数 OpenAI 兼容服务器(vLLM、LiteLLM、DeepSeek、Qwen、MiniMax、豆包……)。+- **`OpenAIResponsesProvider`** —— Responses API(`/v1/responses`)。适用于需要服务端状态和推理摘要的场景。++两者实现相同的 `Provider` 协议;每个 agent 选择其一。++## Chat Completions:`OpenAIProvider`++```python+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ provider_id="openai",+ api_key="sk-…", # or reads OPENAI_API_KEY+ base_url=None, # set for OpenAI-compatible servers+ extra_body=None, # merged into every request+ extra_headers=None,+)++model = provider.model(+ "gpt-5",+ reasoning=True, # enables thinking level mapping+ max_tokens=8192,+ context_window=128_000,+)+```++### Chat Completions 中的推理++OpenAI 通过 o 系列和 gpt-5 模型的 `delta.reasoning_content` 暴露推理内容。CubePi 将其捕获为 `ThinkingContent`,并以与 Anthropic 完全相同的方式发出 `thinking_*` 事件。相同的 `ThinkingLevel` 枚举(`"off"` → `"high"`)同样适用。++许多 OpenAI 兼容的开源后端在不同字段下暴露推理内容。CubePi 按优先级顺序识别以下三种:++1. `delta.reasoning_content`(DeepSeek、Qwen、豆包)+2. `delta.reasoning`(vLLM)+3. `delta.reasoning_details`(MiniMax)++无需任何配置——provider 会自动选择存在的字段。++### `extra_body` 处理开源后端的差异++大多数 OpenAI 兼容服务器通过请求体接受扩展字段。在构造时一次性设置:++```python+provider = OpenAIProvider(+ api_key="…",+ base_url="https://api.deepseek.com/v1",+ extra_body={"enable_thinking": True, "stream_options": {"include_usage": True}},+)+```++如需按请求修改,请使用 `on_payload`(见下文)。++### 能力描述符++OpenAI 与 OpenAI 兼容后端之间的 wire 格式差异(如 `max_tokens` vs `max_completion_tokens`、推理字段名称、temperature 处理方式)可通过构造时传入的 [`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html) 来配置。例如,`max_tokens_field="max_completion_tokens"` 会在请求发出前重命名该 key。详见 [能力描述符](./overview) 了解完整配置项(CubePi `0.5+`)。++### 指向 vLLM / LiteLLM / DeepSeek++```python+provider = OpenAIProvider(+ api_key="dummy", # vLLM ignores it+ base_url="http://localhost:8000/v1",+ extra_headers={"Authorization": "Bearer dummy"},+)+```++LiteLLM 示例:++```python+provider = OpenAIProvider(+ api_key=os.environ["LITELLM_KEY"],+ base_url="https://litellm.internal/v1",+)+```++关于这些后端的 wire 差异(推理开关、token 字段重命名……),参见 [能力描述符](./overview)。++## Responses API:`OpenAIResponsesProvider`++```python+from cubepi.providers.openai_responses import OpenAIResponsesProvider++provider = OpenAIResponsesProvider(provider_id="openai_responses", api_key="sk-…")+model = provider.model("gpt-5", reasoning=True)+```++Responses API 在服务端维护状态(通过 `previous_response_id` 引用)。CubePi 追踪 `AssistantMessage.response_id` 并自动回传——你的代码看起来与 Chat Completions 路径完全相同。++在以下情况下使用 Responses provider:++- 需要推理**摘要**(而不只是文本)作为 thinking 块暴露。+- 使用 `o` 系列模型,并希望服务端跨轮次保持推理链(更小的 payload,更快的复用)。++如果需要完全控制消息列表和提示缓存策略,请继续使用 `OpenAIProvider`。++## `on_payload` / `on_response`++形状与 [Anthropic](./anthropic) provider 相同。payload 字典有所不同(OpenAI 风格中 `system` 不单独存放,`tools` schema 格式也不同),因此在修改前先检查一次。++```python+async def add_user_metadata(payload, model):+ payload["user"] = "u-42" # billable user attribution+ return payload++agent = Agent(model=model, on_payload=add_user_metadata)+```++## 工具调用++工具定义会自动转换为 OpenAI 的 `{"type": "function", "function": {...}}` 格式。流式格式在 `toolcall_delta` 下发出增量 JSON 参数;CubePi 通过 [`cubepi.utils.json_parse.parse_streaming_json`](../../api/cubepi-utils) 对其进行缓冲和解析,确保部分内容始终能校验为最接近的合法对象。++一条 assistant 消息中的多个并行工具调用开箱即用——它们与 Anthropic provider 使用相同的并行执行器进行路由。++## 常见问题++- **`stream_options.include_usage` 被拒绝** —— 部分兼容服务器会拒绝整个 `stream_options` 字段。**`on_payload` 无法修复此问题**:CubePi 0.3 在你的回调执行**之后**才调用 `kwargs.setdefault("stream_options", {})`,因此在 `on_payload` 中删除该 key 会被静默地撤销。解决方案:+ - 子类化 `OpenAIProvider` 并覆盖 `stream()`,对你的后端跳过 `setdefault`。+ - 在 `on_payload` 中设置 `include_usage=False`(字段仍会发出,但通常被严格后端接受为无操作)。+ - 使用 [`CapabilityDescriptor`](./overview)(CubePi `0.5+`)以声明式方式描述你的后端推理配置。+- **有推理内容但没有 `thinking_*` 事件** —— 你的后端在非标准字段下暴露推理内容。可通过 PR 添加第四个分支,或使用 `on_payload` 进行转码。+- **同一进程中混用多个 provider** —— 每个 provider 持有自己的 HTTP 客户端。按 `(base_url, api_key)` 对复用单个实例,而非每个 agent 各创建一个。+- **usage 显示 0 个输入 token** —— 大多数兼容服务器完全省略 usage,或只在最后一个 chunk 中发出。可在 `on_payload` 中检查尾部 chunk 获取提示,或将 token 计数视为这些后端的尽力而为值。++## 参见+- [图片生成](./image-generation) —— 使用 `openai-images` 与 OpenAI 图片模型。++- [Anthropic Provider](./anthropic) —— 另一个内置 provider。+- [自定义 Provider](./custom) —— 从头编写你自己的 provider。+- [Recipes → 多 Provider 故障转移](../../recipes/multi-provider-failover) —— 结合两个 provider 提升弹性。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/overview.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/overview.mdnew file mode 100644index 0000000..1c247f8--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/providers/overview.md@@ -0,0 +1,249 @@+---+title: Providers Overview+description: "CubePi 的 provider 配置、能力和预设总览。"+---++# Providers Overview++_从这里开始看 provider 配置。_++本页是 provider 配置的入口。先讲 Anthropic / OpenAI 的默认路径,+再讲当你需要接入别的后端时,如何用数据的方式描述 wire 差异和+预设覆盖,而不是写各家厂商 glue code。整体顺序是:++1. **默认零配置。** 对于 Anthropic 和 OpenAI,只需创建 provider 和 model,本页的内容一概不需要。+2. **对于非默认端点,以数据方式描述差异。** `CapabilityDescriptor` 以声明式方式捕获差异——无需子类化,无需 fork。++## `provider.model(...)` 参数++用 `provider.model(model_id, ...)` 为 Agent 创建绑定模型。`model_id` 为必填位置参数,其他参数均为可选关键字参数:++- `api: str` — 覆盖接口名/路由标签,供后续集成读取。+- `reasoning: bool` — 启用推理模式与 reasoning-level 协商。+- `context_window: int` — 上下文窗口提示(主要用于校验与提示词规划)。+- `max_tokens: int` — 该模型默认最大生成 token 上限。+- `temperature: float` — 该模型默认采样温度。+- `cost: ModelCost | None` — 可选的成本元数据对象。+- `thinking_level_map: dict[str, str | None] | None` — 推理等级映射;将某级别设为 `None` 表示该 level 不可用。++## CapabilityDescriptor:当后端模型线不同时该怎么配++`CapabilityDescriptor` 在初始化 provider 时传入,用于统一描述这个 provider+后端在 wire 上的差异。++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ reasoning_off_payload={"extra_body": {"thinking": False}},+ max_tokens_field="max_completion_tokens",+ ),+)+```++如果只有部分模型例外,使用 `model_capability_overrides`:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://openrouter.ai/api/v1",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ ),+ model_capability_overrides={+ "deepseek-r1": CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": "enabled"}},+ ),+ },+)+```++`model_capability_overrides` 使用精确 `model_id` 匹配。++`CapabilityDescriptor` 常用字段:++- `reasoning_on_payload / reasoning_off_payload` — 在 reasoning 开/关时,深度+ 合并到最终 payload。+- `reasoning_level`(`ReasoningLevelSpec`)— 将 `off`/`minimal`/... 映射到后端字段。+- `temperature`(`TemperatureSpec`)— 裁剪、固定或去掉温度参数。+- `max_tokens_field` — 选 `max_tokens` 或 `max_completion_tokens`。+- `supports_tools` / `supports_images` / `supports_streaming` — 供宿主应用或前端消费的能力元数据。++:::note 预设目录由宿主应用维护+CubePi 提供的是**机制**(`CapabilityDescriptor` 及其 wire 运行时),而非厂商目录。包含 base URL、认证、区域/代码规划端点、模型列表的现成 provider 列表属于产品数据,应由嵌入 CubePi 的应用自行维护(例如,cubebox 维护自己的 provider 目录)。如需接入特定厂商,请按下文所示,使用正确的 `base_url` + `CapabilityDescriptor` 构建 provider。+:::++## 1. 简单场景——完全零配置++大多数用户无需关心能力描述符。内置 provider 已有合理的默认值:++```python+import cubepi+from cubepi import Agent+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(provider_id="anthropic") # reads ANTHROPIC_API_KEY+agent = Agent(model=provider.model("claude-sonnet-4-6"))+await agent.prompt("Hello!")+```++这就是全部配置。不带 `capability=` 构建的 provider,其输出与 CubePi `0.4` 完全一致——下文的机制仅在显式使用时才会生效。++## 2. 非默认端点——CapabilityDescriptor++当你需要使用 OpenAI 或 Anthropic 之外的模型——DeepSeek、Qwen、豆包、OpenRouter 路由、本地服务器——麻烦在于每家的 wire 方言不同(是用 `max_tokens` 还是 `max_completion_tokens`?如何开关推理?)。你不需要子类化 provider;只需将差异描述为一个 [`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html),连同正确的 `base_url` 和对应端点 wire 形状的 provider 类一起传入:++```python+import os+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key=os.environ["DEEPSEEK_API_KEY"],+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_off_payload={"extra_body": {"reasoning": {"exclude": True}}},+ reasoning_on_payload={"extra_body": {"reasoning": {"exclude": False}}},+ ),+)+```++根据端点的 wire 方言选择 provider 类:++| Wire 方言 | Provider 类 |+| --- | --- |+| `anthropic-messages` | `AnthropicProvider` |+| `openai-completions` | `OpenAIProvider` |+| `openai-responses` | `OpenAIResponsesProvider` |++每个字段对应一种 wire 行为,未设置的字段不产生任何影响——因此只需声明实际存在差异的部分。++### `max_tokens_field`++`"max_tokens"`(默认)或 `"max_completion_tokens"`。部分 OpenAI 兼容服务器只接受其中一种拼写;此字段在请求发出前重命名该 key。**效果:** 选错会导致服务器忽略输出长度限制或返回 400。++### `temperature`++`TemperatureSpec` 控制调用方传入的 temperature 如何处理:++```python+from cubepi import TemperatureSpec++TemperatureSpec(mode="free", min=0.0, max=2.0, default=1.0) # clamp into [min, max]+TemperatureSpec(mode="fixed", fixed_value=1.0) # always overwrite+TemperatureSpec(mode="ignored") # drop the key+```++- **`free`** —— 调用方的值被截断到 `[min, max]` 范围内;若未传入则不写入任何值。**效果:** 防止超出范围的值被后端拒绝。+- **`fixed`** —— 始终使用 `fixed_value`。**效果:** 适用于只允许固定 temperature 的模型(如部分 o 系列推理模型)。+- **`ignored`** —— 完全去除该 key。**效果:** 适用于在推理时对任何 `temperature` 字段返回 400 的后端。++### 推理开关:`reasoning_off_payload` / `reasoning_on_payload`++关闭推理时,将 `reasoning_off_payload` 深度合并到请求中;开启时合并 `reasoning_on_payload`。**效果:** 这就是"开关推理"如何映射到厂商期望的字段:++```python+CapabilityDescriptor(+ reasoning_off_payload={"extra_body": {"enable_thinking": False}},+ reasoning_on_payload={"extra_body": {"enable_thinking": True}},+)+```++合并会递归处理嵌套 dict;数组是原子的;冲突时 capability 值优先。++### 推理级别:`reasoning_level`(三种形状)++在开/关之外,CubePi 将 `ThinkingLevel`(`off`/`minimal`/`low`/`medium`/`high`/`xhigh`)映射到通过点路径 `path` 写入的具体 wire 值。`kind` 决定形状:++`ReasoningLevelSpec` 只负责「`thinking`/`minimal`/`low`/... 具体映射成后端字段」;要真正生效,还要配两个参数:++- 在 `provider.model(...)` 时把 `reasoning=True`(把这个模型设为推理模型)+- 在 `Agent(...)` 初始化时把 `thinking` 设成 `off|minimal|low|medium|high|xhigh`(默认 `off`)++```python+from cubepi import Agent, CapabilityDescriptor, ReasoningLevelSpec+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"reasoning": {"enabled": True}}},+ reasoning_level=ReasoningLevelSpec(+ path="reasoning.effort",+ kind="effort",+ level_to_effort={+ "off": "low",+ "minimal": "low",+ "low": "low",+ "medium": "medium",+ "high": "high",+ "xhigh": "high",+ },+ ),+ ),+)++agent = Agent(model=provider.model("deepseek-r1", reasoning=True), thinking="high")+```++```python+from cubepi import ReasoningLevelSpec++# int_budget — a token budget (Anthropic).+ReasoningLevelSpec(+ path="thinking.budget_tokens", kind="int_budget",+ level_budgets={"off": 0, "minimal": 1024, "low": 2048,+ "medium": 8192, "high": 16384, "xhigh": 16384},+)++# effort — an effort string (OpenAI Responses).+ReasoningLevelSpec(+ path="reasoning.effort", kind="effort",+ level_to_effort={"minimal": "minimal", "low": "low",+ "medium": "medium", "high": "high", "xhigh": "high"},+)++# enum — a vendor-specific state (Doubao's 3-state thinking).+ReasoningLevelSpec(+ path="thinking.type", kind="enum",+ level_to_enum={"off": "disabled", "low": "enabled", "high": "enabled"},+)+```++**效果:** 映射表中缺失的级别不会被写入,端点将保持该级别的自身默认值。++### `supports_tools` / `supports_images` / `supports_streaming`++声明式标志,由宿主应用和前端读取(例如,用于置灰图片上传按钮)。provider 本身不依赖这些标志做行为控制。++### 共享端点上的按模型覆盖++一个网关(OpenRouter、LiteLLM、内部代理)通常同时服务推理型和非推理型模型。`model_capability_overrides` 将 `model_id` 映射到一个描述符,该描述符会**替换**该模型的基础描述符:++```python+provider = OpenAIProvider(+ api_key="…",+ base_url="https://openrouter.ai/api/v1",+ capability=base_cap, # default for unlisted models+ model_capability_overrides={+ "deepseek/deepseek-r1": reasoning_cap, # this model only+ },+)+```++解析方式为对 `model_id` 精确匹配;未列出的模型回退到 `capability`。++## 参见++- [OpenAI Provider](./openai) —— OpenAI / OpenAI 兼容端点的具体配置。+- [Anthropic Provider](./anthropic) —— `int_budget` 推理形状的实际用法。+- [自定义 Provider](./custom) —— 当端点甚至不是 OpenAI/Anthropic 形状时。+- [API 参考 → `cubepi.providers`](../../api/cubepi-providers)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/cli.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/cli.mdnew file mode 100644index 0000000..57e823d--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/cli.md@@ -0,0 +1,169 @@+---+title: Trace CLI+description: "使用 CubePi trace CLI 检查、过滤并展示 OpenTelemetry trace span。"+---++# 使用 `cubepi trace` 检查 trace++`JsonlSpanExporter` 在 `./cubepi-traces/<date>/<trace_id>.jsonl` 下每条+trace 写入一个文件。`cubepi trace` CLI(由 `trace-cli` extra 提供)读取这些+文件,让你无需重新运行即可了解一次运行的详细情况——哪些 LLM 和工具调用被+触发、顺序如何、各自返回什么、在哪里出错以及 token 数量。++```bash+pip install 'cubepi[trace-cli]' # 或:uv sync --extra trace-cli+cubepi trace --help+```++`--dir` 默认为 `./cubepi-traces`;若 trace 文件存放在其他位置,传入+`--dir <path>` 即可。每个文件是一条 **trace**:运行本身加上所有嵌套+subagent 运行(它们继承父级的 `trace_id`,因此写入同一文件)。++## `ls` —— 列出近期 trace++```bash+cubepi trace ls # 最新优先;-n N 限制数量+```++| 列 | 含义 |+|---|---|+| `started` | trace 开始时间(UTC) |+| `trace_id` | 传递给 `view` / `follow` / `stats` 的 ID |+| `spans` | 整条 trace 的 span 数量(含 subagent) |+| `status` | `ok` 或 `error` |+| `duration` | trace 的实际耗时 |+| `input` | 用户的 prompt,用于识别此次运行 |++### 按运行元数据过滤(`--meta`)++如果宿主在 trace 上打了 run 级别的元数据(通过 `tracing_context(metadata=…)`+——例如 cubebox 会在根 `invoke_agent` span 上记录 `conversation_id`、+`user_id`、`org_id`、`workspace_id`),可以只过滤出匹配的 trace:++```bash+cubepi trace ls --meta conversation_id=conv_123+cubepi trace ls --meta user_id=usr_9 --meta org_id=org_1 # 可重复 = AND,精确匹配+```++每个 `--meta KEY=VALUE` 与 trace 根节点元数据精确匹配;重复该标志表示 AND 关系。++如需将元数据值**显示**为列(而不只是过滤),添加 `--show-meta KEY[,KEY…]`:++```bash+cubepi trace ls --show-meta conversation_id,user_id+cubepi trace ls --meta org_id=org_1 --show-meta conversation_id # 过滤 + 显示+```++(或通过 `cubepi trace view <id> -v` 查看单条 trace 的所有元数据。)++## `view` —— 将 trace 渲染为 span 树++trace-id 的**前缀**即可(表格会截断 ID);如果前缀不唯一,会列出候选结果。++```bash+cubepi trace view 1cd97cdb+```++```+trace+└── invoke_agent 14425.8ms [0x1cd97cdb]+ ├── cubepi.turn 1283.1ms [0x5cfda93e]+ │ ├── chat deepseek-v4-flash 1208.7ms tok 6845/68 [0x0d130229]+ │ └── execute_tool subagent 9610.2ms subagent [0x38bdd10a]+ │ └── invoke_agent 9601.0ms [0x8094f99b] ← subagent 运行,嵌套其中+ │ └── cubepi.turn 9598.4ms [0x57c5cfc7]+ │ ├── chat deepseek-v4-flash 1190.3ms [0x8205ca6b]+ │ └── execute_tool web_search 6500.2ms web_search [0xca4e59fc]+ └── cubepi.turn 491.9ms ERROR [0xce25f242]+ └── chat deepseek-v4-flash 427.2ms ERROR [0x0bff68ec]+ └── error: Error code: 400 - ... `tool_use` ids were found without+ `tool_result` blocks immediately after: call_01_...+```++从上到下阅读:`invoke_agent`(一次运行)→ `cubepi.turn`(一个 agent 循环+turn)→ `chat <model>`(一次 LLM 调用,含 `tok <input>/<output>`)和+`execute_tool <name>`(一次工具调用)。**subagent** 显示为+`execute_tool subagent`,其自身的 `invoke_agent → cubepi.turn → …` 直接+嵌套在其下方。每个节点后缀的 `[0x…]` 是该 span 的 `span_id`——在原始+JSONL 中 grep 它即可检查对应的具体 span。错误信息内联显示在失败 span 的下方。++标志:++```bash+cubepi trace view <id> --content # 展开 gen_ai prompt / 工具参数 / 结果+cubepi trace view <id> -v # 展开所有 span 属性(详细,输出量大)+```++`--content` 要求该运行以 `record_content=True` 方式录制+(参见[内容记录与脱敏](./content-recording))。++## `follow` —— 实时观察 trace++```bash+cubepi trace follow <id> # 轮询 span 完成情况;适合正在进行中的运行+```++## `stats` —— 跨 trace 聚合统计++```bash+cubepi trace stats --by model # 延迟 p50/p95、错误率、token 数量+cubepi trace stats --by tool --since 2026-05-20+```++`stats` 同样接受 `--meta KEY=VALUE`(语义与 `ls` 相同),仅对匹配的 trace+进行聚合——例如按单个用户或对话统计延迟/错误率/token:++```bash+cubepi trace stats --by model --meta user_id=usr_9+cubepi trace stats --by tool --meta conversation_id=conv_123+```++## `convert` —— 重建 API 请求体++当你需要重放某次特定的 LLM 调用——复现故障、测试 prompt 变更,或对同一+上下文执行原始 `curl`——`convert` 读取已录制的 `chat` span 并输出完整的+请求体。++需要 `record_content=True`。++```bash+# 默认:trace 中最后一个 chat span,OpenAI JSON 格式+cubepi trace convert <trace_id>++# 选择要重建的 LLM 调用+cubepi trace convert <trace_id> --turn 2 # 第 2 个 chat span(从 1 计数)+cubepi trace convert <trace_id> --span 0xbb7eb1 # 按 span_id 前缀(来自 `view`)++# 输出格式+cubepi trace convert <trace_id> --format openai # 默认——JSON 请求体+cubepi trace convert <trace_id> --format anthropic # Anthropic Messages API 请求体+cubepi trace convert <trace_id> --format curl # 可直接执行的 curl 命令+```++`view` 输出中的 `[0x…]` span id 可直接传给 `--span`:++```+├── chat kimi-k2.6 31704.5ms [0xbb7eb192] ← 粘贴为:--span 0xbb7eb1+├── chat kimi-k2.6 32420.2ms [0x7c76f48d] ← 或:--span 0x7c76f4+```++重建后的请求体包含完整对话历史、system prompt、所有工具定义以及请求参数+(`model`、`max_tokens`、`temperature`)。可管道传给 `python -m json.tool`、+`jq` 或直接用于重放脚本。++## CLI 之外++这些文件是纯 JSONL——每行一个 span——因此可以直接解析(`jq`、`python -c`)+以提取特定属性(`gen_ai.usage.*`、`gen_ai.tool.call.result`、+`gen_ai.input.messages`……)。错误详情存放在名为+`gen_ai.client.operation.exception` 的 span **event** 中。++:::tip 为 AI agent 准备+内置的 `cubepi-trace` skill 可驱动此 CLI 进行调试("这次运行为何没有回复?"、+"工具结果不对")。它内置了快速路径(`ls` → `view <prefix>`)以及+token/缓存命中率的约定。++```bash+npx skills add cubeplexai/cubepi@cubepi-trace -a claude-code+```+:::diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/content-recording.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/content-recording.mdnew file mode 100644index 0000000..671efe1--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/content-recording.md@@ -0,0 +1,154 @@+---+title: 内容记录与脱敏+description: "在 CubePi 的 OpenTelemetry tracing 中配置 prompt 内容记录与脱敏。"+sidebar_position: 4+---++# 记录 Prompt、响应与工具 Payload++CubePi 的 tracing 默认只发出结构性属性——操作名称、模型、token 数量、+结束原因、耗时。**不会有 prompt 内容、模型输出、工具参数或结果离开进程。**+这是有意为之:很多 agent 场景处理 PII、客户数据或商业机密 prompt,+这些内容不应发送到第三方可观测性后端。++当你确实需要捕获内容——用于离线评测、调试偶发的工具调用问题,或构建标注+数据集——可以通过 `record_content=True` 显式开启,并配合 `redact` 回调在+数据离开进程之前剥除敏感部分。++## 开启内容记录++```python+tracer = Tracer(+ service_name="my-bot",+ agent_name="assistant",+ record_content=True, # ← 显式开启+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+)+```++开启 `record_content=True` 后,每层 span 按照 OTel GenAI semconv 携带相应+内容属性:++| Span | 新增内容属性 |+|---|---|+| `invoke_agent` | `gen_ai.system_instructions`、`gen_ai.input.messages`、`gen_ai.output.messages` |+| `cubepi.turn` | `gen_ai.input.messages`(per-turn 切片)、`gen_ai.output.messages`(per-turn 切片) |+| `chat <model>` | `gen_ai.system_instructions`、`gen_ai.input.messages`、`gen_ai.tool.definitions`、`cubepi.llm.raw_request`、`cubepi.llm.raw_response` |+| `execute_tool <tool_name>` | `gen_ai.tool.call.arguments`、`gen_ai.tool.call.result` |++`chat` span 的 `gen_ai.input.messages` 包含 provider 请求实际携带的+**完整时序上下文**——包括之前的 assistant turn 和工具结果,而不只是新的+用户 prompt。对于多轮工具调用运行,trace 消费方可以精确还原模型在每次+调用时所看到的内容。++## 导出前脱敏++`redact` 是一个 `(key, value) -> value | None` 回调,在每个内容属性的设置+处被调用。返回值:++- 原始值不变 → 保留原样+- 相同形状的修改后的值 → 替换原值+- `None` → 完全丢弃该属性++```python+def redact(key: str, value):+ # 在 prompt 离开进程之前剥除其中的密钥。+ if key in ("gen_ai.input.messages", "gen_ai.output.messages"):+ return _scrub_messages(value)+ # 在生产环境中不发送原始请求/响应体——只保留规范化后的结构。+ if key in ("cubepi.llm.raw_request", "cubepi.llm.raw_response"):+ return None+ return value+++tracer = Tracer(+ service_name="my-bot",+ record_content=True,+ redact=redact,+ exporters=[…],+)+```++`redact` 是内容的唯一拦截点——recorder 在将属性序列化为 OTel 属性之前,+每个属性只调用一次,函数返回什么就发送什么。`redact` 内部抛出的异常会被+吞掉(此时该属性被丢弃),因此有 bug 的 redactor 会以"不发送"方式失败,+而不是泄露数据。++### 常用模式++丢弃所有内容,仅保留 per-message 长度,让 dashboard 仍可正常工作:++```python+def redact(key, value):+ if key in ("gen_ai.input.messages", "gen_ai.output.messages"):+ return [{"role": m["role"], "parts": [{"type": "text", "content": "<redacted>",+ "length": sum(len(p.get("content", "")) for p in m["parts"])}]}+ for m in value]+ return value+```++基于标签的选择性记录——除非某个 thread 已开启,否则全部过滤:++```python+import contextvars+RECORD = contextvars.ContextVar("trace.record_content", default=False)++def redact(key, value):+ return value if RECORD.get() else None+```++然后对需要捕获的运行调用 `RECORD.set(True)`。++## 大小预算++OTel 属性值在 recorder 内部以 JSON 序列化。大多数后端会截断或拒绝超过+几百 KB 的属性。如果每个 chat span 都记录原始 provider 响应,多轮工具调用+的 agent 运行会很快产生大量数据。对超出预算的字段,请通过 `redact` 丢弃+或做摘要处理。++## 流级别记录++`record_content` 捕获每个 `chat` span 上最终组装好的请求和响应,但不捕获+逐块的流式 chunk。如需对流式失败做事后调试——空工具调用参数、重复事件、+输出被截断——可开启 `record_stream`,在主 trace 旁边写入逐块事件日志:++```python+tracer = Tracer(+ record_content=True, # trace convert 所需+ record_stream=True, # ← 逐块事件日志+ stream_dir="./cubepi-traces", # <run_id>.stream.jsonl 的写入目录+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+)+```++`record_stream` 写入 `<stream_dir>/<run_id>.stream.jsonl`(每个+`StreamEvent` 一行 JSON)。每行携带 `t`(距运行开始的秒数)和 `type`。+工具调用相关行还包含 `ci`(content index)、`id`、`name`、delta 大小和+参数预览:++```json+{"t": 5.873, "type": "toolcall_start", "ci": 1, "id": "toolu_...", "name": "show_widget"}+{"t": 5.875, "type": "toolcall_delta", "ci": 1, "chars": 11, "accumulated": 11, "preview": "{\"title\": \""}+{"t": 33.177, "type": "toolcall_end", "ci": 1, "id": "toolu_...", "args_chars": 7465, "args_preview": "{\"title\": \"CubePi..."}+```++通过这些数据,可以方便地确认参数 chunk 是否按预期到达,或者同一事件是否+触发了两次(例如,某个 provider 发送了两次 `finish_reason`,就会产生两条+相同 `ci` 的 `toolcall_end` 行)。++`record_stream` 与 `record_content` 相互独立——仅在调试会话中开启。+对于长时间运行、工具调用密集的 agent,文件可能会变得很大。++## 审计已记录的内容++recorder 始终在每个 span 上设置 `service.name`、`gen_ai.agent.name` 和+`cubepi.run_id`——无论 `record_content` 是否开启。使用这些属性在 trace+后端过滤到单次运行,并直观确认哪些数据已落地。++如需深度审计,`JsonlSpanExporter` 每行写入一个 span,因此可以在将同一+exporter 指向远程后端之前,先对本地文件进行 grep / `jq` 检查:++```bash+jq -r 'select(.attributes["gen_ai.input.messages"]) | .attributes["gen_ai.input.messages"]' \+ cubepi-traces/2026-05-19/*.jsonl+```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/getting-started.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/getting-started.mdnew file mode 100644index 0000000..35d6020--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/getting-started.md@@ -0,0 +1,223 @@+---+title: 快速开始+description: "在 CubePi 中快速上手 OpenTelemetry tracing——安装、配置并导出 span。"+sidebar_position: 2+---++# Tracing 快速开始++## 安装 extra++CubePi 将 OpenTelemetry 作为可选依赖:++```bash+pip install "cubepi[tracing]"+```++这会拉取 `opentelemetry-sdk` 及相关包。若未安装该 extra,导入 `cubepi.tracing`+时会抛出清晰的错误提示,让你在导入阶段就能发现问题,而不是运行到一半才报错。++## 挂载 Tracer++最简端到端配置——本地 JSONL 导出,符合习惯的 RAII 模式:++```python+import asyncio+from cubepi import Agent, Model+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.tracing import Tracer+from cubepi.tracing.exporters import JsonlSpanExporter+++async def main() -> None:+ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ system_prompt="Be helpful.",+ )++ async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ tracer.attached(agent),+ ):+ await agent.prompt("Say hello.")+ await agent.wait_for_idle()+ # 退出时:自动 detach(关闭所有已取消运行的 span,等待 flush)+ # + tracer shutdown(flush + 关闭 exporter)。无需 try/finally。+++asyncio.run(main())+```++如果无法改写成 `async with`(例如将 agent 传递给长生命周期 web handler),+也可以使用显式模式,效果完全等价:++```python+detach = tracer.attach(agent)+try:+ await agent.prompt("…")+finally:+ # 以下两者单独调用均可:+ # await detach() # 等待已调度的 flush+ # await tracer.shutdown() # flush + 关闭 exporter+ detach()+ await tracer.shutdown()+```++即使完全忘记清理,`Tracer` 默认会注册一个 `atexit` hook,在进程退出时+同步 flush 缓冲 span——传入 `atexit_flush=False` 可关闭此行为,或在开发阶段+把它当作安全网使用。(`SIGKILL` 或 `os._exit` 时不会触发;如需保证必达,+请使用 OTel 的同步 `SimpleSpanProcessor`。)++每次运行产生一个 JSONL 文件,按 `trace_id` 分片:++```text+./cubepi-traces/+ 2026-05-19/+ 8e1c9a3f4b2d…d976a.jsonl ← 一条 trace,一个文件,每行一个 span+```++一条 trace 代表整次运行,包括所有嵌套 subagent 运行(它们继承父级的+`trace_id`,因此写入同一文件)。每个 span 仍携带 `cubepi.run_id` 属性,+可按单次运行过滤。++用任何支持 OTLP/JSON 的工具,或直接用 `jq` 打开:++```bash+jq -r '"\(.name) \(.attributes."gen_ai.operation.name" // "")"' \+ cubepi-traces/2026-05-19/*.jsonl+# invoke_agent invoke_agent+# cubepi.turn+# chat claude-sonnet-4-5-20250929 chat+```++## Span 层级++单次 prompt 经过一轮 LLM 往返,recorder 产生三个 span:++```text+invoke_agent assistant [INTERNAL] gen_ai.operation.name=invoke_agent+└── cubepi.turn [INTERNAL] cubepi.turn.index=0+ └── chat <model> [CLIENT] gen_ai.operation.name=chat+```++模型调用工具时,每个工具多一层:++```text+invoke_agent assistant+└── cubepi.turn ← turn index 0+ ├── chat <model> ← 第一轮往返+ └── execute_tool <tool_name> ← gen_ai.tool.name, gen_ai.tool.call.id+└── cubepi.turn ← turn index 1(工具结果返回后的响应)+ └── chat <model>+```++MCP 工具的 `execute_tool` span 会有一个 CLIENT 子节点:++```text+execute_tool <tool_name> [INTERNAL] cubepi 侧封装+└── tools/call <tool_name> [CLIENT] gen_ai.operation.name=execute_tool+ mcp.method.name=tools/call+ mcp.session.id=…+ server.address / server.port+```++CLIENT span 会将 W3C `traceparent` 注入出站 HTTP 头,让下游已埋点的+MCP 服务器能够续接 trace。++## 取消、错误与中止++recorder 将取消视为控制信号,而非失败:++- 流式传输中调用 `agent.abort()` → span 以 `cubepi.aborted=true` 和+ `error.type=cubepi.aborted` 关闭,**状态为 UNSET**(遵循 OTel 指导原则——+ 取消不是错误)。+- provider 抛出异常 → chat/turn/root 以 **状态 ERROR** 关闭,chat span 上有+ `exception` 事件,`error.type` 由异常类派生(`timeout`、`connection_error`、+ 完全限定类名……)。+- MCP `tools/call` 返回 `isError=true` → CLIENT span 以 ERROR ++ `error.type=mcp.is_error` 关闭。++无论哪种情况,`detach()` 和 `tracer.shutdown()` 始终会关闭运行遗留的所有+未关闭 span,已取消的运行依然会出现在后端,不会悄悄消失。++## 每个 span 的属性++默认(无需额外开启):++- `invoke_agent`(根节点)—— `gen_ai.operation.name`、`gen_ai.provider.name`、+ `gen_ai.agent.name`、`cubepi.run_id`、`cubepi.agent.system_prompt.sha256`、+ `cubepi.agent.tools`(名称列表)、`cubepi.input_messages.count`、+ `cubepi.output_messages.count`+- `cubepi.turn` —— `cubepi.turn.index`、`cubepi.turn.stop_reason`、+ `cubepi.turn.tool_calls.count`、`cubepi.turn.terminated_by_tool`、+ `cubepi.run_id`+- `chat <model>` —— `gen_ai.operation.name`、`gen_ai.provider.name`、+ `gen_ai.request.model`、`gen_ai.request.max_tokens` / `temperature` /+ `top_p`、`gen_ai.request.stream`、`gen_ai.usage.input_tokens` /+ `output_tokens` / `cache_read_input_tokens` / `cache_creation_input_tokens` /+ `reasoning_output_tokens`、`gen_ai.response.model` /+ `finish_reasons` / `id`、`gen_ai.response.time_to_first_chunk`,以及+ OpenAI 专属字段(`openai.api.type`、service tier、system fingerprint)+- `execute_tool <tool_name>` —— `gen_ai.operation.name=execute_tool`、+ `gen_ai.tool.name`、`gen_ai.tool.call.id`、`gen_ai.tool.description`、+ `gen_ai.tool.type`、`cubepi.tool.is_error`、`cubepi.tool.execution_mode`+- `tools/call <tool_name>`(仅 MCP)—— `mcp.method.name`、`mcp.session.id`、+ `mcp.protocol.version`、`server.address`、`server.port`、`gen_ai.tool.name`++可选,通过 `Tracer(record_content=True)` 开启:+`gen_ai.input.messages`、`gen_ai.output.messages`、`gen_ai.system_instructions`、+`gen_ai.tool.definitions`、`gen_ai.tool.call.arguments`、+`gen_ai.tool.call.result`、`cubepi.llm.raw_request`、+`cubepi.llm.raw_response`。详见[内容记录与脱敏](./content-recording)。++## 多 agent 单进程++`Tracer` 和 `Meter` 均可安全地在多个 agent 之间共享——多次调用+`attach(agent)` 即可。每次 attach 都有独立的 recorder / metric 状态,+并发 agent 不会共享或相互覆盖 span 或直方图状态;MCP CLIENT span 根据+哪个 agent 的 `execute_tool` span 是父节点来路由到正确的 Tracer。++使用 RAII 方式,叠加多个 agent 只需一个 `async with`:++```python+async with (+ Tracer(...) as tracer,+ tracer.attached(agent_a),+ tracer.attached(agent_b),+):+ await asyncio.gather(agent_a.prompt("…"), agent_b.prompt("…"))+```++## 为单次运行打标签++`cubepi.tracing.tracing_context` 将 per-run 标签和元数据作用于+`invoke_agent` span,非常适合记录 `user_id`、`session_id`、A/B 测试分组+等需要在后端过滤的字段:++```python+from cubepi.tracing import tracing_context++async with tracer.attached(agent):+ with tracing_context(tags=["beta-arm"], metadata={"user_id": "u-42"}):+ await agent.prompt("Hello.")+```++span 上的属性:++- `cubepi.tags = ("beta-arm",)`+- `cubepi.metadata.user_id = "u-42"`++`cubepi.metadata.*` 前缀防止用户键与 recorder 自有 schema(如 `cubepi.run_id`)+冲突。标签和元数据的 contextvar 均为 per-asyncio-task 作用域,并发 agent+各自看到独立的值;嵌套的 `tracing_context` 块会合并(标签追加,元数据键取并集,+内层优先)。++## 下一步++- [OTLP 与后端](./otlp) —— Jaeger、Tempo、Honeycomb、Datadog……+- [内容记录与脱敏](./content-recording)+- [Metrics](./metrics)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/metrics.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/metrics.mdnew file mode 100644index 0000000..c1a0a55--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/metrics.md@@ -0,0 +1,145 @@+---+title: Metrics+description: "在 CubePi tracing 中收集 token 用量、延迟和首包时间指标。"+sidebar_position: 5+---++# 使用 `Meter` 采集指标++Span 描述单次运行的形态;直方图描述整个集群的形态。+`cubepi.tracing.Meter` 与 `Tracer` 对称,发出 OTel GenAI 指标集,让 dashboard+开箱即用。++## 发出的指标++| Instrument | 说明 |+|---|---|+| `gen_ai.client.operation.duration` | 直方图(秒)——在 `chat`、`execute_tool` 和 `invoke_agent` 关闭时记录 |+| `gen_ai.client.operation.time_to_first_chunk` | 直方图(秒)——当 `chat` 至少收到一个内容 chunk 时记录 |+| `gen_ai.client.token.usage` | 直方图(`{token}`)——每次 chat 响应按 `gen_ai.token.type`(`input`、`output`)各记录一次 |++每个数据点携带操作、provider 和请求模型属性,因此失败/已取消的请求+(没有响应体或响应模型落地)仍可按请求内容分组:++- `gen_ai.operation.name` —— `chat` / `execute_tool` / `invoke_agent`+- `gen_ai.provider.name` —— `anthropic`、`openai`、`openai_responses`……+- `gen_ai.request.model` —— 例如 `claude-sonnet-4-5-20250929`+- `gen_ai.response.model` —— 例如 `claude-sonnet-4-5-20250929`(仅成功时)+- `gen_ai.token.type` —— `input` 或 `output`(仅 token 用量)++## 挂载 Meter++RAII 惯用形式——`async with` 全包,无需手动清理:++```python+from opentelemetry.exporter.otlp.proto.http.metric_exporter import (+ OTLPMetricExporter,+)+from cubepi.tracing import Tracer, Meter+from cubepi.tracing.exporters import JsonlSpanExporter++async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ Meter(+ resource=tracer.resource, # 共享 Resource,使 service.* 与 span 匹配+ exporters=[+ OTLPMetricExporter(endpoint="http://otel-collector:4318/v1/metrics"),+ ],+ ) as meter,+ tracer.attached(agent),+ meter.attached(agent),+):+ await agent.prompt("...")+# 退出顺序自动:先 detach(关闭已取消运行的 span,flush trace pipeline)+# → 再 shutdown 两者(flush + 关闭 exporter)。+```++`Meter.attach()` 与 `Tracer.attach()` 相互独立,可以单独使用任一个;+推荐同时使用并共享一个 `Resource`,让后端将它们视为同一服务。++如果需要非 RAII 的显式形式(例如在长时间运行的服务器生命周期内动态挂载+agent):++```python+tracer_detach = tracer.attach(agent)+meter_detach = meter.attach(agent)+try:+ ...+finally:+ tracer_detach() # 关闭已取消运行的 span+ meter_detach() # 取消订阅 meter 的监听器+ await tracer.shutdown()+ await meter.shutdown()+```++## Bucket 边界++duration 直方图采用 OTel GenAI semconv 推荐的边界值(单位:秒):++```text+0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300+```++OTel 将这些值作为 `_advisory` 边界暴露;后端可以直接使用或覆盖。++## 单个 Meter 对应多个并发 agent++与 `Tracer` 一样,一个 `Meter` 实例可以安全地挂载到同一进程中的多个 agent。+每次 `attach()` 调用都有独立的内部 `_MeterState`,保存各自的 open-ns+时间戳和属性字典,来自两个 agent 的并发运行永远不会共享或相互覆盖指标状态。++```python+meter = Meter(resource=tracer.resource, exporters=[exporter])+meter.attach(agent_a)+meter.attach(agent_b)+```++两个 agent 各自发出独立的 duration / token / TTFC 观测值,可按+`gen_ai.agent.name`(使用 `Tracer(agent_name=…)` 时在 Resource 层面设置)+或 `gen_ai.request.model` 过滤。++## 关闭++上面的 RAII 形式(`async with … as tracer, … as meter, tracer.attached(agent),+meter.attached(agent):`)会自动处理关闭顺序:先 detach 内层 → 再执行外层+`Tracer/Meter` 的 `__aexit__`,调用 `shutdown()`。++对于手动模式,顺序很重要——`tracer_detach()` 必须在 `tracer.shutdown()`+之前运行,确保正在飞行的取消操作遗留的 span 在同一批次中被关闭和导出:++```python+finally:+ tracer_detach()+ meter_detach()+ await tracer.shutdown()+ await meter.shutdown()+```++`Meter.shutdown()` 等待指标 reader 完成 flush,然后关闭它。+`PeriodicExportingMetricReader` 按固定间隔(默认 60 秒)导出——`shutdown`+是在进程退出前保证最后一个时间窗口数据落地的唯一方式。++## 查询示例(Honeycomb)++过去一小时内按 provider 划分的 p95 chat 延迟:++```text+VISUALIZE P95(duration_s)+GROUP BY gen_ai.provider.name+WHERE gen_ai.operation.name = "chat"+TIME last 1 hour+```++按模型划分的 token 用量:++```text+VISUALIZE SUM(token_count)+GROUP BY gen_ai.request.model, gen_ai.token.type+WHERE gen_ai.operation.name = "chat"+```++替换为你所用后端的查询 DSL——属性名和聚合方式相同。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/otlp.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/otlp.mdnew file mode 100644index 0000000..e186642--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/otlp.md@@ -0,0 +1,165 @@+---+title: OTLP 与后端+description: "将 CubePi OpenTelemetry trace 导出到 Jaeger、Tempo、Honeycomb 等兼容 OTLP 的后端。"+sidebar_position: 3+---++# 导出到 OTLP 后端++`cubepi.tracing.Tracer` 接受任何 `opentelemetry.sdk.trace.export.SpanExporter`,+因此 OpenTelemetry 生态中的所有 exporter 均可直接使用。选择传输协议+(HTTP 或 gRPC),指向你的 collector,将 exporter 传入 Tracer 即可。++## HTTP(OTLP/HTTP)++```bash+pip install "cubepi[tracing]" opentelemetry-exporter-otlp-proto-http+```++```python+from opentelemetry.exporter.otlp.proto.http.trace_exporter import (+ OTLPSpanExporter,+)+from cubepi.tracing import Tracer++tracer = Tracer(+ service_name="my-bot",+ service_version="1.4.2",+ deployment_environment="prod",+ agent_name="assistant",+ exporters=[+ OTLPSpanExporter(+ endpoint="http://otel-collector:4318/v1/traces",+ headers={"x-api-key": "…"}, # 后端专属配置+ ),+ ],+)+```++`service_name`、`service_version`、`deployment_environment` 和 `agent_name`+会作为 OTel Resource 属性(`service.*`、`gen_ai.agent.name`、+`deployment.environment.name`)透传,后端无需额外配置即可对运行进行分组。++## gRPC(OTLP/gRPC)++```bash+pip install "cubepi[tracing]" opentelemetry-exporter-otlp-proto-grpc+```++```python+from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (+ OTLPSpanExporter,+)++exporter = OTLPSpanExporter(endpoint="otel-collector:4317", insecure=True)+tracer = Tracer(service_name="my-bot", exporters=[exporter])+```++## 后端配置示例++以下后端均消费 OTLP,区别仅在于 endpoint 和 auth header。++### Jaeger(>=1.50)++Jaeger 原生支持 OTLP/HTTP,端口 4318:++```python+OTLPSpanExporter(endpoint="http://jaeger:4318/v1/traces")+```++### Grafana Tempo++发送到你的 collector,或直接发送到 Tempo 的 OTLP endpoint:++```python+OTLPSpanExporter(endpoint="http://tempo:4318/v1/traces")+```++### Honeycomb++```python+OTLPSpanExporter(+ endpoint="https://api.honeycomb.io/v1/traces",+ headers={"x-honeycomb-team": HONEYCOMB_API_KEY},+)+```++### Datadog(经由 OTel collector)++在 collector 中配置 Datadog exporter,再将数据发送给它:++```python+OTLPSpanExporter(endpoint="http://otel-collector:4318/v1/traces")+```++Datadog 也直接支持原生 OTLP HTTP——格式相同,URL 不同。++### AWS X-Ray(经由 collector)++OTel collector 内置 AWS X-Ray exporter;与其他 OTLP 目标的配置方式相同。++## 续接上游 trace++`Tracer.attach(agent)` 目前会让每次 agent 运行从自己的 trace 开始——+暂无公开 API 可传入入站 `traceparent`,使 span 嵌套到调用方 HTTP handler+的 trace 中。内部钩子(`Tracer._make_parent_context`)为未来的 `run_scope`+特性预留;在该特性发布之前,agent 运行与周边服务 trace 仅通过 resource+属性(`service.name`、`gen_ai.agent.name`、`cubepi.run_id`)关联。++如果需要立即将上游 trace 延续到 CubePi,可以在调用 `agent.prompt(...)` 之前+手动设置 OTel 当前 span,让 agent 的 span 通过 OTel 的环境上下文继承它——+CubePi 不会覆盖已有的活跃父节点。++出站方向,MCP `tools/call` 会自动将 W3C `traceparent` 注入 HTTP 头,+让下游已埋点的 MCP 服务器能够续接 trace 并写入其自己的后端。++## 组合多个 exporter++可以传入多个 exporter,它们将接收所有 span。常见模式——JSONL 用于本地调试,+OTLP 用于生产后端:++```python+tracer = Tracer(+ service_name="my-bot",+ exporters=[+ JsonlSpanExporter(directory="./cubepi-traces"),+ OTLPSpanExporter(endpoint="https://api.honeycomb.io/v1/traces", headers={…}),+ ],+)+```++## Flush++`Tracer` 底层使用 `BatchSpanProcessor`,span 在后台异步导出。推荐使用+`async with` 形式,它会自动走完所有清理路径:++```python+async with Tracer(...) as tracer, tracer.attached(agent):+ await agent.prompt("...")+# 退出时:+# - detach() 同步执行:关闭所有已取消运行遗留的 span,+# 然后将 flush 调度为 asyncio.Task 并等待完成。+# - tracer.shutdown() 再次 flush(幂等)并关闭 exporter。+```++如果你保存了手动 `tracer.attach(agent)` 返回的 `detach`,它会返回已调度+的 flush `Task`。两种有效的手动模式:++```python+# (a) 双保险——最安全,两者都执行。+finally:+ detach() # 关闭已取消运行的 span,调度 flush+ await tracer.shutdown() # 等待 force_flush,然后关闭 SDK++# (b) 等待 detach——不同时关闭时的单次调用。+finally:+ await detach() # 等待已调度的 flush+```++在非 async 上下文(无正在运行的 event loop)中,`detach()` 返回 `None`——+同步部分已执行,但 flush 需由调用方通过 `await tracer.shutdown()` 完成。++即使以上步骤都遗漏了——例如脚本在到达 `finally` 之前就抛出异常——+`Tracer(atexit_flush=True)`(默认)会注册进程退出处理程序,通过+`BatchSpanProcessor` 同步 flush 缓冲 span。`SIGKILL` / `os._exit` 时不运行;+如需保证必达,请使用 `SimpleSpanProcessor`(每个 span 同步导出)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.mdnew file mode 100644index 0000000..4b48459--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md@@ -0,0 +1,92 @@+---+title: Tracing 概览+description: "CubePi 基于 OpenTelemetry 的 tracing 系统概览——span、属性、exporter 与语义约定。"+sidebar_position: 1+---++# Tracing 概览++CubePi 发出符合+[GenAI Semantic Conventions v1.41](https://opentelemetry.io/docs/specs/semconv/gen-ai/)+的 [OpenTelemetry](https://opentelemetry.io/) span,任何兼容 OTel 的后端+(Jaeger、Tempo、Honeycomb、Datadog、AWS X-Ray、Azure Monitor……)都能直接接收+agent 运行数据,无需额外自定义埋点。++将 `Tracer` 挂载到 `Agent` 后,每次 prompt 都会产生一棵 span 树,可对其进行+聚合、查询,并与服务内其他链路数据关联:++```+trace+└── invoke_agent 14425.8ms [0x1cd97cdb] ← 每次 agent.prompt() 一个+ ├── cubepi.turn 1283.1ms [0x5cfda93e] ← 每次 LLM 往返一个+ │ ├── chat deepseek-v4-flash 1208.7ms tok 6845/68 [0x0d130229]+ │ └── execute_tool subagent 9610.2ms subagent [0x38bdd10a]+ │ └── invoke_agent 9601.0ms [0x8094f99b] ← subagent 运行,嵌套其中+ │ └── cubepi.turn 9598.4ms [0x57c5cfc7]+ │ ├── chat deepseek-v4-flash 1190.3ms [0x8205ca6b]+ │ └── execute_tool web_search 6500.2ms web_search [0xca4e59fc]+ └── cubepi.turn 491.9ms ERROR [0xce25f242]+ └── chat deepseek-v4-flash 427.2ms ERROR [0x0bff68ec]+ └── error: Error code: 400 - ... `tool_use` ids were found without+ `tool_result` blocks immediately after: call_01_...+```++每一层都携带标准 `gen_ai.*` 属性——`gen_ai.operation.name`、+`gen_ai.request.model`、`gen_ai.provider.name`、`gen_ai.usage.input_tokens`、+`gen_ai.usage.output_tokens`、`gen_ai.response.finish_reasons`……++## 开箱即用的功能++- **Tracer** —— 构建 SDK `TracerProvider`,为每个 exporter 挂载一个+ `BatchSpanProcessor`,将 CubePi 事件流接入 span。+- **Meter** —— 用于 OTel 直方图的同级组件:+ `gen_ai.client.operation.duration`、`gen_ai.client.operation.time_to_first_chunk`、+ `gen_ai.client.token.usage`。+- **JsonlSpanExporter** —— 将每条 span 以 JSON 单行写入+ `./cubepi-traces/<date>/<trace_id>.jsonl`。文件按 `trace_id` 分片,+ 因此一个文件包含整条 trace——运行本身加上所有嵌套 subagent 运行(继承同一+ trace)。适合本地开发与离线调试;可与任何支持 JSONL 的 OTel 查看工具配合,+ 也可与 [`cubepi trace` CLI](./cli) 配合。+- **OTLP** —— 通过 `opentelemetry-exporter-otlp-proto-http`(HTTP)或+ `…-grpc` 带入自己的 exporter,传入 `Tracer(exporters=[…])` 即可。+- **W3C trace context 传播** —— 出站 MCP 调用会自动将当前 `traceparent`+ 注入 HTTP 头,让下游已埋点的 MCP 服务器能够续接 trace。+- **`tracer.attached(agent)` / `meter.attached(agent)`** —— 以 RAII 方式+ 包裹 attach/detach 的 async context manager,清理只需一个 `async with`+ 块,无需显式 `try/finally`。+- **`atexit` flush hook** —— `Tracer(atexit_flush=True)`(默认开启)注册+ 进程退出处理程序,在进程正常退出时同步 flush 缓冲 span,即使忘记调用+ `await tracer.shutdown()` 也不会丢失数据。+- **`tracing_context()`** —— 通过 contextvar 作用域块为单次运行设置标签和+ 元数据(`cubepi.tags = ("beta-arm",)`、`cubepi.metadata.user_id = "u-42"`),+ 并发 agent 各自看到独立的值。++## 开销++- 每次 agent 运行一个纯 Python recorder,订阅 agent 的事件流和 provider+ 的监听注册——不做 monkey-patching,不新起线程。+- 每层一个 OTel SDK span。`BatchSpanProcessor` 在后台批量导出,不阻塞热路径。+- **默认不记录任何 payload。** `gen_ai.input.messages`、`gen_ai.output.messages`、+ 原始请求/响应以及工具参数/结果均需通过 `record_content=True` 显式开启,+ 避免意外将 PII 发送到后端。详见[内容记录与脱敏](./content-recording)。++## 各功能使用场景++| 需求 | 使用 |+|---|---|+| 跟踪本地单次 agent 运行并检查 JSONL 文件 | `Tracer` + `JsonlSpanExporter` |+| 发送到 Jaeger / Tempo / Honeycomb / Datadog | `Tracer` + OTLP exporter |+| 在 span 旁边获取延迟与 token 直方图 | `Meter` 与 `Tracer` 并用 |+| 记录 prompt / 模型输出用于评测 | `Tracer(record_content=True)` |+| 在数据离开进程前脱敏 PII | `Tracer(redact=…)` |+| 为运行打上 `user_id` / `session_id` / A-B 分组标签 | `tracing_context(tags=…, metadata=…)` |+| 一行代码完成清理,无需 try/finally | `async with tracer.attached(agent): …` |+| 忘记调用 `shutdown()` 也不丢 span | `Tracer(atexit_flush=True)`(默认) |+| 从上游服务续接 trace | `Tracer(resource=…)` + W3C `traceparent`(MCP 自动,HTTP 手动) |++## 下一步++- [快速开始](./getting-started) —— 安装 extra 并发出第一批 span+- [OTLP 与后端](./otlp) —— 将 CubePi 接入 Jaeger、Tempo、Honeycomb……+- [内容记录与脱敏](./content-recording) —— 安全地记录 prompt 与响应+- [Metrics](./metrics) —— 通过 `Meter` 使用直方图diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/intro.mdx b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/intro.mdxnew file mode 100644index 0000000..989dd39--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/intro.mdx@@ -0,0 +1,74 @@+---+id: intro+title: CubePi+description: "一个 Pythonic、原生异步的 Agent 框架。普通的 async 函数、追加式 checkpointing、三个依赖。"+slug: /+sidebar_position: 0+---++# CubePi++**CubePi** 是一个 Pythonic、原生异步的 Agent 框架,为高性能、可读性和+生产级持久化而设计。它把一次 Agent 运行建模为一个线性的 `while`+循环——可追踪、可调试,没有 DAG、没有图 DSL、没有出人意料的控制流。+整个核心循环你五分钟就能读完。++```bash+pip install cubepi+```++然后跳到 [快速开始](./getting-started/quick-start),五分钟内跑通一个+能调用工具的 Agent。++## 你能拿到什么++- **普通 async 函数,不是图节点。** Agent 循环就是一个对消息轮次的+ `while` 循环。你把工具写成 `async def` 函数,框架负责路由调用并并行+ 执行。+- **统一的流式模式。** 所有 provider 都通过 `MessageStream` 产出+ `StreamEvent`。你用 `async for` 迭代它。没有 callback 注册表,也没有+ 为文本和工具单独的 handler 类型。+- **追加式检查点。** 每一轮只写入新消息,而不是整段对话历史。无论+ 会话多长,数据库 I/O 都是 O(1)——SQLite 适合个人开发,Postgres 适合+ 生产环境。+- **原生多 provider。** Anthropic 和 OpenAI 开箱即用,通过 `Provider`+ Protocol 接入。要新增一个 provider 只需要写一个类。+- **八个 Hook 的中间件。** `transform_context`、`convert_to_llm`、+ `transform_system_prompt`、`before_tool_call`、`after_tool_call`、+ `should_stop_after_turn`、`after_model_response`、`on_run_end`——+ 每一个都有明确的组合规则。没有"节点执行顺序之谜"。+- **MCP 加载器。** 指向任意+ [Model Context Protocol](https://modelcontextprotocol.io) 服务器+ (HTTP 或 stdio),就能拿回一组 `AgentTool`。+- **原生 OpenTelemetry。** 挂一个 `Tracer`,每次 prompt 就产生符合+ GenAI 语义约定的 OTel span 树 —— Jaeger、Tempo、Honeycomb、+ Datadog 等任意 OTLP 后端都能直接接收。默认不记录任何 payload;+ 需要时用 `record_content=True` 配合 `redact` 回调显式开启。+- **人机协同(HITL)。** 在 run 中途挂起 Agent,把决策路由给人工+ (审批 / 拒绝 / 回答),然后继续执行 —— 支持检查点持久化,+ 等待期间进程可以重启。++## 接下来去哪儿++| 如果你想… | 从这里开始 |+|---|---|+| 安装并跑通第一个 Agent | [入门 → 安装](./getting-started/installation) |+| 理解骨架 | [入门 → 核心概念](./getting-started/core-concepts) |+| 接入一个真实的工具 Agent | [指南 → 构建第一个 Agent](./guides/agents/first-agent) |+| 跨重启保留对话 | [指南 → SQLite 检查点](./guides/checkpointing/sqlite) |+| 查找某个具体符号 | [API 参考](./api/) |+| 看完整的可运行示例 | [Recipes](./recipes/weather-agent) |+| 从 langgraph Agent 迁移 | [迁移 → 来自 langgraph](./migration/from-langgraph) |++## 状态++你正在浏览的是 **CubePi 0.7** 发布版文档。这是当前最新的稳定文档,+适用于安装 CubePi 并基于已发布的 0.7 API 构建应用。++CubePi 0.7 新增 provider 级 `generate()`、压缩和子 Agent 中间件、+`on_run_end`、类型化 provider 错误、`Tracer.oneshot()`,以及带 `run_id`+的 checkpointed HITL 恢复能力。未发布的 `main` 分支变更请从右上角版本+选择器切到 **Next** 文档。++源代码、Issue 和讨论都在+[GitHub](https://github.com/cubeplexai/cubepi)。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/migration/from-langgraph.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/migration/from-langgraph.mdnew file mode 100644index 0000000..93e0b2f--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/migration/from-langgraph.md@@ -0,0 +1,232 @@+---+title: 从 langgraph 迁移+description: "将你的 agent 从 LangGraph 迁移到 CubePi。"+---++# 从 langgraph 迁移++CubePi 和 [langgraph](https://github.com/langchain-ai/langgraph) 都用于构建+使用工具的 LLM agent,但两者的思维模型有所不同。+本页将 langgraph 概念映射到 CubePi,让你无需从头学起就能移植代码。++## 思维模型转换++| langgraph | CubePi | 原因 |+|---|---|---|+| **状态图**,包含节点、边、channel | **Agent 循环**,就是一个可读的普通 `while` 循环 | 线性循环比图更易于推理;CubePi 在运行时从不分支 —— 控制流在 middleware 中 |+| **Channels**(类型化状态槽) | **`AgentContext.extra`** + `AgentState.messages` | 一个 dict 加一个消息列表,覆盖了我们见过的所有状态结构 |+| **`StateGraph.add_node(name, fn)`** | middleware hook 或一个工具 | langgraph 节点中的函数在 CubePi 中分为两种角色:工具执行(由模型决定时)和 middleware(始终生效的变换) |+| **`add_edge(a, b)`** / `add_conditional_edges` | 内置:工具 → 下一轮 → 工具 → … | 条件形式(工具调用 → 重新提示)就是循环本身,无需显式构建 |+| **`MemorySaver` / `SqliteSaver` / `PostgresSaver`** | `MemoryCheckpointer` / `SQLiteCheckpointer` / `PostgresCheckpointer` | 思路相同,但采用追加写入 schema,而非完整快照 |+| **`config: {"configurable": {"thread_id": …}}`** | `Agent(thread_id=…)` | 作为 agent 的一等参数 |+| **`stream_mode="messages"` / `"values"` / `"updates"`** | `agent.subscribe(listener)` —— 统一事件流 | 一种模式,十一种事件类型 |+| **以 `@tool` 装饰器修饰的工具函数** | 带有 Pydantic 参数和 async execute 的 `AgentTool` | 更接近 OpenAI/Anthropic 原生形式 |+| **`HumanMessage`、`AIMessage`** | `UserMessage`、`AssistantMessage` | 相同的角色标记消息,只是重命名 |+| **通过 `interrupt_before` / `interrupt_after` 中断** | `agent.steer(...)`、`agent.follow_up(...)`、`agent.abort()` | 命令式控制,而非声明式中断点 |+| **`config_schema`** | `Agent` 的构造函数参数 | 没有独立的 schema 层 |++## 并排对比:一个使用工具的 agent++### langgraph++```python+from typing import TypedDict+from langchain_anthropic import ChatAnthropic+from langgraph.graph import StateGraph, END+from langgraph.prebuilt import ToolNode+from langchain_core.tools import tool+++@tool+def get_weather(city: str) -> str:+ """Get current weather for a city."""+ return f"72°F and sunny in {city}"+++llm = ChatAnthropic(model="claude-sonnet-4-5-20250929").bind_tools([get_weather])++class State(TypedDict):+ messages: list++def call_model(state: State):+ return {"messages": [llm.invoke(state["messages"])]}++def should_continue(state: State):+ last = state["messages"][-1]+ return "tools" if last.tool_calls else END++graph = StateGraph(State)+graph.add_node("llm", call_model)+graph.add_node("tools", ToolNode([get_weather]))+graph.add_edge("__start__", "llm")+graph.add_conditional_edges("llm", should_continue)+graph.add_edge("tools", "llm")+app = graph.compile()++for chunk in app.stream({"messages": [("user", "Weather in Tokyo?")]}):+ print(chunk)+```++### CubePi++```python+import asyncio+from pydantic import BaseModel+from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ return AgentToolResult(content=[TextContent(text=f"72°F and sunny in {params.city}")])+++agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ tools=[AgentTool(+ name="get_weather",+ description="Get current weather for a city.",+ parameters=GetWeatherParams,+ execute=get_weather,+ )],+)+agent.subscribe(lambda e, s=None: print(e.type))+asyncio.run(agent.prompt("Weather in Tokyo?"))+```++CubePi 版本去除了:++- `StateGraph`、边、节点、`END` 哨兵、条件边。+- `ToolNode` 注册表 —— 工具直接传给 `Agent`。+- `should_continue` 函数 —— 循环自己知道是否有工具调用。+- `State` TypedDict —— 状态存在于 agent 上。++## 常见模式映射++### Checkpointing++```python+# langgraph+from langgraph.checkpoint.sqlite import SqliteSaver+graph.compile(checkpointer=SqliteSaver.from_conn_string(":memory:"))++# CubePi+from cubepi.checkpointer import SQLiteCheckpointer+async with SQLiteCheckpointer("agent.db") as cp:+ agent = Agent(..., checkpointer=cp, thread_id="conv-1")+```++CubePi 的追加写入模型每条消息的复杂度为 O(1),与对话长度无关。+langgraph 保存完整快照,随历史记录线性增长。++### 流式输出++```python+# langgraph+for chunk in app.stream(state, stream_mode="messages"):+ if chunk["event"] == "on_chat_model_stream":+ print(chunk["data"]["chunk"].content, end="")++# CubePi+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="")++agent.subscribe(on_event)+await agent.prompt("…")+```++一个订阅者,一个流 —— 无需模式标志。++### 中断 / 人机协同++```python+# langgraph+graph.compile(interrupt_before=["tools"])++# CubePi+class HumanApproval(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ approved = await ask_human(f"Run {ctx.tool_call.name}({ctx.args})?")+ if not approved:+ return BeforeToolCallResult(block=True, reason="rejected")+ return None+```++通过 middleware 实现命令式中断。你可以针对每次调用做决策,而不是+配置图级别的中断点。++### 分支++```python+# langgraph+graph.add_conditional_edges("llm", lambda s: "tools" if s["messages"][-1].tool_calls else "summary")+graph.add_node("summary", summarize)+graph.add_edge("summary", END)+```++```python+# CubePi+class SummariseAtEnd(Middleware):+ async def should_stop_after_turn(self, ctx) -> bool:+ msg = ctx.message+ if not any(isinstance(c, ToolCall) for c in msg.content):+ # No more tool calls; we're done. Inject a summary turn first.+ ...+ return True+ return False+```++没有内置的分支原语;流程控制通过 `should_stop_after_turn` 和+`after_model_response` 完成。++## langgraph 有而 CubePi 暂无的功能++- **多 agent 监管模式。** 没有"agent 派生 agent"的原生抽象。+ 你可以通过运行多个共享工具的 `Agent` 实例来实现。+- **可视化图渲染。** 没有 `app.get_graph().draw_mermaid()` 的等价物。+ CubePi 的流程是线性的,画出来也只是一条直线。+- **在任意检查点进行时间旅行 / 分叉。** Postgres schema 有分叉列,+ 但 v0.4 中没有对应的 API 接口。+- **原生 trace 可视化 UI。** CubePi 不像 LangSmith / Langfuse 那样+ 渲染自己的 trace 可视化界面;它改为发射符合厂商中立标准的+ OpenTelemetry —— 通过+ `Tracer(exporters=[OTLPSpanExporter(...)])` 将任意 OTLP 后端+ (LangSmith 的 OTel endpoint、Langfuse v3、Jaeger、Tempo、+ Honeycomb、Datadog 等)接入。参见+ [Tracing → OTLP & Backends](../guides/tracing/otlp)。++## CubePi 有而 langgraph 没有的功能++- **原生 OpenTelemetry tracing** —— `Tracer` + `Meter` 开箱即用地+ 发射带 GenAI-semconv 属性的 OTel span,可被任意 OTLP 后端采集。+ 参见 [Tracing → Overview](../guides/tracing/overview)。+- **原生 async-first** —— 每个入口都是 async,没有+ `app.invoke` vs. `app.ainvoke` 的分裂。+- **追加写入持久化** —— O(1) 数据库写入,消息可用 JSONB 查询。+- **3 个核心依赖** vs. langchain-core + langgraph-sdk + 传递依赖。+- **流式逼真的测试 provider**(`FauxProvider`)随包附带。+- **MCP loaders** 支持 HTTP + stdio 传输。++## 移植检查清单++1. 将 `StateGraph` 构造替换为单个 `Agent(...)` 调用。+2. 将 `@tool` 装饰的函数移至 `AgentTool` 实例(Pydantic 模型作参数,+ async execute)。+3. 将 `MemorySaver` / `SqliteSaver` / `PostgresSaver` 替换为+ `MemoryCheckpointer` / `SQLiteCheckpointer` / `PostgresCheckpointer`。+4. 将 `stream_mode` 回调替换为 `agent.subscribe(...)`。+5. 将做消息变换的自定义节点转换为 `Middleware` hook。+6. 将 `interrupt_before/after` 转换为 `before_tool_call` /+ `after_model_response` middleware。+7. 如果你有 `summary` 或 `route` 节点 —— 将其折叠进+ `after_model_response`,使用 `decision="stop"` 或 `"loop_to_model"`。++## 另请参见++- [核心概念](../getting-started/core-concepts) —— 你正在映射的构建块。+- [Middleware → 组合](../guides/middleware/composition) —— 流程控制逻辑所在之处。+- [Checkpointing](../guides/checkpointing/sqlite) —— 新的持久化方案。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/ask-user-form.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/ask-user-form.mdnew file mode 100644index 0000000..a9d299b--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/ask-user-form.md@@ -0,0 +1,182 @@+---+title: 通过 ask_user 实现多问题表单+description: "使用 CubePi 的 ask_user HITL 工具构建多问题表单以收集结构化用户输入。"+---++# 配方:通过 `ask_user` 实现多问题表单++适用场景:agent 在继续之前需要用户的**结构化答案** —— 配置向导、+偏好选择器、功能开关。++## 步骤 1:注册工具++```python+from cubepi.agent.agent import Agent+from cubepi.hitl import InMemoryChannel, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=...,+ system_prompt=(+ "当您需要用户在选项中选择时,请使用 ask_user 工具。"+ "对于自由形式的澄清问题,直接用文本结束当前轮次——"+ "用户的下一条消息就是答案。"+ ),+ tools=[ask_user_tool(channel)],+ channel=channel,+)+```++`ask_user` 工具像其他工具一样注册。它的+`execution_mode="sequential"` 使工具批次逐一执行——+HITL 暂停不能与并行工具执行重叠。++## 步骤 2:宿主渲染表单++模型调用 `ask_user` 并传入一个问题对象列表。宿主在 channel 上收到+一个 `AskRequest` payload:++```python+async def host():+ async for req in channel.subscribe():+ if req.payload.kind == "ask":+ answers = {}+ for q in req.payload.questions:+ if q.options is None:+ # 自由文本问题+ answers[q.key] = await my_ui.text_input(q.prompt)+ elif q.multi_select:+ answers[q.key] = await my_ui.checkbox_group(+ q.prompt, [(o.label, o.value) for o in q.options],+ )+ else:+ answers[q.key] = await my_ui.radio_group(+ q.prompt,+ [(o.label, o.value) for o in q.options],+ allow_input_indexes=[+ i for i, o in enumerate(q.options) if o.allow_input+ ],+ )+ await channel.answer(req.question_id, answers)+```++## 模型看到的工具参数++模型可以传入混合了自由文本、单选和多选字段的问题:++```json+{+ "questions": [+ {+ "key": "project_type",+ "prompt": "什么类型的项目?",+ "options": [+ {"label": "Web 应用", "value": "web"},+ {"label": "CLI 工具", "value": "cli"},+ {"label": "库", "value": "lib"}+ ]+ },+ {+ "key": "framework",+ "prompt": "哪个框架?",+ "options": [+ {"label": "React", "value": "react"},+ {"label": "Vue", "value": "vue"},+ {"label": "其他", "value": "other", "allow_input": true}+ ]+ },+ {+ "key": "features",+ "prompt": "你需要哪些功能?",+ "multi_select": true,+ "options": [+ {"label": "认证", "value": "auth"},+ {"label": "支付", "value": "payments"},+ {"label": "文件上传", "value": "uploads"}+ ]+ },+ {+ "key": "project_name",+ "prompt": "这个项目叫什么名字?"+ }+ ]+}+```++## 答案结构++宿主用一个 `key → value` 的 dict 回答:++```python+# 上述表单的答案示例:+{+ "project_type": "web",+ "framework": "svelte", # 用户选择了"其他"并输入了"svelte"+ "features": ["auth", "uploads"],+ "project_name": "my-saas"+}+```++答案被填入工具结果的 `details["hitl"]["answers"]`。+模型可以通过文本内容看到人类可读的摘要,也可以通过 dict 进行结构化消费。++## 取消与超时++如果宿主通过 `channel.cancel(qid, reason)` 取消:++```python+await channel.cancel(req.question_id, reason="user closed the form")+```++工具会向模型显示一个错误结果:++```+tool_result.is_error = True+tool_result.details["hitl"]["outcome"] = "cancelled"+tool_result.details["hitl"]["reason"] = "user closed the form"+```++如果超时到期:++```+tool_result.details["hitl"]["outcome"] = "timed_out"+tool_result.details["hitl"]["seconds"] = 30.0+```++两种情况下模型都会看到干净的错误结果,并能做出相应反应——+再次提问、回退到默认值或向用户报告。++## 进程内示例(完整可运行代码段)++```python+import asyncio+from cubepi.agent.agent import Agent+from cubepi.hitl import InMemoryChannel, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=...,+ tools=[ask_user_tool(channel)],+ channel=channel,+)++async def host():+ async for req in channel.subscribe():+ if req.payload.kind == "ask":+ answers = {+ q.key: q.options[0].value if q.options else ""+ for q in req.payload.questions+ }+ await channel.answer(req.question_id, answers)++async def main():+ host_task = asyncio.create_task(host())+ try:+ await agent.prompt("Scaffold a new project.")+ finally:+ host_task.cancel()++asyncio.run(main())+```diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/multi-provider-failover.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/multi-provider-failover.mdnew file mode 100644index 0000000..65357ee--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/multi-provider-failover.md@@ -0,0 +1,255 @@+---+title: 多 Provider 故障转移+description: "在 CubePi 中配置多 provider 故障转移以提升弹性 —— provider 间自动降级。"+---++# Recipe:多 Provider 故障转移++当 Anthropic 触发限速或发生故障时,自动切换到 OpenAI 而不让 agent 崩溃。+我们将把两个 provider 封装在一个单独的 `Provider` 适配器后面,+由该适配器自行实现重试/故障转移逻辑。++**预计耗时:** 10 分钟。+**依赖:** `cubepi`、`ANTHROPIC_API_KEY`、`OPENAI_API_KEY`。++## 封装 provider++```python title="failover.py"+import asyncio+import logging+import time+from typing import Sequence++from cubepi.providers.base import (+ AssistantMessage,+ BaseProvider,+ BoundModel,+ Message,+ MessageStream,+ Model,+ StreamEvent,+ StreamOptions,+ ToolDefinition,+ Usage,+)+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider++log = logging.getLogger(__name__)+++class FailoverProvider(BaseProvider):+ """按顺序尝试各 provider;在构造时或首个事件出错时进行故障转移。++ 内置 provider 会吞掉 API/网络错误,并将其作为+ `StreamEvent(type="error")` 暴露在返回的流上 —— 而不是从+ `provider.stream()` 抛出异常。因此我们先查看每个内部流的第一个事件,+ 只有当看到非错误事件时才确认切换到该 provider。++ 限制:*首个事件之后*发生的错误(如流传输过半时的限速、服务端断连)+ 会原样转发给 agent。对故障 provider 完整重放半条流式响应+ 需要缓冲整轮内容 —— 超出本 recipe 范围。+ """++ def __init__(self, primary: BoundModel, *fallbacks: BoundModel) -> None:+ super().__init__(provider_id=primary.spec.provider_id)+ self._chain: list[BoundModel] = [primary, *fallbacks]++ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream:+ last_error: str | None = None++ for bound_model in self._chain:+ provider = bound_model.provider+ mapped_model = bound_model.spec+ # 构造时失败(罕见 —— 大多数错误留在生产者任务内部)。+ try:+ inner = await provider.stream(+ mapped_model,+ messages,+ system_prompt=system_prompt,+ tools=tools,+ options=options,+ )+ except Exception as e:+ log.warning("provider %s failed at construction: %s", mapped_model.provider_id, e)+ last_error = repr(e)+ continue++ # 查看第一个事件以判断流是否健康。+ iterator = inner.__aiter__()+ try:+ first = await iterator.__anext__()+ except StopAsyncIteration:+ last_error = "stream ended before producing any events"+ continue++ if first.type == "error":+ log.warning("provider %s errored on first event: %s",+ mapped_model.provider_id, first.error_message)+ last_error = first.error_message or "stream error"+ continue++ # 健康 —— 提交到此 provider。通过全新的外层 MessageStream+ # 转发 `first` 及后续事件,让调用方看到从头开始的完整流。+ outer = MessageStream()++ async def _forward(first_event=first, src=iterator, src_stream=inner):+ try:+ outer.push(first_event)+ async for ev in src:+ outer.push(ev)+ final = await src_stream.result()+ outer.set_result(final)+ except Exception as exc:+ fallback_msg = AssistantMessage(+ content=[],+ stop_reason="error",+ error_message=str(exc),+ usage=Usage(),+ timestamp=time.time(),+ )+ outer.push(StreamEvent(type="error", error_message=str(exc)))+ outer.set_result(fallback_msg)++ outer.attach_task(asyncio.create_task(_forward()))+ return outer++ raise RuntimeError(f"all providers exhausted; last error: {last_error!r}")+```++## 使用方式++```python title="main.py"+import asyncio+import os++from cubepi import Agent+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider+from failover import FailoverProvider+++async def main():+ anthropic = AnthropicProvider(+ provider_id="anthropic",+ api_key=os.environ["ANTHROPIC_API_KEY"],+ )+ openai = OpenAIProvider(+ provider_id="openai",+ api_key=os.environ["OPENAI_API_KEY"],+ )+ failover = FailoverProvider(+ anthropic.model("claude-sonnet-4-5-20250929"),+ openai.model("gpt-5"),+ )++ # 此处传入的 model 会被 FailoverProvider 内部覆盖;可传任意占位符。+ # 我们使用主 provider 的 model,以便用量统计标签与正常路径保持一致。+ agent = Agent(+ model=failover.model("claude-sonnet-4-5-20250929"),+ system_prompt="You answer concisely.",+ )+ agent.subscribe(lambda e, s=None: None)+ await agent.prompt("Capital of Mongolia?")+ last = agent.state.messages[-1]+ print(last.content[0].text)+++asyncio.run(main())+```++## 更智能的故障转移策略++上面的示例在**任何**错误事件时都会降级。这对于 `RateLimitError`、+`APIConnectionError` 或 5xx 是合理的 —— 但对于 `BadRequestError`+则未必正确(代码有误;下一个 provider 也会以同样方式失败)。++首个事件的 `error_message` 来自底层 SDK 异常的 `str(exc)`。+可以按子字符串过滤,或者 —— 更好的做法 —— 封装每个 provider 的+`_produce` 方法,为错误打上类别标签:++```python+NON_RETRYABLE_HINTS = ("bad request", "invalid_request_error", "401", "403")++if first.type == "error":+ msg = (first.error_message or "").lower()+ if any(h in msg for h in NON_RETRYABLE_HINTS):+ raise RuntimeError(f"non-retryable error from {mapped_model.provider_id}: {msg}")+ last_error = first.error_message+ continue+```++更健壮的方法是 fork 内置 provider 并从 `_produce` 中重新抛出+特定的 SDK 异常,使其作为真正的 Python 异常到达 `provider.stream()` ——+但这需要对 CubePi 本身做较大改动。++## 添加熔断器++不要持续重试一个明显已宕机的 provider。一个简单的计数器:++```python+import time++class CircuitBreaker:+ def __init__(self, failure_threshold: int = 3, recovery_seconds: float = 60) -> None:+ self._failures = 0+ self._opened_at: float | None = None+ self._threshold = failure_threshold+ self._recovery = recovery_seconds++ def can_attempt(self) -> bool:+ if self._opened_at and time.monotonic() - self._opened_at < self._recovery:+ return False+ if self._opened_at:+ self._opened_at = None # half-open+ return True++ def record_failure(self) -> None:+ self._failures += 1+ if self._failures >= self._threshold:+ self._opened_at = time.monotonic()+ self._failures = 0++ def record_success(self) -> None:+ self._failures = 0+```++在 `FailoverProvider` 中为每个 provider 持有一个 `CircuitBreaker`,+若 `can_attempt()` 返回 False 则跳过该 provider。++## 按工具故障转移不适用++本 recipe 处理的是 **provider** 故障。工具故障是另一回事 ——+参见 [Middleware → 重试](../guides/middleware/examples#retries-with-backoff)+了解该模式。++## 常见陷阱++- **不同 provider 的工具 schema 不同** —— 两个内置 provider 都接受相同的+ `ToolDefinition`,但 extra-body 定制(如 OpenAI 的+ `parallel_tool_calls=False`)不会带到 Anthropic。请将跨 provider 的+ 行为放在 [`transform_context`](../guides/middleware/hooks#transform_context)+ 中,而非 `extra_body`。+- **成本不同** —— 从 Anthropic 故障转移到 OpenAI 会改变每 token 的费用。+ 跟踪是哪个 provider 响应(通过 `on_response` 或+ `AssistantMessage.provider_id`)并据此计费。+- **流一致性** —— 封装器通过全新的 `MessageStream` 转发事件,因此+ 消费者无论哪个 provider 响应都能看到相同的 `StreamEvent` 结构。+ 原始的 `start` 事件由内部 provider 原样传递。+- **流传输中途的错误无法恢复** —— 一旦看到健康的首个事件,封装器即+ 提交到该 provider。如果在长响应传输途中出错,agent 会看到该错误。+ 完整的中途重放需要缓冲 —— 超出本 recipe 范围。++## 另请参见++- [Providers / Anthropic](../guides/providers/anthropic) 和+ [OpenAI](../guides/providers/openai) —— provider 专属细节。+- [编写自定义 Provider](../guides/providers/custom) —— 本封装器使用的同一 Protocol。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/persistent-chat.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/persistent-chat.mdnew file mode 100644index 0000000..91bc33f--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/persistent-chat.md@@ -0,0 +1,170 @@+---+title: 持久化聊天+description: "使用 CubePi 和 SQLiteCheckpointer 构建持久化聊天应用。"+---++# Recipe:持久化聊天(SQLite)++一个能在重启后恢复的 REPL 聊天程序。对话历史保存在 SQLite 文件中;+每位用户拥有独立的 `thread_id`。++**预计耗时:** 5 分钟。+**依赖:** `cubepi[sqlite]`、`ANTHROPIC_API_KEY`。++## 脚本++```python title="chat.py"+import asyncio+import os+import sys++from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main(thread_id: str):+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])++ async with SQLiteCheckpointer("chat.db") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a concise, friendly assistant.",+ checkpointer=cp,+ thread_id=thread_id,+ )++ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "agent_end":+ print()++ agent.subscribe(on_event)++ print(f"chatting on thread {thread_id!r}. Ctrl-D to quit.\n")+ loop = asyncio.get_event_loop()+ while True:+ try:+ user_input = await loop.run_in_executor(None, input, "you> ")+ except EOFError:+ print()+ return+ if not user_input.strip():+ continue+ print("ai > ", end="", flush=True)+ await agent.prompt(user_input)+++if __name__ == "__main__":+ asyncio.run(main(thread_id=sys.argv[1] if len(sys.argv) > 1 else "default"))+```++运行:++```bash+pip install "cubepi[sqlite]"+export ANTHROPIC_API_KEY=sk-…+python chat.py alice+# 聊一会儿,然后按 Ctrl-D。++python chat.py alice+# 历史记录已恢复。问"你刚才告诉你什么了?" —— 模型+# 记得上一次会话的内容。++python chat.py bob+# 不同的 thread,空白起点。+```++## 运行原理++- **每个进程的第一次 `prompt()` 会加载历史记录。** CubePi 在第一次+ prompt 开始时检查一次 checkpointer,恢复 `agent.state.messages`,+ 然后继续。+- **每个 `message_end` 追加写入数据库。** 没有批处理,没有有损缓冲。+ 如果进程在流传输中途崩溃,下次运行会从最后一条已提交的消息继续。+- **system prompt 不会持久化** —— 它是 agent 构造的一部分,而不是+ 状态。请将其保留在代码中(或环境配置中)。++## 多用户路由++在 web 服务中,从已认证的用户信息派生 `thread_id`:++```python+agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id=f"user-{user_id}",+)+```++你可以从一个 `SQLiteCheckpointer` 为数千名用户提供服务 ——+各线程通过 `thread_id` 隔离。++## 清理 thread++设计上没有内置的"删除 thread" API —— checkpointer 是追加写入的。+如需清理,直接按 `thread_id` 删除行:++```bash+sqlite3 chat.db "DELETE FROM messages WHERE thread_id='alice'; DELETE FROM thread_extra WHERE thread_id='alice'"+```++或者实现一个小型管理工具,按需执行 SQL。++## 滑动上下文窗口++在长对话后,模型的上下文会变得昂贵。添加一个+[`SlidingWindow`](../guides/middleware/examples#sliding-window-truncation)+middleware:++```python+from cubepi import Middleware++class SlidingWindow(Middleware):+ def __init__(self, n: int) -> None:+ self.n = n++ async def transform_context(self, messages, *, ctx, signal=None):+ return messages[-self.n:] if len(messages) > self.n else messages+++agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id=thread_id,+ middleware=[SlidingWindow(40)],+)+```++数据库保留所有消息;模型只看到最近的 40 条。用户可见的历史记录+(如聊天界面渲染历史对话)保持完整。++## 切换到 Postgres++代码相同,仅替换 checkpointer:++```python+from cubepi.checkpointer import PostgresCheckpointer++async with PostgresCheckpointer("postgresql://…") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id=…)+```++Postgres 适合多实例服务或大量并发用户 ——+参见 [Postgres + FastAPI](./postgres-fastapi)。++## 常见陷阱++- **忘记 `async with`** —— 没有它,SQLite 连接永远不会被打开。+ 你会得到 `AssertionError`。请务必包裹。+- **多个进程写入同一个 `thread_id`** —— 历史记录会交叉混乱。+ 一个 thread 对应一个 agent,或者迁移到 Postgres。+- **`chat.db` 放在 `/tmp`** —— 某些操作系统重启时会清空 `/tmp`。+ 用户数据请使用 `~/.local/share/myapp/chat.db` 或类似路径。++## 另请参见++- [多轮对话](../guides/agents/multi-turn) —— `steer`、`follow_up`、`resume`。+- [SQLite Checkpointing](../guides/checkpointing/sqlite) —— 后端详细说明。+- [可恢复长任务](./resumable-tasks) —— 当崩溃发生在工具执行中途,而非轮次之间时。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/postgres-fastapi.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/postgres-fastapi.mdnew file mode 100644index 0000000..67194d8--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/postgres-fastapi.md@@ -0,0 +1,248 @@+---+title: Postgres + FastAPI 服务+description: "使用 PostgresCheckpointer 将 FastAPI 后端的 CubePi agent 部署到生产环境。"+---++# Recipe:Postgres + FastAPI 服务++一个生产形态的 HTTP 服务,用于封装 CubePi agent:以 FastAPI 做路由,+Server-Sent Events 做流式传输,共享的 `PostgresCheckpointer` 做持久化,+`thread_id` 从已认证用户派生。++**预计耗时:** 30 分钟。+**依赖:** `cubepi[postgres]`、`fastapi`、`uvicorn[standard]`、+`sse-starlette`、已运行并应用 CubePi schema 的 Postgres 实例。++## 先建 Schema++在服务启动之前,运行 CubePi schema 迁移。本 recipe 最快捷的方式:++```bash+psql "$DATABASE_URL" <<'SQL'+CREATE TABLE cubepi_threads (+ thread_id TEXT PRIMARY KEY,+ parent_thread_id TEXT REFERENCES cubepi_threads(thread_id),+ forked_at_seq BIGINT,+ extra JSONB NOT NULL DEFAULT '{}'::jsonb,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now()+);++CREATE TABLE cubepi_messages (+ thread_id TEXT NOT NULL REFERENCES cubepi_threads(thread_id) ON DELETE CASCADE,+ seq BIGINT NOT NULL,+ role TEXT NOT NULL,+ metadata JSONB NOT NULL DEFAULT '{}'::jsonb,+ payload BYTEA NOT NULL,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (thread_id, seq)+) PARTITION BY HASH (thread_id);++-- 64 个哈希分区+DO $$+BEGIN+ FOR i IN 0..63 LOOP+ EXECUTE format(+ 'CREATE TABLE cubepi_messages_p%s PARTITION OF cubepi_messages FOR VALUES WITH (MODULUS 64, REMAINDER %s)',+ i, i+ );+ END LOOP;+END$$;++CREATE INDEX ix_cubepi_messages_metadata_gin+ON cubepi_messages USING gin (metadata jsonb_path_ops);++CREATE TABLE cubepi_schema_version (version INT PRIMARY KEY);+INSERT INTO cubepi_schema_version VALUES (1);+SQL+```++在真实部署中,通过 Alembic 生成此迁移 —— 参见+[Postgres Checkpointing → 通过 Alembic 初始化](../guides/checkpointing/postgres#bootstrapping-via-alembic)。++## 服务代码++```python title="service.py"+import asyncio+import os+from contextlib import asynccontextmanager+from typing import AsyncIterator++from fastapi import FastAPI, Depends, HTTPException+from pydantic import BaseModel+from sse_starlette.sse import EventSourceResponse++from cubepi import Agent, Model+from cubepi.checkpointer import PostgresCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++# --- 应用生命周期 ----------------------------------------------------------++_provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+_checkpointer: PostgresCheckpointer | None = None+++@asynccontextmanager+async def lifespan(app: FastAPI):+ global _checkpointer+ _checkpointer = await PostgresCheckpointer(+ os.environ["DATABASE_URL"],+ min_pool_size=2,+ max_pool_size=20,+ ).__aenter__()+ yield+ await _checkpointer.__aexit__(None, None, None)+++app = FastAPI(lifespan=lifespan)+++# --- 认证(存根 —— 替换为你的真实认证逻辑)--------------------------------++async def current_user_id() -> str:+ # 生产中:解码 JWT、查找 session 等。+ return "demo-user"+++# --- 路由 ------------------------------------------------------------------++class PromptBody(BaseModel):+ text: str++[email protected]("/chat/{conversation_id}/messages")+async def post_message(+ conversation_id: str,+ body: PromptBody,+ user_id: str = Depends(current_user_id),+):+ thread_id = f"{user_id}:{conversation_id}"++ async def event_generator() -> AsyncIterator[dict]:+ agent = Agent(+ model=_provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a helpful assistant.",+ checkpointer=_checkpointer,+ thread_id=thread_id,+ )++ queue: asyncio.Queue = asyncio.Queue()+ agent.subscribe(lambda e, s=None: queue.put_nowait(e))++ async def run():+ try:+ await agent.prompt(body.text)+ finally:+ queue.put_nowait(None) # sentinel++ task = asyncio.create_task(run())++ try:+ while True:+ event = await queue.get()+ if event is None:+ break+ # 向客户端发送一小部分事件。+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ yield {"event": "delta", "data": event.stream_event.delta}+ elif event.type == "tool_execution_start":+ yield {"event": "tool_start", "data": event.tool_name}+ elif event.type == "agent_end":+ yield {"event": "done", "data": ""}+ finally:+ await task++ return EventSourceResponse(event_generator())++[email protected]("/chat/{conversation_id}/history")+async def get_history(+ conversation_id: str,+ user_id: str = Depends(current_user_id),+):+ thread_id = f"{user_id}:{conversation_id}"+ data = await _checkpointer.load(thread_id)+ if data is None:+ return {"messages": []}+ return {+ "messages": [m.model_dump(mode="json") for m in data.messages],+ }+```++运行:++```bash+pip install "cubepi[postgres]" fastapi "uvicorn[standard]" sse-starlette+export DATABASE_URL=postgresql://user:pass@localhost/cubepi+export ANTHROPIC_API_KEY=sk-…+uvicorn service:app --reload --port 8000+```++测试:++```bash+curl -N -X POST http://localhost:8000/chat/conv1/messages \+ -H "content-type: application/json" \+ -d '{"text":"hi"}'+# event: delta+# data: Hello+# event: delta+# data: !+# event: done+```++## 设计说明++- **每个进程一个 `PostgresCheckpointer`,跨请求共享。**+ 它持有连接池;每个请求单独打开连接会使连接池失去意义。+- **每个请求一个 `Agent`。** Agent 拥有各自的对话状态+ (steering 队列、监听器)。不要复用。+- **`thread_id = f"{user_id}:{conversation_id}"`** —— 通过前缀隔离用户。+ Agent 只读写自己的 thread。+- **SSE 做流式传输。** 每个文本增量作为单独事件发给客户端。+ 工具启动有专属事件类型 —— 客户端无需重建事件处理逻辑即可渲染+ "思考中"指示器。+- **无需负载均衡亲和性。** 状态在 Postgres 中,任何服务实例都能+ 接管任意对话。++## 同一 thread 的并发问题++如果用户双击发送,两个 `POST` 请求会同时到达。两者都创建绑定到同一+`thread_id` 的 `Agent`。Postgres 咨询锁会串行化它们的追加写入,但+**内存中**的状态会分叉 —— 第二个请求的 agent 可能看不到第一个请求+正在进行的消息(`agent.state.messages`)。++对大多数聊天 UI 来说这没问题(客户端控制发送时机)。如果需要严格+排序,可以添加应用层互斥锁(按 `thread_id` 键控的 `asyncio.Lock`)+或队列。++## 生产加固检查清单++- **认证:** 将 `current_user_id()` 替换为真实的 JWT / session 验证。+- **限速:** 在 agent 构造函数中添加+ [`RateLimitMiddleware`](../guides/middleware/examples#rate-limiting),+ 以 `user_id` 为键。+- **成本跟踪:** 订阅 `agent_end`,对每个 `AssistantMessage` 的+ `usage` 求和,写入计费表。+- **可观测性:** 使用 `on_response` 捕获 `anthropic-*` 限速响应头,+ 导出到 Prometheus。+- **备份:** Postgres 原生方式 —— `pg_dump`、时间点恢复。+- **优雅关闭:** uvicorn 的 lifespan handler 会关闭连接池;如有其他+ 资源,添加 `signal.signal(SIGTERM, ...)`。++## 常见陷阱++- **启动时 CubepiSchemaUninitialized** —— 迁移未运行。请先应用 schema。+- **连接池耗尽** —— 默认 `max_pool_size=10`。如果服务的并发 agent+ 数量超过此值,请调大。+- **SSE 在负载均衡器后面** —— 某些负载均衡器会缓冲 SSE。禁用缓冲+ (nginx 用 `X-Accel-Buffering: no`)。+- **长请求超时** —— 重工具 agent 可能运行数分钟。+ 请设置宽松的代理超时并将 uvicorn 的 `--timeout-keep-alive` 设为 600。++## 另请参见++- [Postgres Checkpointing](../guides/checkpointing/postgres) —— 后端深度说明。+- [持久化聊天](./persistent-chat) —— 使用 SQLite 的相同流程。+- [多 Provider 故障转移](./multi-provider-failover) —— 与本服务结合以提升弹性。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/resumable-tasks.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/resumable-tasks.mdnew file mode 100644index 0000000..3b145c6--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/resumable-tasks.md@@ -0,0 +1,216 @@+---+title: 可恢复长任务+description: "使用 CubePi checkpointing 和恢复机制构建崩溃容错的长时间运行任务。"+---++# Recipe:可恢复长任务++当 agent 正在执行一个长时间运行的操作(一系列工具调用、多轮推理会话)+时,如果进程崩溃,你希望能从中断处继续,而不是从头开始。+CubePi 的追加写入 checkpointing 加上 `agent.resume()` 让**轮次之间**+的恢复变得轻而易举;而**工具执行中途**的恢复则需要多一些准备。++**预计耗时:** 15 分钟。+**依赖:** `cubepi[sqlite]`、`ANTHROPIC_API_KEY`。++## 模式概述++有三种崩溃点需要考虑:++1. **轮次之间** —— 模型已回复,无工具可运行,循环在迭代间等待。+ `resume()` 会重新调用模型。*使用 checkpointer 即免费获得。*+2. **工具结果之后、模型调用之前** —— 工具结果已持久化。`resume()`+ 看到最后一条消息是 `ToolResultMessage`,重新调用模型。+ *使用 checkpointer 即免费获得。*+3. **工具执行中途** —— 工具已启动但未完成。尚未持久化任何内容+ (CubePi 只持久化消息)。需要工具内部的幂等性。*需要额外处理。*++本 recipe 重点关注第 3 种情况。++## 带外部状态的幂等工具++模式:每个工具操作都有一个确定性的幂等键。在执行工作之前,先检查+是否已经完成过。++```python title="tools.py"+import os+import json+from pathlib import Path++from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent+++# 简单的文件支撑 job store;生产中替换为 Redis / Postgres。+JOB_DIR = Path(os.environ.get("JOB_DIR", "/tmp/cubepi-jobs"))+JOB_DIR.mkdir(parents=True, exist_ok=True)+++class TranscodeParams(BaseModel):+ source_path: str+ output_path: str+++async def transcode_video(tool_call_id, params: TranscodeParams, *, signal=None, on_update=None):+ job_key = f"transcode:{params.source_path}->{params.output_path}"+ job_file = JOB_DIR / f"{job_key.replace('/', '_')}.json"++ if job_file.exists():+ # 上次运行已完成。+ state = json.loads(job_file.read_text())+ return AgentToolResult(+ content=[TextContent(text=f"Already transcoded to {state['output_path']}.")],+ details=state,+ )++ # 执行实际工作(长时间运行,代价高昂)。+ # 使用 signal 在取消时干净地中止。+ output = await run_ffmpeg(params.source_path, params.output_path, signal=signal)++ # 在工作成功后再写入 job 完成标记。+ job_file.write_text(json.dumps({"output_path": output}))++ return AgentToolResult(+ content=[TextContent(text=f"Transcoded to {output}.")],+ details={"output_path": output},+ )+++transcode_tool = AgentTool(+ name="transcode_video",+ description="Transcode a video file. Idempotent — safe to retry.",+ parameters=TranscodeParams,+ execute=transcode_video,+ execution_mode="sequential", # 一次只转码一个+)+```++现在,如果进程在 `run_ffmpeg` 执行期间崩溃,下次 agent 运行时会发现+`job_file.exists() == False`,重新执行工作,并且只在成功后写入标记。+如果进程在标记**已写入后**崩溃,下次运行会发现标记,立即返回缓存结果,+agent 继续运行,就好像刚刚完成了一样。++## 恢复 agent++```python title="resume.py"+import asyncio+import os+import sys++from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider++from tools import transcode_tool # 上面封装好的 AgentTool+++async def main(thread_id: str, initial_prompt: str | None):+ async with SQLiteCheckpointer("jobs.db") as cp:+ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"]).model("claude-sonnet-4-5-20250929"),+ system_prompt="You orchestrate video transcoding jobs.",+ tools=[transcode_tool],+ checkpointer=cp,+ thread_id=thread_id,+ )+ agent.subscribe(lambda e, s=None: None)++ if initial_prompt:+ # 全新运行。prompt() 在第一次调用前自动加载历史记录,+ # 然后追加新的用户消息。+ await agent.prompt(initial_prompt)+ else:+ # 恢复。agent.resume() 不会自动加载 —— 只有 prompt() 会。+ # 需要先手动恢复 agent 状态。+ data = await cp.load(thread_id)+ if data is None:+ raise RuntimeError(f"No saved state for thread {thread_id!r}")+ agent.state.messages = list(data.messages)+ # `extra` 也会被恢复;它在 Agent 上是私有的,如果你的+ # middleware 需要读取,请使用 checkpointer 的视图。++ # resume 从最后一条持久化消息继续:+ # ToolResultMessage / UserMessage → 重新调用模型+ # 没有排队 steer/follow_up 的 AssistantMessage → 抛出异常+ await agent.resume()+++if __name__ == "__main__":+ thread_id = sys.argv[1]+ initial = sys.argv[2] if len(sys.argv) > 2 else None+ asyncio.run(main(thread_id, initial))+```++工作流:++```bash+# 启动一个 job:+python resume.py job-1 "Transcode /videos/a.mov to /out/a.mp4 and /videos/b.mov to /out/b.mp4"++# 中途终止:Ctrl-C。++# 恢复 —— agent 从最后一条持久化消息继续:+python resume.py job-1+```++## 三种恢复场景的代码++```python+async def smart_resume(agent, cp, thread_id):+ # resume() 不会自动加载 —— 如果 agent 状态为空,先手动恢复。+ if not agent.state.messages:+ data = await cp.load(thread_id)+ if data is None or not data.messages:+ return False # 没有可恢复的内容+ agent.state.messages = list(data.messages)++ last = agent.state.messages[-1]+ last_role = type(last).__name__++ if last_role == "AssistantMessage":+ # 要么自然结束,要么在一轮结束后立即崩溃。+ # 除非有排队的 steering,否则 resume() 会抛出。+ # 最简单的方案:询问用户下一步做什么。+ return False++ # 最后一条是 UserMessage 或 ToolResultMessage —— 可以安全恢复。+ await agent.resume()+ return True+```++## 持久化与中止++`agent.abort()` 触发干净的拆卸并发射 `agent_end`。最后一条**完全持久化**+的消息是通过 `message_end` 提交的那条。工具执行期间的中止不会持久化+工具结果(工具未返回),因此 `resume()` 会使用包含未完成 `ToolCall`+的最后一条 `AssistantMessage` 重新调度模型。模型通常会重新发出调用 ——+你的幂等性保护机制会处理剩下的一切。++## 关于持久化部分工具状态++CubePi 不提供"持久化部分工具结果"的 API。预期的模式是:将部分状态+保存在工具自己的外部存储中(文件系统、Redis、S3),以工具参数为键+进行确定性寻址。上面的 `transcode_video` 用 `JOB_DIR` 实现的就是这种模式。++## 常见陷阱++- **非幂等工具** —— 没有确定性键,重试可能导致信用卡被扣两次或发送+ 重复邮件。始终用幂等键包裹外部副作用。+- **job 标记放在 `/tmp`** —— 重启时会被清除。生产 job 请使用真正的+ 持久化层。+- **在无队列的 assistant 消息后调用 `resume()`** —— 会抛出异常。+ 要么向用户询问下一条消息,要么重新调用 `prompt()`。+- **在全新 agent 上调用 `resume()`** —— 会抛出 `No messages to continue+ from`。`resume()` 不会从 checkpointer 自动加载;只有 `prompt()` 会。+ 请先手动恢复:`agent.state.messages =+ (await cp.load(thread_id)).messages`。+- **忘记在工具内部检查 signal** —— 一个长时间运行的+ `await asyncio.sleep(...)` 或忽略 `signal.is_set()` 的 `for ... in stream`+ 不会响应 `abort`。请在任何热循环内部添加检查。++## 另请参见++- [多轮对话 → `resume()`](../guides/agents/multi-turn#resume--continue-from-the-last-message)+ —— 完整语义。+- [持久化聊天](./persistent-chat) —— 更简单的可重启场景。+- [SQLite Checkpointing](../guides/checkpointing/sqlite) —— 持久化内容与时机。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/sandbox-confirm.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/sandbox-confirm.mdnew file mode 100644index 0000000..0071388--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/sandbox-confirm.md@@ -0,0 +1,137 @@+---+title: 使用 ApprovalPolicyMiddleware 做沙箱确认+description: "使用 CubePi 的 ApprovalPolicyMiddleware 实现沙箱工具确认——自动放行、拒绝或人工确认。"+---++# 配方:使用 `ApprovalPolicyMiddleware` 做沙箱确认++适用场景:一个 web 服务中每个工具调用都经过规则引擎,被分类为自动放行、+硬阻止或人类确认。++## 步骤 1:定义策略函数++策略接收一个 `BeforeToolCallContext` 并返回一个 `ApprovalDecision` ——+`Approve()`、`Deny(reason)` 或 `AskUser(...)`。++```python+from cubepi.hitl import Approve, AskUser, Deny++# 模拟规则引擎 —— 替换为你实际的策略目录。+def classify_command(cmd: str) -> tuple[str, str | None]:+ """(tier, reason) — "allow", "block", 或 "confirm"."""+ if cmd.startswith(("ls", "cat", "head", "grep", "find")):+ return ("allow", None)+ if "rm -rf /" in cmd or cmd.startswith("dd"):+ return ("block", "destructive I/O")+ return ("confirm", "needs human review")+++def sandbox_policy(ctx):+ cmd = ctx.args.cmd # ctx.args 是校验后的 pydantic 模型+ tier, reason = classify_command(cmd)++ if tier == "allow":+ return Approve()+ if tier == "block":+ return Deny(reason=reason or "blocked by policy")+ return AskUser(+ timeout_seconds=180,+ details={"matched_rule": tier, "impact": reason or "unknown"},+ )+```++`ctx.args` 是**校验后的 pydantic 模型**。通过属性访问字段+(`ctx.args.cmd`)。中间件内部会将其转为 dict 用于 channel 的 approve payload,+但你的策略收到的仍然是类型化的模型。++## 步骤 2:接入 agent++```python+from cubepi.agent.agent import Agent+from cubepi.checkpointer.postgres import PostgresCheckpointer+from cubepi.hitl import ApprovalPolicyMiddleware, CheckpointedChannel++async def main():+ async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="session-1")++ agent = Agent(+ model=anthropic.model("claude-sonnet-4-6"),+ system_prompt="You are a helpful assistant with access to a bash shell.",+ tools=[bash_tool],+ middleware=[+ ApprovalPolicyMiddleware(channel, policy=sandbox_policy),+ ],+ channel=channel,+ checkpointer=cp,+ thread_id="session-1",+ )++ await agent.prompt("list files then delete temp logs")+ # Agent 运行中;当 bash 被调用时,sandbox_policy 决定:+ # ls → Approve() → 立即执行+ # rm /tmp/logs → AskUser() → channel 挂起,HitlRequestEvent 触发+```++## 步骤 3:宿主处理挂起的请求++```python+async def host_loop(channel: CheckpointedChannel):+ async for req in channel.subscribe():+ if req.payload.kind == "approve":+ tool_name = req.payload.tool_name+ command = req.payload.args.get("cmd", "")+ details = req.payload.details or {}+ timeout = req.timeout_seconds # 前端倒计时用的秒数++ # 渲染给前端:tool_name、command、details["matched_rule"]、+ # details["impact"]、以及基于 timeout 的倒计时。+ human_answer = await my_frontend.show_confirm(+ tool_name=tool_name,+ command=command,+ details=details,+ timeout=timeout,+ )+ # 根据人类的决定构建 ApproveAnswer。+ from cubepi.hitl import ApproveAnswer+ human_answer = ApproveAnswer(+ decision=ui_response["decision"], # "approve" | "deny" | "edit"+ reason=ui_response.get("reason"), # 仅用于 deny+ edited_args=ui_response.get("edited_args"), # 仅用于 edit+ )+ await channel.answer(req.question_id, human_answer)+ elif req.payload.kind == "ask":+ await channel.answer(req.question_id, await my_frontend.show_form(req))+ else: # confirm+ await channel.answer(req.question_id, await my_frontend.show_confirm(req))+```++## 决策语义++| 人类选择 | 工具结果 | `hitl_trace["decision"]` | 模型看到 |+|---|---|---|---|+| 批准 | 使用原始参数执行 | 未设置(直通,无 HITL 细节) | 正常 `tool_result` |+| 拒绝 | 被阻止 | `"human_deny"` | `tool_result.is_error=True` 及用户的原因 |+| 编辑 | 使用编辑后的参数执行 | `"edit"` + `original_args` / `edited_args` | 正常 `tool_result`(来自编辑后的执行) |++策略决策(不经询问人类直接硬阻止)携带+`hitl_trace["decision"]="policy_deny"`。++## 超时行为++如果人类在 `timeout_seconds` 内未响应,中间件会转换为+`BeforeToolCallResult(block=True, deny_reason="approval_timeout")`。+模型看到 `tool_result.is_error=True`,其中+`details.hitl.decision="timed_out"`,并自然产生一个解释超时的后续轮次。++## 中止++如果用户关闭标签页或管理员终止对话:++```python+await agent.abort_pending(reason="user closed tab")+```++这会干净地关闭对话:为所有未解决的 tool call 追加合成的 deny tool_result,+持久化一个终止性的 `AssistantMessage(stop_reason="aborted")`,并触发+`AgentAbortedEvent`。下一次 `agent.prompt(...)` 重新开始。diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/weather-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/weather-agent.mdnew file mode 100644index 0000000..a011336--- /dev/null+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/recipes/weather-agent.md@@ -0,0 +1,155 @@+---+title: 天气 Agent+description: "使用 CubePi 工具构建一个调用真实 API 数据的天气 agent。"+---++# Recipe:天气 Agent++一个完整可运行的 agent,将真实的天气 API 作为工具调用。+演示了 HTTP 调用工具、错误处理、流式 UI 和取消操作。++**预计耗时:** 5 分钟。+**依赖:** `cubepi`、`httpx`、`ANTHROPIC_API_KEY`。++## 脚本++```python title="weather_agent.py"+import asyncio+import os++import httpx+from pydantic import BaseModel, Field++from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++# --- 工具 -----------------------------------------------------------------++class GetWeatherParams(BaseModel):+ city: str = Field(..., description="The city to look up weather for")+ units: str = Field("metric", pattern="^(metric|imperial)$")+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # 免费的 Open-Meteo 地理编码 + 预报。无需 API key。+ async with httpx.AsyncClient(timeout=10) as client:+ try:+ geo = await client.get(+ "https://geocoding-api.open-meteo.com/v1/search",+ params={"name": params.city, "count": 1, "language": "en"},+ )+ geo.raise_for_status()+ results = geo.json().get("results")+ if not results:+ return AgentToolResult(+ content=[TextContent(text=f"Couldn't find a city named {params.city!r}.")],+ is_error=True,+ )+ lat, lon = results[0]["latitude"], results[0]["longitude"]++ wx = await client.get(+ "https://api.open-meteo.com/v1/forecast",+ params={+ "latitude": lat,+ "longitude": lon,+ "current_weather": True,+ "temperature_unit": "celsius" if params.units == "metric" else "fahrenheit",+ },+ )+ wx.raise_for_status()+ cw = wx.json()["current_weather"]+ unit = "°C" if params.units == "metric" else "°F"+ return AgentToolResult(+ content=[TextContent(text=f"{cw['temperature']}{unit}, wind {cw['windspeed']} km/h in {params.city}.")],+ )+ except httpx.HTTPError as e:+ return AgentToolResult(+ content=[TextContent(text=f"Weather API error: {e}")],+ is_error=True,+ )+++weather_tool = AgentTool(+ name="get_weather",+ description="Get current weather for a city. Returns a short text summary.",+ parameters=GetWeatherParams,+ execute=get_weather,+)+++# --- Agent ----------------------------------------------------------------++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a concise weather assistant. Always use the tool; don't guess.",+ tools=[weather_tool],+ )++ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "tool_execution_start":+ print(f"\n[calling {event.tool_name}({event.args})]")+ elif event.type == "agent_end":+ print()++ agent.subscribe(on_event)++ # 包裹 prompt(),使 Ctrl-C 能干净地取消运行。+ task = asyncio.create_task(agent.prompt("Weather in Tokyo and São Paulo, please."))+ try:+ await task+ except KeyboardInterrupt:+ agent.abort()+ await agent.wait_for_idle()+++if __name__ == "__main__":+ asyncio.run(main())+```++运行:++```bash+pip install cubepi httpx+export ANTHROPIC_API_KEY=sk-…+python weather_agent.py+```++示例输出:++```+[calling get_weather({'city': 'Tokyo', 'units': 'metric'})]+[calling get_weather({'city': 'São Paulo', 'units': 'metric'})]+Tokyo is currently 18°C with a wind speed of 12 km/h. São Paulo is 25°C with winds of 9 km/h.+```++## 运行原理++- **两个工具并行执行。** 模型在同一个 assistant 轮次中发出两次+ `get_weather` 工具调用。CubePi 并发运行它们 ——+ 第二个不等第一个完成。+- **流式文本和工具事件交错。** `on_event` 过滤器同时处理+ `text_delta`(用于最终答案)和 `tool_execution_start`(用于"思考中"指示器)。+- **错误是工具结果,不是异常。** 城市名错误或网络抖动会返回+ `is_error=True`;模型收到错误消息后通常会用不同拼写重试。+- **取消是干净的。** `agent.abort()` 通过 `signal` 传播到正在运行的+ 工具和 provider 流。++## 扩展本 recipe++- **添加缓存:** 按 `city` 记忆化地理编码查询 ——+ Open-Meteo 的坐标是稳定的。+- **添加重试:** 用 [`RetryMiddleware`](../guides/middleware/examples#retries-with-backoff)+ 包裹以处理瞬时 API 错误。+- **持久化对话:** 添加+ [`SQLiteCheckpointer`](../guides/checkpointing/sqlite),使追问+ ("那大阪呢?")能有历史上下文。++## 另请参见++- [构建你的第一个 Agent](../guides/agents/first-agent) —— 使用硬编码工具的相同模式。+- [工具使用与并行执行](../guides/agents/tool-use) —— 更多关于并行工具调用的内容。diff --git a/website/scripts/build_api_reference.py b/website/scripts/build_api_reference.pyindex 9e0bdc9..bb4a6ee 100644--- a/website/scripts/build_api_reference.py+++ b/website/scripts/build_api_reference.py@@ -49,6 +49,8 @@ MODULES = [("cubepi.utils", "Utils", 7),]+REPO_ROOT = Path(__file__).resolve().parents[2]+# ---------------------------------------------------------------------------# Public-surface detection@@ -156,6 +158,19 @@ def _anchor(name: str) -> str:return s+def _source_relpath(filepath: str | Path) -> str:+ """Return a GitHub-linkable source path relative to the repo root."""+ path = Path(str(filepath))+ try:+ return path.resolve().relative_to(REPO_ROOT).as_posix()+ except ValueError:+ parts = path.as_posix().split("/")+ for index in range(len(parts) - 1, -1, -1):+ if parts[index] == "cubepi":+ return "/".join(parts[index:])+ return path.as_posix()++def build_ref_index(modules_to_symbols: dict[str, list]) -> dict[str, str]:"""Build ``Name`` → ``./cubepi-module#anchor`` map for RST refs.@@ -576,9 +591,7 @@ def render_class_members(fp = getattr(member, "filepath", None)ln = getattr(member, "lineno", None)if fp and ln:- rel = Path(str(fp)).as_posix()- if "/cubepi/" in rel:- rel = "cubepi/" + rel.split("/cubepi/", 1)[1]+ rel = _source_relpath(fp)link = f"{github_blob_root}/{rel}#L{ln}"lines.append(f"[source]({link})")lines.append("")@@ -649,9 +662,7 @@ def render_symbol(fp = getattr(symbol, "filepath", None)ln = getattr(symbol, "lineno", None)if fp and ln:- rel = Path(str(fp)).as_posix()- if "/cubepi/" in rel:- rel = "cubepi/" + rel.split("/cubepi/", 1)[1]+ rel = _source_relpath(fp)link = f"{github_blob_root}/{rel}#L{ln}"block.append(f"[source]({link})")block.append("")diff --git a/website/scripts/tests/test_build_api_reference.py b/website/scripts/tests/test_build_api_reference.pyindex 46f2a65..88a78ce 100644--- a/website/scripts/tests/test_build_api_reference.py+++ b/website/scripts/tests/test_build_api_reference.py@@ -122,6 +122,16 @@ def test_anchor_lowercases_and_dehyphens():assert mod._anchor("__init__") == "init"+def test_source_relpath_strips_worktree_prefix():+ mod = _load_module()+ path = (+ "/home/chris/cubepi/.worktrees/2026-06-05-release-0.7-review/"+ "cubepi/agent/agent.py"+ )++ assert mod._source_relpath(path) == "cubepi/agent/agent.py"++def test_build_ref_index_includes_class_members():mod = _load_module()diff --git a/website/sidebars.ts b/website/sidebars.tsindex 863b772..887a806 100644--- a/website/sidebars.ts+++ b/website/sidebars.ts@@ -24,10 +24,11 @@ const sidebars: SidebarsConfig = {'guides/agents/streaming',]},{ type: 'category', label: 'Providers', items: [+ 'guides/providers/overview','guides/providers/anthropic','guides/providers/openai',- 'guides/providers/capability-and-presets','guides/providers/custom',+ 'guides/providers/image-generation',]},{ type: 'category', label: 'Checkpointing', items: ['guides/checkpointing/sqlite',@@ -39,6 +40,8 @@ const sidebars: SidebarsConfig = {'guides/middleware/hooks','guides/middleware/composition','guides/middleware/examples',+ 'guides/middleware/compaction',+ 'guides/middleware/subagents',]},{ type: 'category', label: 'Human-in-the-Loop', items: ['guides/hitl/overview',diff --git a/website/src/components/DocFeedback/index.tsx b/website/src/components/DocFeedback/index.tsxindex f5119f9..1a4d60b 100644--- a/website/src/components/DocFeedback/index.tsx+++ b/website/src/components/DocFeedback/index.tsx@@ -14,7 +14,7 @@ function capture(event: string, payload: object) {if (ph && typeof ph.capture === 'function') ph.capture(event, payload);}-export default function DocFeedback({ slug, version, locale }: Props): JSX.Element {+export default function DocFeedback({ slug, version, locale }: Props): React.ReactElement {const [phase, setPhase] = useState<Phase>('ask');const [comment, setComment] = useState('');diff --git a/website/src/components/Home/WhyTable.tsx b/website/src/components/Home/WhyTable.tsxindex 7437682..8547549 100644--- a/website/src/components/Home/WhyTable.tsx+++ b/website/src/components/Home/WhyTable.tsx@@ -9,7 +9,7 @@ const ROWS_EN: { label: string; langgraph: string; cubepi: string }[] = [{ label: 'Dependencies', langgraph: 'langchain-core, langgraph-sdk, and transitive deps', cubepi: '3 core deps: pydantic, anthropic, openai' },{ label: 'Tool execution', langgraph: 'Tools are graph nodes with manual wiring', cubepi: 'Declare tools as functions; framework routes and parallelizes' },{ label: 'Multi-provider', langgraph: 'Via langchain chat model adapters', cubepi: 'Native Provider protocol — Anthropic, OpenAI built in' },- { label: 'Middleware', langgraph: 'Graph-level middleware on node entry/exit', cubepi: '7 typed hooks with declarative composition rules' },+ { label: 'Middleware', langgraph: 'Graph-level middleware on node entry/exit', cubepi: '8 typed hooks with declarative composition rules' },{ label: 'Observability', langgraph: 'LangSmith / Langfuse integration', cubepi: 'Native OpenTelemetry — Tracer, Meter, GenAI semconv, OTLP / JSONL out of the box' },];@@ -20,7 +20,7 @@ const ROWS_ZH: { label: string; langgraph: string; cubepi: string }[] = [{ label: '依赖项', langgraph: 'langchain-core、langgraph-sdk 及传递依赖', cubepi: '3 个核心依赖:pydantic、anthropic、openai' },{ label: '工具执行', langgraph: '工具是需要手动连线的图节点', cubepi: '声明为函数;框架自动路由并并行执行' },{ label: '多 Provider', langgraph: '通过 langchain chat model 适配器', cubepi: '原生 Provider 协议 — 内置 Anthropic、OpenAI' },- { label: 'Middleware', langgraph: '图级中间件,在节点进出时触发', cubepi: '7 种类型化 hook,声明式组合规则' },+ { label: 'Middleware', langgraph: '图级中间件,在节点进出时触发', cubepi: '8 种类型化 hook,声明式组合规则' },{ label: '可观测性', langgraph: 'LangSmith / Langfuse 集成', cubepi: '原生 OpenTelemetry — Tracer、Meter、GenAI semconv,开箱即用 OTLP / JSONL' },];diff --git a/website/src/components/VersionAwareDocLink.test.tsx b/website/src/components/VersionAwareDocLink.test.tsxnew file mode 100644index 0000000..fd4ec78--- /dev/null+++ b/website/src/components/VersionAwareDocLink.test.tsx@@ -0,0 +1,11 @@+import {describe, expect, it} from 'vitest';++import {buildVersionAwareDocTo} from './versionAwareDocLinkConfig';++describe('VersionAwareDocLink', () => {+ it('links the docs section to the docs root', () => {+ expect(buildVersionAwareDocTo('docs', '/docs')).toBe('/docs');+ expect(buildVersionAwareDocTo('docs', '/docs/next')).toBe('/docs/next');+ expect(buildVersionAwareDocTo('docs', '/docs/0.7')).toBe('/docs/0.7');+ });+});diff --git a/website/src/components/VersionAwareDocLink.tsx b/website/src/components/VersionAwareDocLink.tsxindex f661420..a07646c 100644--- a/website/src/components/VersionAwareDocLink.tsx+++ b/website/src/components/VersionAwareDocLink.tsx@@ -5,29 +5,17 @@ import {useActiveDocContext,useLatestVersion,} from '@docusaurus/plugin-content-docs/client';--type Section = 'docs' | 'api' | 'recipes';+import {+ buildVersionAwareDocTo,+ SECTIONS,+ type Section,+} from './versionAwareDocLinkConfig';// One shared sidebar means a plain `type: 'doc'` item would light up all three// top-nav links at once. We instead drive the active state from the pathname// (per-section regex, tolerant of the optional /zh-Hans locale and /docs/0.4/// version segments) while building a version-aware `to` so clicking stays in// whatever version the reader is currently browsing.-const SECTIONS: Record<Section, {entry: string; activeBaseRegex: string}> = {- docs: {- entry: 'getting-started/installation',- activeBaseRegex:- '^(?:/zh-Hans)?/docs/(?!(?:(?:\\d+\\.\\d+|next)/)?(?:api|recipes)(?:/|$))',- },- api: {- entry: 'api/',- activeBaseRegex: '^(?:/zh-Hans)?/docs/(?:(?:\\d+\\.\\d+|next)/)?api(?:/|$)',- },- recipes: {- entry: 'recipes/weather-agent',- activeBaseRegex: '^(?:/zh-Hans)?/docs/(?:(?:\\d+\\.\\d+|next)/)?recipes(?:/|$)',- },-};type Props = Omit<DefaultNavbarItemProps, 'to' | 'activeBaseRegex'> & {section: Section;@@ -40,13 +28,12 @@ export default function VersionAwareDocLink({const {activeVersion} = useActiveDocContext('default');const latestVersion = useLatestVersion('default');const version = activeVersion ?? latestVersion;- const {entry, activeBaseRegex} = SECTIONS[section];- const base = version.path.replace(/\/$/, '');+ const {activeBaseRegex} = SECTIONS[section];return (<DefaultNavbarItem{...props}- to={`${base}/${entry}`}+ to={buildVersionAwareDocTo(section, version.path)}activeBaseRegex={activeBaseRegex}/>);diff --git a/website/src/components/versionAwareDocLinkConfig.ts b/website/src/components/versionAwareDocLinkConfig.tsnew file mode 100644index 0000000..51222ba--- /dev/null+++ b/website/src/components/versionAwareDocLinkConfig.ts@@ -0,0 +1,23 @@+export type Section = 'docs' | 'api' | 'recipes';++export const SECTIONS: Record<Section, {entry: string; activeBaseRegex: string}> = {+ docs: {+ entry: '',+ activeBaseRegex:+ '^(?:/zh-Hans)?/docs/(?!(?:(?:\\d+\\.\\d+|next)/)?(?:api|recipes)(?:/|$))',+ },+ api: {+ entry: 'api/',+ activeBaseRegex: '^(?:/zh-Hans)?/docs/(?:(?:\\d+\\.\\d+|next)/)?api(?:/|$)',+ },+ recipes: {+ entry: 'recipes/weather-agent',+ activeBaseRegex: '^(?:/zh-Hans)?/docs/(?:(?:\\d+\\.\\d+|next)/)?recipes(?:/|$)',+ },+};++export function buildVersionAwareDocTo(section: Section, versionPath: string): string {+ const base = versionPath.replace(/\/$/, '');+ const {entry} = SECTIONS[section];+ return entry ? `${base}/${entry}` : base;+}diff --git a/website/src/pages/index.tsx b/website/src/pages/index.tsxindex 93d9600..091e75b 100644--- a/website/src/pages/index.tsx+++ b/website/src/pages/index.tsx@@ -8,7 +8,7 @@ import InstallMatrix from '@site/src/components/Home/InstallMatrix';import MetaBar from '@site/src/components/Home/MetaBar';import { useIsZhHans } from '@site/src/hooks/useIsZhHans';-export default function Home(): JSX.Element {+export default function Home(): React.ReactElement {const zh = useIsZhHans();return (<Layoutdiff --git a/website/src/theme/DocItem/Footer/index.tsx b/website/src/theme/DocItem/Footer/index.tsxindex af5a32c..57a895d 100644--- a/website/src/theme/DocItem/Footer/index.tsx+++ b/website/src/theme/DocItem/Footer/index.tsx@@ -5,7 +5,7 @@ import { useLocation } from '@docusaurus/router';import useDocusaurusContext from '@docusaurus/useDocusaurusContext';import DocFeedback from '@site/src/components/DocFeedback';-export default function FooterWrapper(props: any): JSX.Element {+export default function FooterWrapper(props: any): React.ReactElement {const { metadata } = useDoc();const { i18n } = useDocusaurusContext();const { pathname } = useLocation();diff --git a/website/versioned_docs/version-0.6/api/cubepi-agent.mdx b/website/versioned_docs/version-0.6/api/cubepi-agent.mdxindex 2896c92..c149e83 100644--- a/website/versioned_docs/version-0.6/api/cubepi-agent.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-agent.mdx@@ -16,7 +16,7 @@ Agent(self, *, provider: Provider, model: Model, system_prompt: str = '', tools:```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L124)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L124)**Attributes**@@ -46,7 +46,7 @@ Agent(self, *, provider: Provider, model: Model, system_prompt: str = '', tools:subscribe(listener: Callable) -> Callable[[], None]```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L210)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L210)#### `steer` {#agent-steer}@@ -55,7 +55,7 @@ subscribe(listener: Callable) -> Callable[[], None]steer(message: Message) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L216)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L216)#### `cancel_steer` {#agent-cancel-steer}@@ -69,7 +69,7 @@ Remove a not-yet-drained steering message by its steer_id.Returns True if a queued message was removed; False if it was alreadydrained or never queued (best-effort cancel).-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L219)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L219)#### `follow_up` {#agent-follow-up}@@ -78,7 +78,7 @@ drained or never queued (best-effort cancel).follow_up(message: Message) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L227)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L227)#### `abort` {#agent-abort}@@ -87,7 +87,7 @@ follow_up(message: Message) -> Noneabort() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L230)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L230)#### `wait_for_idle` {#agent-wait-for-idle}@@ -96,7 +96,7 @@ abort() -> Nonewait_for_idle() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L234)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L234)#### `reset` {#agent-reset}@@ -105,7 +105,7 @@ wait_for_idle() -> Nonereset() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L238)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L238)#### `prompt` {#agent-prompt}@@ -114,7 +114,7 @@ reset() -> Noneprompt(message: str | Message | list[Message]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L247)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L247)#### `resume` {#agent-resume}@@ -123,7 +123,7 @@ prompt(message: str | Message | list[Message]) -> Noneresume() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L287)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L287)#### `detach` {#agent-detach}@@ -132,7 +132,7 @@ resume() -> Nonedetach() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L385)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L385)#### `load_pending_hitl_request` {#agent-load-pending-hitl-request}@@ -141,7 +141,7 @@ detach() -> Noneload_pending_hitl_request() -> HitlRequest | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L403)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L403)#### `respond` {#agent-respond}@@ -150,7 +150,7 @@ load_pending_hitl_request() -> HitlRequest | Nonerespond(*, question_id: str | None = None, answer: Any) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L411)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L411)#### `abort_pending` {#agent-abort-pending}@@ -167,7 +167,7 @@ the agent signal; Phase 2 (with lock) appends synthetic denytool_results + a terminal stop_reason="aborted" assistant messageand emits AgentAbortedEvent.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L451)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L451)@@ -180,7 +180,7 @@ AgentState(self, system_prompt: str = '', model: Model = (lambda: Model(id='unkn```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/agent.py#L85)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/agent.py#L85)**Attributes**@@ -205,7 +205,7 @@ run_agent_loop(*, prompts: list[Message], context: AgentContext, provider: Provi```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/loop.py#L31)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/loop.py#L31)### run_agent_loop_continue {#run-agent-loop-continue}@@ -217,7 +217,7 @@ run_agent_loop_continue(*, context: AgentContext, provider: Provider, model: Mod```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/loop.py#L88)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/loop.py#L88)### execute_tool_calls {#execute-tool-calls}@@ -229,7 +229,7 @@ execute_tool_calls(context: AgentContext, assistant_message: AssistantMessage, *```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/tools.py#L260)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/tools.py#L260)### AfterToolCallContext {#aftertoolcallcontext}@@ -241,7 +241,7 @@ AfterToolCallContext(self, assistant_message: AssistantMessage, tool_call: ToolC```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L82)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L82)**Attributes**@@ -259,7 +259,7 @@ AfterToolCallContext(self, assistant_message: AssistantMessage, tool_call: ToolC_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L67)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L67)**Attributes**@@ -279,7 +279,7 @@ AgentContext(self, system_prompt: str, messages: list[Message], tools: list[Agen```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L48)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L48)**Attributes**@@ -295,7 +295,7 @@ AgentContext(self, system_prompt: str, messages: list[Message], tools: list[Agen_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L107)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L107)**Attributes**@@ -313,7 +313,7 @@ AgentEvent = AgentStartEvent | AgentEndEvent | TurnStartEvent | TurnEndEvent | M```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L196)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L196)### AgentListener {#agentlistener}@@ -325,7 +325,7 @@ AgentListener = Callable[[AgentEvent, asyncio.Event | None], Awaitable[None] | N```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L215)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L215)### AgentStartEvent {#agentstartevent}@@ -333,7 +333,7 @@ AgentListener = Callable[[AgentEvent, asyncio.Event | None], Awaitable[None] | N_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L103)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L103)**Attributes**@@ -350,7 +350,7 @@ AgentTool(self, name: str, description: str, parameters: type[TParams], execute:```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L30)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L30)**Attributes**@@ -371,7 +371,7 @@ AgentTool(self, name: str, description: str, parameters: type[TParams], execute:to_definition() -> ToolDefinition```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L39)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L39)@@ -380,7 +380,7 @@ to_definition() -> ToolDefinition_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L23)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L23)**Attributes**@@ -400,7 +400,7 @@ BeforeToolCallContext(self, assistant_message: AssistantMessage, tool_call: Tool```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L74)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L74)**Attributes**@@ -416,7 +416,7 @@ BeforeToolCallContext(self, assistant_message: AssistantMessage, tool_call: Tool_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L59)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L59)**Attributes**@@ -433,7 +433,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L133)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L133)**Attributes**@@ -447,7 +447,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L122)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L122)**Attributes**@@ -461,7 +461,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L127)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L127)**Attributes**@@ -480,7 +480,7 @@ ShouldStopAfterTurnContext(self, message: AssistantMessage, tool_results: list[T```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L92)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L92)**Attributes**@@ -496,7 +496,7 @@ ShouldStopAfterTurnContext(self, message: AssistantMessage, tool_results: list[T_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L153)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L153)**Attributes**@@ -516,7 +516,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L138)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L138)**Attributes**@@ -532,7 +532,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L145)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L145)**Attributes**@@ -549,7 +549,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L116)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L116)**Attributes**@@ -564,7 +564,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/agent/types.py#L112)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/agent/types.py#L112)**Attributes**@@ -581,7 +581,7 @@ emit_event(emit_fn: Callable, event: object) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/utils/emit.py#L7)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/utils/emit.py#L7)diff --git a/website/versioned_docs/version-0.6/api/cubepi-checkpointer.mdx b/website/versioned_docs/version-0.6/api/cubepi-checkpointer.mdxindex 1775bd8..e6f4224 100644--- a/website/versioned_docs/version-0.6/api/cubepi-checkpointer.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-checkpointer.mdx@@ -12,7 +12,7 @@ hide_table_of_contents: false_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L13)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L13)**Methods**@@ -23,7 +23,7 @@ _class_load(thread_id: str) -> CheckpointData | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L15)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L15)#### `append` {#checkpointer-append}@@ -32,7 +32,7 @@ load(thread_id: str) -> CheckpointData | Noneappend(thread_id: str, messages: list[Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L16)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L16)#### `save_extra` {#checkpointer-save-extra}@@ -41,7 +41,7 @@ append(thread_id: str, messages: list[Any]) -> Nonesave_extra(thread_id: str, extra: dict[str, Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L17)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L17)#### `save_pending_request` {#checkpointer-save-pending-request}@@ -56,7 +56,7 @@ First-party implementations (Memory, SQLite, Postgres, MySQL) all implement thisHITL-requiring features (Agent.respond, CheckpointedChannel) use`getattr(checkpointer, "save_pending_request", None)` for graceful degradation.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L19)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L19)#### `load_pending_request` {#checkpointer-load-pending-request}@@ -69,7 +69,7 @@ Load the persisted pending HITL request for a thread, or None.Returns a `HitlRequest` instance or `None`.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L28)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L28)@@ -82,7 +82,7 @@ CheckpointData(self, messages: list[Any] = list(), extra: dict[str, Any] = dict(```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/base.py#L7)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/base.py#L7)**Attributes**@@ -100,7 +100,7 @@ MemoryCheckpointer(self)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L9)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L9)**Methods**@@ -111,7 +111,7 @@ MemoryCheckpointer(self)load(thread_id: str) -> CheckpointData | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L14)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L14)#### `append` {#memorycheckpointer-append}@@ -120,7 +120,7 @@ load(thread_id: str) -> CheckpointData | Noneappend(thread_id: str, messages: list[Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L17)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L17)#### `save_extra` {#memorycheckpointer-save-extra}@@ -129,7 +129,7 @@ append(thread_id: str, messages: list[Any]) -> Nonesave_extra(thread_id: str, extra: dict[str, Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L22)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L22)#### `save_pending_request` {#memorycheckpointer-save-pending-request}@@ -138,7 +138,7 @@ save_extra(thread_id: str, extra: dict[str, Any]) -> Nonesave_pending_request(thread_id: str, request: Any) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L27)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L27)#### `load_pending_request` {#memorycheckpointer-load-pending-request}@@ -147,7 +147,7 @@ save_pending_request(thread_id: str, request: Any) -> Noneload_pending_request(thread_id: str) -> Any```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/memory.py#L33)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/memory.py#L33)@@ -175,7 +175,7 @@ async with cp:Raises CubepiSchemaUninitialized / CubepiSchemaMismatch at __aenter__if the DB schema isn't compatible with this cubepi version.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L46)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L46)**Methods**@@ -186,7 +186,7 @@ if the DB schema isn't compatible with this cubepi version.load(thread_id: str) -> CheckpointData | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L110)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L110)#### `append` {#postgrescheckpointer-append}@@ -195,7 +195,7 @@ load(thread_id: str) -> CheckpointData | Noneappend(thread_id: str, messages: list[Message]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L152)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L152)#### `save_extra` {#postgrescheckpointer-save-extra}@@ -204,7 +204,7 @@ append(thread_id: str, messages: list[Message]) -> Nonesave_extra(thread_id: str, extra: dict[str, Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L200)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L200)#### `save_pending_request` {#postgrescheckpointer-save-pending-request}@@ -213,7 +213,7 @@ save_extra(thread_id: str, extra: dict[str, Any]) -> Nonesave_pending_request(thread_id: str, request) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L213)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L213)#### `load_pending_request` {#postgrescheckpointer-load-pending-request}@@ -222,7 +222,7 @@ save_pending_request(thread_id: str, request) -> Noneload_pending_request(thread_id: str)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L238)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/postgres/checkpointer.py#L238)@@ -235,7 +235,7 @@ SQLiteCheckpointer(self, db_path: str)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L12)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L12)**Methods**@@ -246,7 +246,7 @@ SQLiteCheckpointer(self, db_path: str)load(thread_id: str) -> CheckpointData | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L49)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L49)#### `append` {#sqlitecheckpointer-append}@@ -255,7 +255,7 @@ load(thread_id: str) -> CheckpointData | Noneappend(thread_id: str, messages: list[Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L75)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L75)#### `save_extra` {#sqlitecheckpointer-save-extra}@@ -264,7 +264,7 @@ append(thread_id: str, messages: list[Any]) -> Nonesave_extra(thread_id: str, extra: dict[str, Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L86)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L86)#### `save_pending_request` {#sqlitecheckpointer-save-pending-request}@@ -273,7 +273,7 @@ save_extra(thread_id: str, extra: dict[str, Any]) -> Nonesave_pending_request(thread_id: str, request: Any) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L108)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L108)#### `load_pending_request` {#sqlitecheckpointer-load-pending-request}@@ -282,7 +282,7 @@ save_pending_request(thread_id: str, request: Any) -> Noneload_pending_request(thread_id: str) -> Any```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/sqlite.py#L125)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/sqlite.py#L125)@@ -310,7 +310,7 @@ async with cp:Raises CubepiSchemaUninitialized / CubepiSchemaMismatch at __aenter__ if theDB schema isn't compatible with this cubepi version.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L75)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L75)**Methods**@@ -321,7 +321,7 @@ DB schema isn't compatible with this cubepi version.load(thread_id: str) -> CheckpointData | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L159)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L159)#### `append` {#mysqlcheckpointer-append}@@ -330,7 +330,7 @@ load(thread_id: str) -> CheckpointData | Noneappend(thread_id: str, messages: list[Message]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L190)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L190)#### `save_extra` {#mysqlcheckpointer-save-extra}@@ -339,7 +339,7 @@ append(thread_id: str, messages: list[Message]) -> Nonesave_extra(thread_id: str, extra: dict[str, Any]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L244)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L244)#### `save_pending_request` {#mysqlcheckpointer-save-pending-request}@@ -348,7 +348,7 @@ save_extra(thread_id: str, extra: dict[str, Any]) -> Nonesave_pending_request(thread_id, request)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L278)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L278)#### `load_pending_request` {#mysqlcheckpointer-load-pending-request}@@ -357,7 +357,7 @@ save_pending_request(thread_id, request)load_pending_request(thread_id)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L309)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/checkpointer/mysql/checkpointer.py#L309)diff --git a/website/versioned_docs/version-0.6/api/cubepi-mcp.mdx b/website/versioned_docs/version-0.6/api/cubepi-mcp.mdxindex ee3fe88..7b6cb9d 100644--- a/website/versioned_docs/version-0.6/api/cubepi-mcp.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-mcp.mdx@@ -44,7 +44,7 @@ wrap initialize/list/call awaits in `asyncio.wait_for` so a serverthat accepts the connection but stalls on protocol messages stillaborts.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/http_loader.py#L112)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/http_loader.py#L112)### load_mcp_tools_stdio {#load-mcp-tools-stdio}@@ -75,7 +75,7 @@ simplicity, no process pooling).- `timeout` — per-call wall-clock timeout for initialize/list/call awaits.A hung server raises asyncio.TimeoutError instead of blocking forever.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/stdio_loader.py#L16)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/stdio_loader.py#L16)### MCPDiscoveryResult {#mcpdiscoveryresult}@@ -92,7 +92,7 @@ Outcome of an MCP discovery handshake.server exposes). `server` and `tool_infos` are display metadatacaptured from the same handshake; both follow MCP spec rev 2025-11-25.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/types.py#L56)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/types.py#L56)**Attributes**@@ -119,7 +119,7 @@ the server supplies separate variants — the spec lets clients pick thevariant matching the current UI theme and fall back to the first entryotherwise.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/types.py#L10)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/types.py#L10)**Attributes**@@ -144,7 +144,7 @@ Sourced from `InitializeResult.serverInfo` (an `Implementation`):the server's display name + version, an optional website URL, and anoptional list of icons that clients can render as the server's logo.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/types.py#L28)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/types.py#L28)**Attributes**@@ -169,7 +169,7 @@ Carries `Tool.icons` separately from the executable `AgentTool` soconsumers can render tool-specific icons without coupling cubepi'score `AgentTool` type to MCP display concerns.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/mcp/types.py#L43)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/mcp/types.py#L43)**Attributes**diff --git a/website/versioned_docs/version-0.6/api/cubepi-middleware.mdx b/website/versioned_docs/version-0.6/api/cubepi-middleware.mdxindex f442a1e..f1a6eb6 100644--- a/website/versioned_docs/version-0.6/api/cubepi-middleware.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-middleware.mdx@@ -12,7 +12,7 @@ hide_table_of_contents: false_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L24)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L24)**Methods**@@ -23,7 +23,7 @@ _class_transform_context(messages: list, *, signal = None) -> list```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L25)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L25)#### `convert_to_llm` {#middleware-convert-to-llm}@@ -32,7 +32,7 @@ transform_context(messages: list, *, signal = None) -> listconvert_to_llm(messages: list) -> list```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L28)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L28)#### `before_tool_call` {#middleware-before-tool-call}@@ -41,7 +41,7 @@ convert_to_llm(messages: list) -> listbefore_tool_call(ctx: Any, *, signal = None) -> Any```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L31)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L31)#### `after_tool_call` {#middleware-after-tool-call}@@ -50,7 +50,7 @@ before_tool_call(ctx: Any, *, signal = None) -> Anyafter_tool_call(ctx: Any, *, signal = None) -> Any```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L34)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L34)#### `transform_system_prompt` {#middleware-transform-system-prompt}@@ -59,7 +59,7 @@ after_tool_call(ctx: Any, *, signal = None) -> Anytransform_system_prompt(system_prompt: str, *, signal = None) -> str```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L37)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L37)#### `should_stop_after_turn` {#middleware-should-stop-after-turn}@@ -68,7 +68,7 @@ transform_system_prompt(system_prompt: str, *, signal = None) -> strshould_stop_after_turn(ctx: Any) -> bool```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L45)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L45)#### `after_model_response` {#middleware-after-model-response}@@ -77,7 +77,7 @@ should_stop_after_turn(ctx: Any) -> boolafter_model_response(response: AssistantMessage, ctx: Any, *, signal: Any = None) -> TurnAction | None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L48)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L48)@@ -95,7 +95,7 @@ Composition (chain): each middleware sees previous middleware'sTurnAction. Last middleware's value wins for response and decision.inject_messages concatenates across the chain.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L10)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L10)**Attributes**@@ -114,7 +114,7 @@ compose_middleware(middlewares: list[Middleware]) -> dict[str, Callable]```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/middleware/base.py#L64)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/middleware/base.py#L64)diff --git a/website/versioned_docs/version-0.6/api/cubepi-providers.mdx b/website/versioned_docs/version-0.6/api/cubepi-providers.mdxindex 0ab2f01..5f377f2 100644--- a/website/versioned_docs/version-0.6/api/cubepi-providers.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-providers.mdx@@ -12,7 +12,7 @@ hide_table_of_contents: false_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L124)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L124)**Attributes**@@ -55,7 +55,7 @@ Per-call mutators (`StreamOptions.on_payload`,semantics and fire independently of the persistent listener registrybelow.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L581)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L581)**Methods**@@ -66,7 +66,7 @@ below.stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L606)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L606)#### `subscribe_request` {#baseprovider-subscribe-request}@@ -79,7 +79,7 @@ Register a persistent observer for request payloads.Returns a detach callable that removes this specific subscription.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L625)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L625)#### `subscribe_chunk` {#baseprovider-subscribe-chunk}@@ -92,7 +92,7 @@ Register a persistent observer for stream chunks.Returns a detach callable.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L633)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L633)#### `subscribe_response` {#baseprovider-subscribe-response}@@ -105,7 +105,7 @@ Register a persistent observer for assembled responses.Returns a detach callable.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L641)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L641)@@ -118,7 +118,7 @@ Content = TextContent | ImageContent```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L107)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L107)### ImageContent {#imagecontent}@@ -126,7 +126,7 @@ Content = TextContent | ImageContent_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L96)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L96)**Attributes**@@ -145,7 +145,7 @@ Message = UserMessage | AssistantMessage | ToolResultMessage```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L148)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L148)### MessageStream {#messagestream}@@ -157,7 +157,7 @@ MessageStream(self)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L209)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L209)**Methods**@@ -168,7 +168,7 @@ MessageStream(self)attach_task(task: asyncio.Task) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L217)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L217)#### `push` {#messagestream-push}@@ -177,7 +177,7 @@ attach_task(task: asyncio.Task) -> Nonepush(event: StreamEvent) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L229)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L229)#### `set_result` {#messagestream-set-result}@@ -186,7 +186,7 @@ push(event: StreamEvent) -> Noneset_result(message: AssistantMessage) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L234)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L234)#### `result` {#messagestream-result}@@ -205,7 +205,7 @@ teardown could exit before async response listeners have run.Producer exceptions are NOT re-raised here; they were alreadysurfaced via the result future or the stream's error events.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L247)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L247)@@ -214,7 +214,7 @@ surfaced via the result future or the stream's error events._class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L79)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L79)**Attributes**@@ -235,7 +235,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L65)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L65)**Attributes**@@ -259,7 +259,7 @@ Persistent observer. Fires for every StreamEvent pushed onto the streamHeavy listeners should early-return on irrelevant event types — this hookfires hot. Return value is ignored.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L301)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L301)### OnPayloadCallback {#onpayloadcallback}@@ -273,7 +273,7 @@ OnPayloadCallback = Callable[[dict, Model], Awaitable[dict | None] | dict | NoneOptional callback for inspecting/replacing provider payloads before sending.Return a dict to replace the payload, or None to keep unchanged.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L288)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L288)### OnRequestCallback {#onrequestcallback}@@ -288,7 +288,7 @@ Persistent observer. Fires just before HTTP send, after any per-call`StreamOptions.on_payload` mutation has been applied. Receives the finalwire payload dict and the Model. Return value is ignored.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L296)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L296)### OnResponseBodyCallback {#onresponsebodycallback}@@ -309,7 +309,7 @@ finally block, after the stream terminates.`asyncio.CancelledError`), or None on normal completion.Return value is ignored.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L307)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L307)### OnResponseCallback {#onresponsecallback}@@ -322,7 +322,7 @@ OnResponseCallback = Callable[['ProviderResponse', Model], Awaitable[None] | NonOptional callback invoked after an HTTP response is received.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L292)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L292)### Provider {#provider}@@ -330,7 +330,7 @@ Optional callback invoked after an HTTP response is received._class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L568)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L568)**Methods**@@ -341,7 +341,7 @@ _class_stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L570)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L570)@@ -355,7 +355,7 @@ ProviderResponse(self, status: int, headers: dict[str, str] = dict())HTTP response metadata exposed to on_response callbacks.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L280)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L280)**Attributes**@@ -369,7 +369,7 @@ HTTP response metadata exposed to on_response callbacks._class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L157)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L157)**Attributes**@@ -387,7 +387,7 @@ _class_Options bag for Provider.stream(), transparent to the agent loop.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L529)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L529)**Attributes**@@ -405,7 +405,7 @@ Options bag for Provider.stream(), transparent to the agent loop._class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L91)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L91)**Attributes**@@ -420,7 +420,7 @@ _class_Token budgets for each thinking level.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L22)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L22)**Attributes**@@ -436,7 +436,7 @@ Token budgets for each thinking level._class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L102)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L102)**Attributes**@@ -454,7 +454,7 @@ ThinkingLevel = Literal['off', 'minimal', 'low', 'medium', 'high', 'xhigh']```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L19)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L19)### ToolCall {#toolcall}@@ -462,7 +462,7 @@ ThinkingLevel = Literal['off', 'minimal', 'low', 'medium', 'high', 'xhigh']_class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L110)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L110)**Attributes**@@ -478,7 +478,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L151)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L151)**Attributes**@@ -493,7 +493,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L137)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L137)**Attributes**@@ -513,7 +513,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L72)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L72)**Attributes**@@ -529,7 +529,7 @@ _class__class_-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L117)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L117)**Attributes**@@ -560,7 +560,7 @@ to leave at least `min_output_tokens` (1024) for output.- A `(max_tokens, thinking_budget)` tuple.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/base.py#L31)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/base.py#L31)### FauxProvider {#fauxprovider}@@ -572,7 +572,7 @@ FauxProvider(self, *, tokens_per_second: float | None = None, token_size_min: in```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L155)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L155)**Attributes**@@ -590,7 +590,7 @@ FauxProvider(self, *, tokens_per_second: float | None = None, token_size_min: inset_responses(responses: list[FauxResponseStep]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L213)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L213)#### `append_responses` {#fauxprovider-append-responses}@@ -599,7 +599,7 @@ set_responses(responses: list[FauxResponseStep]) -> Noneappend_responses(responses: list[FauxResponseStep]) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L216)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L216)#### `clear_prompt_cache` {#fauxprovider-clear-prompt-cache}@@ -610,7 +610,7 @@ clear_prompt_cache() -> NoneClear the prompt cache, useful between test scenarios.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L223)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L223)#### `stream` {#fauxprovider-stream}@@ -619,7 +619,7 @@ Clear the prompt cache, useful between test scenarios.stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L265)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L265)@@ -632,7 +632,7 @@ faux_assistant_message(content: str | FauxContentBlock | list[FauxContentBlock],```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L63)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L63)### faux_text {#faux-text}@@ -644,7 +644,7 @@ faux_text(text: str) -> TextContent```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L46)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L46)### faux_thinking {#faux-thinking}@@ -656,7 +656,7 @@ faux_thinking(thinking: str) -> ThinkingContent```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L50)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L50)### faux_tool_call {#faux-tool-call}@@ -668,7 +668,7 @@ faux_tool_call(name: str, arguments: dict[str, Any], *, id: str | None = None) -```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/faux.py#L54)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/faux.py#L54)### THINKING_LEVELS {#thinking-levels}@@ -680,7 +680,7 @@ THINKING_LEVELS: list[ThinkingLevel] = ['off', 'minimal', 'low', 'medium', 'high```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/models.py#L12)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/models.py#L12)### clamp_thinking_level {#clamp-thinking-level}@@ -697,7 +697,7 @@ If *level* is already supported, return it unchanged. Otherwise searchupward first (higher intensity), then downward, through the ordered levellist to find the closest available level.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/models.py#L54)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/models.py#L54)### get_supported_thinking_levels {#get-supported-thinking-levels}@@ -716,7 +716,7 @@ Return the thinking levels supported by *model*.`"xhigh"` is only included when it has an explicit (non-None) mapping.All other levels are included by default when the map omits them.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/models.py#L22)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/models.py#L22)### models_are_equal {#models-are-equal}@@ -732,7 +732,7 @@ Return `True` if *a* and *b* refer to the same model.Comparison is by `id` and `provider`. Returns `False` when eitherargument is `None`.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/providers/models.py#L87)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/providers/models.py#L87)diff --git a/website/versioned_docs/version-0.6/api/cubepi-tracing.mdx b/website/versioned_docs/version-0.6/api/cubepi-tracing.mdxindex fae2121..2c4afdf 100644--- a/website/versioned_docs/version-0.6/api/cubepi-tracing.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-tracing.mdx@@ -44,7 +44,7 @@ keys winning).tuple/list of those); other shapes will be silentlydropped by the recorder.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/context.py#L51)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/context.py#L51)### JsonlSpanExporter {#jsonlspanexporter}@@ -65,7 +65,7 @@ id; spans without a context fall back to `"unknown-trace"`.Permissions: files are created mode `0o600` (user-only).-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/exporters/jsonl.py#L23)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/exporters/jsonl.py#L23)**Methods**@@ -76,7 +76,7 @@ Permissions: files are created mode `0o600` (user-only).export(spans: Sequence[ReadableSpan]) -> SpanExportResult```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/exporters/jsonl.py#L42)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/exporters/jsonl.py#L42)#### `shutdown` {#jsonlspanexporter-shutdown}@@ -85,7 +85,7 @@ export(spans: Sequence[ReadableSpan]) -> SpanExportResultshutdown() -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/exporters/jsonl.py#L69)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/exporters/jsonl.py#L69)#### `force_flush` {#jsonlspanexporter-force-flush}@@ -94,7 +94,7 @@ shutdown() -> Noneforce_flush(timeout_millis: int = 30000) -> bool```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/exporters/jsonl.py#L72)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/exporters/jsonl.py#L72)@@ -128,7 +128,7 @@ tracer.attach(agent)meter.attach(agent)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/meter.py#L71)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/meter.py#L71)**Attributes**@@ -154,7 +154,7 @@ the Meter instance, so a second agent's `provider_request`overwrote the first agent's `_chat_open_ns` / `_chat_attrs`before its response landed).-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/meter.py#L144)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/meter.py#L144)#### `force_flush` {#meter-force-flush}@@ -163,7 +163,7 @@ before its response landed).force_flush(timeout_seconds: float = 30.0) -> bool```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/meter.py#L194)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/meter.py#L194)#### `shutdown` {#meter-shutdown}@@ -172,7 +172,7 @@ force_flush(timeout_seconds: float = 30.0) -> boolshutdown(timeout_seconds: float = 30.0) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/meter.py#L197)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/meter.py#L197)#### `attached` {#meter-attached}@@ -199,7 +199,7 @@ async with (# auto: detach both + shutdown both```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/meter.py#L204)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/meter.py#L204)@@ -212,7 +212,7 @@ SCHEMA_URL = 'https://opentelemetry.io/schemas/1.41.0'```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/schema.py#L13)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/schema.py#L13)### Tracer {#tracer}@@ -248,7 +248,7 @@ finally:await tracer.shutdown()```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L35)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L35)**Attributes**@@ -292,7 +292,7 @@ back to the OTel global default (a no-op provider when thecaller didn't separately call `trace.set_tracer_provider`).The detach callable unregisters.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L155)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L155)#### `force_flush` {#tracer-force-flush}@@ -305,7 +305,7 @@ Block until all currently buffered spans are exported.Returns `False` on timeout.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L235)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L235)#### `shutdown` {#tracer-shutdown}@@ -316,7 +316,7 @@ shutdown(timeout_seconds: float = 30.0) -> NoneFlush and close all exporters. Tracer is unusable after this.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L243)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L243)#### `attached` {#tracer-attached}@@ -352,7 +352,7 @@ async with Tracer(...) as tracer, tracer.attached(agent):# auto: detach (closes cancelled spans) + shutdown (flush + close)```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L284)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L284)@@ -388,7 +388,7 @@ async with trace(tracer, agent):await agent.prompt("...")```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/tracing/tracer.py#L398)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/tracing/tracer.py#L398)diff --git a/website/versioned_docs/version-0.6/api/cubepi-utils.mdx b/website/versioned_docs/version-0.6/api/cubepi-utils.mdxindex ab9a121..8e3c276 100644--- a/website/versioned_docs/version-0.6/api/cubepi-utils.mdx+++ b/website/versioned_docs/version-0.6/api/cubepi-utils.mdx@@ -16,7 +16,7 @@ emit_event(emit_fn: Callable, event: object) -> None```-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/utils/emit.py#L7)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/utils/emit.py#L7)### parse_streaming_json {#parse-streaming-json}@@ -38,7 +38,7 @@ Uses a 3-tier fallback:Always returns a *dict* (or at minimum `\{\}`) so callers never needto handle parse failures.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/utils/json_parse.py#L169)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/utils/json_parse.py#L169)### repair_json {#repair-json}@@ -60,7 +60,7 @@ JSON string value. Inside strings it:(e.g. `\x` becomes `\\x`), making the output parseable.* Handles truncated backslash at end-of-string.-[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/.worktrees/2026-05-31-release-0.6.0/cubepi/utils/json_parse.py#L39)+[source](https://github.com/cubeplexai/cubepi/blob/v0.6.0/cubepi/utils/json_parse.py#L39)diff --git a/website/versioned_docs/version-0.7/api/cubepi-agent.mdx b/website/versioned_docs/version-0.7/api/cubepi-agent.mdxnew file mode 100644index 0000000..21eb9f2--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-agent.mdx@@ -0,0 +1,590 @@+---+id: cubepi-agent+title: cubepi.agent+sidebar_position: 1+hide_table_of_contents: false+---++# `cubepi.agent`++### Agent {#agent}++_class_++```python+Agent(self, *, model: BoundModel, system_prompt: str = '', tools: list[AgentTool] | None = None, thinking: ThinkingLevel = 'off', convert_to_llm: Callable[..., list[Message]] | None = None, transform_context: Callable | None = None, transform_system_prompt: Callable | None = None, after_model_response: Callable | None = None, before_tool_call: Callable | None = None, after_tool_call: Callable | None = None, should_stop_after_turn: Callable | None = None, on_run_end: Callable | None = None, on_payload: OnPayloadCallback | None = None, on_response: OnResponseCallback | None = None, steering_mode: str = 'one-at-a-time', follow_up_mode: str = 'one-at-a-time', tool_execution: str = 'parallel', checkpointer: Checkpointer | None = None, thread_id: str | None = None, middleware: list[Middleware] | None = None, channel: HitlChannel | None = None)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L130)+++**Attributes**++- `convert_to_llm`+- `transform_context`+- `transform_system_prompt`+- `after_model_response`+- `before_tool_call`+- `after_tool_call`+- `should_stop_after_turn`+- `on_run_end`+- `on_payload`+- `on_response`+- `tool_execution`+- `checkpointer`+- `thread_id`+- `state`: `AgentState`+- `channel`: `HitlChannel | None`+- `in_flight_hitl_request`: `HitlRequest | None`+++**Methods**++#### `subscribe` {#agent-subscribe}++```python+subscribe(listener: Callable) -> Callable[[], None]+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L223)+++#### `steer` {#agent-steer}++```python+steer(message: Message) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L229)+++#### `cancel_steer` {#agent-cancel-steer}++```python+cancel_steer(steer_id: str) -> bool+```++Remove a not-yet-drained steering message by its steer_id.++Returns True if a queued message was removed; False if it was already+drained or never queued (best-effort cancel).++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L232)+++#### `follow_up` {#agent-follow-up}++```python+follow_up(message: Message) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L240)+++#### `abort` {#agent-abort}++```python+abort() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L243)+++#### `wait_for_idle` {#agent-wait-for-idle}++```python+wait_for_idle() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L247)+++#### `reset` {#agent-reset}++```python+reset() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L251)+++#### `prompt` {#agent-prompt}++```python+prompt(message: str | Message | list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L260)+++#### `resume` {#agent-resume}++```python+resume() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L300)+++#### `detach` {#agent-detach}++```python+detach() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L400)+++#### `load_pending_hitl_request` {#agent-load-pending-hitl-request}++```python+load_pending_hitl_request() -> HitlRequest | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L418)+++#### `respond` {#agent-respond}++```python+respond(*, question_id: str | None = None, answer: StructuredValue) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L426)+++#### `abort_pending` {#agent-abort-pending}++```python+abort_pending(reason: str = 'aborted by host') -> None+```++Abort a pending HITL request and CLOSE the conversation.++Per spec §5.2 "abort closes the conversation" — no new model call.+Two-phase: Phase 1 (no lock) interrupts any in-flight HITL await via+the agent signal; Phase 2 (with lock) appends synthetic deny+tool_results + a terminal stop_reason="aborted" assistant message+and emits AgentAbortedEvent.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L466)++++### AgentState {#agentstate}++_class_++```python+AgentState(self, system_prompt: str = '', model: Model = (lambda: Model(id='unknown', provider_id='unknown'))(), thinking: ThinkingLevel = 'off', is_streaming: bool = False, streaming_message: Message | None = None, error_message: str | None = None, _tools: list[AgentTool] = list(), _messages: list[Message] = list(), _pending_tool_calls: set[str] = set())+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/agent.py#L91)+++**Attributes**++- `system_prompt`: `str`+- `model`: `Model`+- `thinking`: `ThinkingLevel`+- `is_streaming`: `bool`+- `streaming_message`: `Message | None`+- `error_message`: `str | None`+- `tools`: `list[AgentTool]`+- `messages`: `list[Message]`+- `pending_tool_calls`: `set[str]`+++### run_agent_loop {#run-agent-loop}++_function_++```python+run_agent_loop(*, prompts: list[Message], context: AgentContext, provider: Provider, model: Model, convert_to_llm: Callable, emit: Callable, transform_context: Callable | None = None, transform_system_prompt: Callable | None = None, after_model_response: Callable | None = None, before_tool_call: Callable | None = None, after_tool_call: Callable | None = None, should_stop_after_turn: Callable | None = None, get_steering_messages: Callable | None = None, get_follow_up_messages: Callable | None = None, on_run_end: Callable | None = None, stream_options: StreamOptions | None = None, tool_execution: str = 'parallel', system_prompt: str | None = None) -> list[Message]+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/loop.py#L32)+++### run_agent_loop_continue {#run-agent-loop-continue}++_function_++```python+run_agent_loop_continue(*, context: AgentContext, provider: Provider, model: Model, convert_to_llm: Callable, emit: Callable, transform_context: Callable | None = None, transform_system_prompt: Callable | None = None, after_model_response: Callable | None = None, before_tool_call: Callable | None = None, after_tool_call: Callable | None = None, should_stop_after_turn: Callable | None = None, get_steering_messages: Callable | None = None, get_follow_up_messages: Callable | None = None, on_run_end: Callable | None = None, stream_options: StreamOptions | None = None, tool_execution: str = 'parallel', system_prompt: str | None = None) -> list[Message]+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/loop.py#L91)+++### execute_tool_calls {#execute-tool-calls}++_function_++```python+execute_tool_calls(context: AgentContext, assistant_message: AssistantMessage, *, tool_execution: str = 'parallel', before_tool_call: Callable | None = None, after_tool_call: Callable | None = None, signal: asyncio.Event | None = None, emit: Callable) -> ToolCallBatch+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/tools.py#L314)+++### AfterToolCallContext {#aftertoolcallcontext}++_class_++```python+AfterToolCallContext(self, assistant_message: AssistantMessage, tool_call: ToolCall, args: BaseModel | JsonObject, result: AgentToolResult, is_error: bool, context: AgentContext)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L85)+++**Attributes**++- `assistant_message`: `AssistantMessage`+- `tool_call`: `ToolCall`+- `args`: `BaseModel | JsonObject`+- `result`: `AgentToolResult`+- `is_error`: `bool`+- `context`: `AgentContext`+++### AfterToolCallResult {#aftertoolcallresult}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L70)+++**Attributes**++- `content`: `list[Content] | None`+- `details`: `StructuredValue`+- `is_error`: `bool | None`+- `terminate`: `bool | None`+++### AgentContext {#agentcontext}++_class_++```python+AgentContext(self, system_prompt: str, messages: list[Message], tools: list[AgentTool] | None = None, extra: JsonObject = dict())+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L51)+++**Attributes**++- `system_prompt`: `str`+- `messages`: `list[Message]`+- `tools`: `list[AgentTool] | None`+- `extra`: `JsonObject`+++### AgentEndEvent {#agentendevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L110)+++**Attributes**++- `type`: `Literal['agent_end']`+- `messages`: `list[Message]`+++### AgentEvent {#agentevent}++_attribute_++```python+AgentEvent = AgentStartEvent | AgentEndEvent | TurnStartEvent | TurnEndEvent | MessageStartEvent | MessageUpdateEvent | MessageEndEvent | ToolExecutionStartEvent | ToolExecutionUpdateEvent | ToolExecutionEndEve...+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L199)+++### AgentListener {#agentlistener}++_attribute_++```python+AgentListener = Callable[[AgentEvent, asyncio.Event | None], Awaitable[None] | None]+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L218)+++### AgentStartEvent {#agentstartevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L106)+++**Attributes**++- `type`: `Literal['agent_start']`+++### AgentTool {#agenttool}++_class_++```python+AgentTool(self, name: str, description: str, parameters: type[TParams], execute: Callable[..., Awaitable[AgentToolResult]], label: str = '', execution_mode: Literal['sequential', 'parallel'] | None = None, hitl_builtin: bool = False)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L32)+++**Attributes**++- `name`: `str`+- `description`: `str`+- `parameters`: `type[TParams]`+- `execute`: `Callable[..., Awaitable[AgentToolResult]]`+- `label`: `str`+- `execution_mode`: `Literal['sequential', 'parallel'] | None`+- `hitl_builtin`: `bool`+++**Methods**++#### `to_definition` {#agenttool-to-definition}++```python+to_definition() -> ToolDefinition+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L42)++++### AgentToolResult {#agenttoolresult}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L25)+++**Attributes**++- `content`: `list[Content]`+- `details`: `StructuredValue`+- `is_error`: `bool | None`+- `terminate`: `bool | None`+++### BeforeToolCallContext {#beforetoolcallcontext}++_class_++```python+BeforeToolCallContext(self, assistant_message: AssistantMessage, tool_call: ToolCall, args: BaseModel | JsonObject, context: AgentContext)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L77)+++**Attributes**++- `assistant_message`: `AssistantMessage`+- `tool_call`: `ToolCall`+- `args`: `BaseModel | JsonObject`+- `context`: `AgentContext`+++### BeforeToolCallResult {#beforetoolcallresult}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L62)+++**Attributes**++- `block`: `bool`+- `reason`: `str | None`+- `edited_args`: `JsonObject | None`+- `deny_reason`: `str | None`+- `hitl_trace`: `StructuredObject | None`+++### MessageEndEvent {#messageendevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L136)+++**Attributes**++- `type`: `Literal['message_end']`+- `message`: `Message`+++### MessageStartEvent {#messagestartevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L125)+++**Attributes**++- `type`: `Literal['message_start']`+- `message`: `Message`+++### MessageUpdateEvent {#messageupdateevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L130)+++**Attributes**++- `type`: `Literal['message_update']`+- `message`: `AssistantMessage`+- `stream_event`: `StreamEvent`+++### ShouldStopAfterTurnContext {#shouldstopafterturncontext}++_class_++```python+ShouldStopAfterTurnContext(self, message: AssistantMessage, tool_results: list[ToolResultMessage], context: AgentContext, new_messages: list[Message])+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L95)+++**Attributes**++- `message`: `AssistantMessage`+- `tool_results`: `list[ToolResultMessage]`+- `context`: `AgentContext`+- `new_messages`: `list[Message]`+++### ToolExecutionEndEvent {#toolexecutionendevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L156)+++**Attributes**++- `type`: `Literal['tool_execution_end']`+- `tool_call_id`: `str`+- `tool_name`: `str`+- `result`: `StructuredValue`+- `is_error`: `bool`+- `terminate`: `bool` — True iff the tool's AgentToolResult.terminate was True (or the after_tool_call hook set terminate=True). Recorders use this to mark the turn as terminated-by-tool without unwrapping `result`.+- `blocked_by_hook`: `bool` — True iff the tool call was blocked by a `before_tool_call` hook returning `block=True`. Distinguishes hook-blocks from other immediate errors (tool-not-found, arg-validation failure).+- `block_reason`: `str | None` — When `blocked_by_hook` is True, the reason string from `BeforeToolCallResult.reason` (or `None` if the hook supplied no reason).+++### ToolExecutionStartEvent {#toolexecutionstartevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L141)+++**Attributes**++- `type`: `Literal['tool_execution_start']`+- `tool_call_id`: `str`+- `tool_name`: `str`+- `args`: `JsonObject`+++### ToolExecutionUpdateEvent {#toolexecutionupdateevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L148)+++**Attributes**++- `type`: `Literal['tool_execution_update']`+- `tool_call_id`: `str`+- `tool_name`: `str`+- `args`: `JsonObject | None`+- `partial_result`: `StructuredValue`+++### TurnEndEvent {#turnendevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L119)+++**Attributes**++- `type`: `Literal['turn_end']`+- `message`: `AssistantMessage`+- `tool_results`: `list[ToolResultMessage]`+++### TurnStartEvent {#turnstartevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/agent/types.py#L115)+++**Attributes**++- `type`: `Literal['turn_start']`+++### emit_event {#emit-event}++_function_++```python+emit_event(emit_fn: Callable, event: object) -> None+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/utils/emit.py#L7)++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-checkpointer.mdx b/website/versioned_docs/version-0.7/api/cubepi-checkpointer.mdxnew file mode 100644index 0000000..5d01c89--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-checkpointer.mdx@@ -0,0 +1,433 @@+---+id: cubepi-checkpointer+title: cubepi.checkpointer+sidebar_position: 3+hide_table_of_contents: false+---++# `cubepi.checkpointer`++### Checkpointer {#checkpointer}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L17)+++**Methods**++#### `load` {#checkpointer-load}++```python+load(thread_id: str) -> CheckpointData | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L19)+++#### `append` {#checkpointer-append}++```python+append(thread_id: str, messages: list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L20)+++#### `save_extra` {#checkpointer-save-extra}++```python+save_extra(thread_id: str, extra: JsonObject) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L21)+++#### `save_pending_request` {#checkpointer-save-pending-request}++```python+save_pending_request(thread_id: str, request: HitlRequest | None, *, run_id: str | None = None) -> None+```++Persist (or clear, if request is None) the pending HITL request for a thread.++First-party implementations (Memory, SQLite, Postgres, MySQL) all implement this.+HITL-requiring features (Agent.respond, CheckpointedChannel) use+`getattr(checkpointer, "save_pending_request", None)` for graceful degradation.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L23)+++#### `load_pending_request` {#checkpointer-load-pending-request}++```python+load_pending_request(thread_id: str) -> HitlRequest | None+```++Load the persisted pending HITL request for a thread, or None.++Returns a `HitlRequest` instance or `None`.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L38)++++### CheckpointData {#checkpointdata}++_class_++```python+CheckpointData(self, messages: list[Message] = list(), extra: JsonObject = dict())+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/base.py#L11)+++**Attributes**++- `messages`: `list[Message]`+- `extra`: `JsonObject`+++### MemoryCheckpointer {#memorycheckpointer}++_class_++```python+MemoryCheckpointer(self)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L9)+++**Methods**++#### `load` {#memorycheckpointer-load}++```python+load(thread_id: str) -> CheckpointData | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L17)+++#### `append` {#memorycheckpointer-append}++```python+append(thread_id: str, messages: list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L20)+++#### `save_extra` {#memorycheckpointer-save-extra}++```python+save_extra(thread_id: str, extra: JsonObject) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L25)+++#### `save_pending_request` {#memorycheckpointer-save-pending-request}++```python+save_pending_request(thread_id: str, request: HitlRequest | None, *, run_id: str | None = None) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L30)+++#### `load_pending_request` {#memorycheckpointer-load-pending-request}++```python+load_pending_request(thread_id: str) -> HitlRequest | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L44)+++#### `load_pending_run_id` {#memorycheckpointer-load-pending-run-id}++```python+load_pending_run_id(thread_id: str) -> str | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/memory.py#L47)++++### PostgresCheckpointer {#postgrescheckpointer}++_class_++```python+PostgresCheckpointer(self, dsn: str, *, min_pool_size: int = 1, max_pool_size: int = 10)+```++Checkpointer backed by PostgreSQL.+++**Usage**++```python+cp = PostgresCheckpointer(dsn="postgresql://...")+async with cp:+ await cp.append(thread_id, [msg1, msg2])+ data = await cp.load(thread_id)+ await cp.save_extra(thread_id, {"k": "v"})+```++Raises CubepiSchemaUninitialized / CubepiSchemaMismatch at __aenter__+if the DB schema isn't compatible with this cubepi version.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L48)+++**Methods**++#### `load` {#postgrescheckpointer-load}++```python+load(thread_id: str) -> CheckpointData | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L117)+++#### `append` {#postgrescheckpointer-append}++```python+append(thread_id: str, messages: list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L159)+++#### `save_extra` {#postgrescheckpointer-save-extra}++```python+save_extra(thread_id: str, extra: JsonObject) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L207)+++#### `save_pending_request` {#postgrescheckpointer-save-pending-request}++```python+save_pending_request(thread_id: str, request: HitlRequest | None, *, run_id: str | None = None) -> None+```++Persist a pending HITL request and its owning run_id.++When `request is None`, both `pending_request` and `run_id`+are cleared; the `run_id` kwarg is ignored in that case (the+pending row's run_id is always cleared alongside the pending).++pending and run_id are set in ONE UPDATE; the surrounding+transaction also covers the lazy thread INSERT.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L220)+++#### `load_pending_request` {#postgrescheckpointer-load-pending-request}++```python+load_pending_request(thread_id: str) -> HitlRequest | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L263)+++#### `load_pending_run_id` {#postgrescheckpointer-load-pending-run-id}++```python+load_pending_run_id(thread_id: str) -> str | None+```++Return the run_id of the currently pending HITL request.++Filters on `pending_request IS NOT NULL` so the result reflects+a real pending, not a leftover run_id from a cleared row. Returns+None when: the thread is unknown, has no pending request, or was+written by a pre-v3 host (legacy rows have run_id NULL).++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py#L278)++++### SQLiteCheckpointer {#sqlitecheckpointer}++_class_++```python+SQLiteCheckpointer(self, db_path: str)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L20)+++**Methods**++#### `load` {#sqlitecheckpointer-load}++```python+load(thread_id: str) -> CheckpointData | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L67)+++#### `append` {#sqlitecheckpointer-append}++```python+append(thread_id: str, messages: list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L93)+++#### `save_extra` {#sqlitecheckpointer-save-extra}++```python+save_extra(thread_id: str, extra: JsonObject) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L104)+++#### `save_pending_request` {#sqlitecheckpointer-save-pending-request}++```python+save_pending_request(thread_id: str, request: HitlRequest | None, *, run_id: str | None = None) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L126)+++#### `load_pending_request` {#sqlitecheckpointer-load-pending-request}++```python+load_pending_request(thread_id: str) -> HitlRequest | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L151)+++#### `load_pending_run_id` {#sqlitecheckpointer-load-pending-run-id}++```python+load_pending_run_id(thread_id: str) -> str | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py#L161)++++### MySQLCheckpointer {#mysqlcheckpointer}++_class_++```python+MySQLCheckpointer(self, dsn: str, *, min_pool_size: int = 1, max_pool_size: int = 10)+```++Checkpointer backed by MySQL (8.0.13+, InnoDB).+++**Usage**++```python+cp = MySQLCheckpointer("mysql://user:pw@host:3306/db")+async with cp:+ await cp.append(thread_id, [msg1, msg2])+ data = await cp.load(thread_id)+ await cp.save_extra(thread_id, {"k": "v"})+```++Raises CubepiSchemaUninitialized / CubepiSchemaMismatch at __aenter__ if the+DB schema isn't compatible with this cubepi version.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L77)+++**Methods**++#### `load` {#mysqlcheckpointer-load}++```python+load(thread_id: str) -> CheckpointData | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L166)+++#### `append` {#mysqlcheckpointer-append}++```python+append(thread_id: str, messages: list[Message]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L197)+++#### `save_extra` {#mysqlcheckpointer-save-extra}++```python+save_extra(thread_id: str, extra: JsonObject) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L251)+++#### `save_pending_request` {#mysqlcheckpointer-save-pending-request}++```python+save_pending_request(thread_id: str, request: HitlRequest | None, *, run_id: str | None = None) -> None+```++Persist a pending HITL request and its owning run_id.++When `request is None`, both `pending_request` and `run_id`+are cleared; the `run_id` kwarg is ignored in that case (the+pending row's run_id is always cleared alongside the pending).++pending and run_id are set in ONE UPDATE; the surrounding+explicit transaction also covers the lazy thread INSERT.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L285)+++#### `load_pending_request` {#mysqlcheckpointer-load-pending-request}++```python+load_pending_request(thread_id: str) -> HitlRequest | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L331)+++#### `load_pending_run_id` {#mysqlcheckpointer-load-pending-run-id}++```python+load_pending_run_id(thread_id: str) -> str | None+```++Return the run_id of the currently pending HITL request.++Filters on `pending_request IS NOT NULL` so the result reflects+a real pending, not a leftover run_id from a cleared row. Returns+None when: the thread is unknown, has no pending request, or was+written by a pre-v3 host (legacy rows have run_id NULL).++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/checkpointer.py#L349)+++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-mcp.mdx b/website/versioned_docs/version-0.7/api/cubepi-mcp.mdxnew file mode 100644index 0000000..e1db297--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-mcp.mdx@@ -0,0 +1,182 @@+---+id: cubepi-mcp+title: cubepi.mcp+sidebar_position: 5+hide_table_of_contents: false+---++# `cubepi.mcp`++### load_mcp_tools_http {#load-mcp-tools-http}++_function_++```python+load_mcp_tools_http(server_url: str, *, headers: dict[str, str] | None = None, timeout: float = 30.0, transport: Transport = 'sse') -> MCPDiscoveryResult+```++Connect to an HTTP MCP server and discover its tools + metadata.++Returns a [`MCPDiscoveryResult`](./cubepi-mcp#mcpdiscoveryresult) carrying:++- `tools` — executable `AgentTool` per `tools/list` entry.+- `server` — `MCPServerInfo` (name, version, websiteUrl, icons)+ captured from the `initialize` handshake's `serverInfo`. Sourced+ from MCP spec rev 2025-11-25's `Implementation` shape.+- `tool_infos` — per-tool display metadata (currently `icons`)+ captured from each `tools/list` entry, separated from+ `AgentTool` so callers can render visuals without coupling+ core types to display concerns.++`transport` picks the wire format:++- `"sse"` (default) — legacy SSE-over-GET transport (`sse_client`).+- `"streamable_http"` — newer SSE-over-POST transport+ (`streamablehttp_client`).++Each returned tool's execute method invokes `tools/call` against a+fresh session — v1 simplicity, no pooling. The session is opened over+the same transport that was used for discovery, so the call path+cannot accidentally regress to the wrong wire format.++The transport's own timeout bounds the connection; we additionally+wrap initialize/list/call awaits in `asyncio.wait_for` so a server+that accepts the connection but stalls on protocol messages still+aborts.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/http_loader.py#L112)+++### load_mcp_tools_stdio {#load-mcp-tools-stdio}++_function_++```python+load_mcp_tools_stdio(command: str, args: list[str], *, env: dict[str, str] | None = None, cwd: str | None = None, timeout: float = 30.0) -> MCPDiscoveryResult+```++Spawn a stdio MCP server subprocess and discover its tools + metadata.++Returns a [`MCPDiscoveryResult`](./cubepi-mcp#mcpdiscoveryresult) with the same shape as+[`load_mcp_tools_http`](./cubepi-mcp#load-mcp-tools-http): `tools` (executable AgentTools),+`server` (Implementation info from `initialize`), and `tool_infos`+(per-tool display metadata such as icons).++Each returned tool's execute opens a fresh subprocess per call (v1+simplicity, no process pooling).+++**Args**++- `command` — executable to run (e.g. "npx" or sys.executable)+- `args` — argv for the server process+- `env` — environment variables (passed to subprocess)+- `cwd` — working directory for the subprocess+- `timeout` — per-call wall-clock timeout for initialize/list/call awaits.+ A hung server raises asyncio.TimeoutError instead of blocking forever.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/stdio_loader.py#L16)+++### MCPDiscoveryResult {#mcpdiscoveryresult}++_class_++```python+MCPDiscoveryResult(self, tools: list[AgentTool], server: MCPServerInfo | None = None, tool_infos: list[MCPToolInfo] = list())+```++Outcome of an MCP discovery handshake.++`tools` is the executable surface (one `AgentTool` per tool the+server exposes). `server` and `tool_infos` are display metadata+captured from the same handshake; both follow MCP spec rev 2025-11-25.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/types.py#L56)+++**Attributes**++- `tools`: `list[AgentTool]`+- `server`: `MCPServerInfo | None`+- `tool_infos`: `list[MCPToolInfo]`+++### MCPIcon {#mcpicon}++_class_++```python+MCPIcon(self, src: str, mime_type: str | None = None, sizes: tuple[str, ...] | None = None, theme: str | None = None)+```++One icon entry from an MCP `Icon` block.++Mirrors `mcp.types.Icon` with snake_case fields. `src` is either an+HTTP/HTTPS URL or a `data:` URI; `sizes` is a tuple of strings such+as `("48x48", "96x96")`. `theme` is `"light"` or `"dark"` when+the server supplies separate variants — the spec lets clients pick the+variant matching the current UI theme and fall back to the first entry+otherwise.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/types.py#L10)+++**Attributes**++- `src`: `str`+- `mime_type`: `str | None`+- `sizes`: `tuple[str, ...] | None`+- `theme`: `str | None`+++### MCPServerInfo {#mcpserverinfo}++_class_++```python+MCPServerInfo(self, name: str, version: str, website_url: str | None = None, icons: tuple[MCPIcon, ...] = ())+```++Server-level metadata captured from the `initialize` handshake.++Sourced from `InitializeResult.serverInfo` (an `Implementation`):+the server's display name + version, an optional website URL, and an+optional list of icons that clients can render as the server's logo.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/types.py#L28)+++**Attributes**++- `name`: `str`+- `version`: `str`+- `website_url`: `str | None`+- `icons`: `tuple[MCPIcon, ...]`+++### MCPToolInfo {#mcptoolinfo}++_class_++```python+MCPToolInfo(self, name: str, icons: tuple[MCPIcon, ...] = ())+```++Per-tool display metadata captured from `tools/list`.++Carries `Tool.icons` separately from the executable `AgentTool` so+consumers can render tool-specific icons without coupling cubepi's+core `AgentTool` type to MCP display concerns.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/types.py#L43)+++**Attributes**++- `name`: `str`+- `icons`: `tuple[MCPIcon, ...]`++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-middleware.mdx b/website/versioned_docs/version-0.7/api/cubepi-middleware.mdxnew file mode 100644index 0000000..e7f41c3--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-middleware.mdx@@ -0,0 +1,251 @@+---+id: cubepi-middleware+title: cubepi.middleware+sidebar_position: 4+hide_table_of_contents: false+---++# `cubepi.middleware`++### Middleware {#middleware}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L33)+++**Methods**++#### `transform_context` {#middleware-transform-context}++```python+transform_context(messages: list[Message], *, ctx: AgentContext, signal: asyncio.Event | None = None) -> list[Message]+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L34)+++#### `convert_to_llm` {#middleware-convert-to-llm}++```python+convert_to_llm(messages: list[Message], *, ctx: AgentContext) -> list[Message]+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L43)+++#### `before_tool_call` {#middleware-before-tool-call}++```python+before_tool_call(ctx: BeforeToolCallContext, *, signal: asyncio.Event | None = None) -> BeforeToolCallResult | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L48)+++#### `after_tool_call` {#middleware-after-tool-call}++```python+after_tool_call(ctx: AfterToolCallContext, *, signal: asyncio.Event | None = None) -> AfterToolCallResult | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L56)+++#### `transform_system_prompt` {#middleware-transform-system-prompt}++```python+transform_system_prompt(system_prompt: str, *, ctx: AgentContext, signal: asyncio.Event | None = None) -> str+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L64)+++#### `should_stop_after_turn` {#middleware-should-stop-after-turn}++```python+should_stop_after_turn(ctx: AgentContext) -> bool+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L73)+++#### `after_model_response` {#middleware-after-model-response}++```python+after_model_response(response: AssistantMessage, ctx: AgentContext, *, signal: asyncio.Event | None = None) -> TurnAction | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L76)+++#### `on_run_end` {#middleware-on-run-end}++```python+on_run_end(ctx: AgentContext, *, signal: asyncio.Event | None = None) -> list[Message] | None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L85)++++### TurnAction {#turnaction}++_class_++```python+TurnAction(self, response: AssistantMessage | None = None, inject_messages: list[Message] = list(), decision: Literal['natural', 'stop', 'loop_to_model'] = 'natural')+```++Directs the agent loop's next step after a model response.++Composition (chain): each middleware sees previous middleware's+TurnAction. Last middleware's value wins for response and decision.+inject_messages concatenates across the chain.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L19)+++**Attributes**++- `response`: `AssistantMessage | None`+- `inject_messages`: `list[Message]`+- `decision`: `Literal['natural', 'stop', 'loop_to_model']`+++### compose_middleware {#compose-middleware}++_function_++```python+compose_middleware(middlewares: list[Middleware]) -> dict[str, Callable]+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/base.py#L100)+++### CompactionMiddleware {#compactionmiddleware}++_class_++```python+CompactionMiddleware(self, *, summary_model: BoundModel, max_tokens_before_compact: int, keep_recent_messages: int = 8, max_summary_tokens: int = 1024, min_compact_messages: int = 4)+```++Keep long histories within context by summarizing older turns.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/compaction/__init__.py#L65)+++**Methods**++#### `transform_context` {#compactionmiddleware-transform-context}++```python+transform_context(messages: list[Message], *, ctx: AgentContext, signal: asyncio.Event | None = None) -> list[Message]+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/compaction/__init__.py#L84)++++### CompactionState {#compactionstate}++_class_++JSON-safe summary state stored in `AgentContext.extra`.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/compaction/state.py#L11)+++**Attributes**++- `summary`: `str`+- `summarized_message_ids`: `list[str]`+- `summarized_message_refs`: `list[str]`+- `last_summarized_message_id`: `str | None`+++### SubagentMiddleware {#subagentmiddleware}++_class_++```python+SubagentMiddleware(self, *, subagents: dict[str, SubagentSpec], default_model: BoundModel, shared_tools: Sequence[AgentTool[BaseModel]] = (), inherited_middleware: Sequence[Middleware] = (), excluded_tool_names: set[str] | None = None, event_mapper: EventMapper | None = None, event_handler: EventHandler | None = None, tracer: SubagentTracer | None = None)+```++Inject a tool that delegates one task to an ephemeral child agent.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/subagents.py#L57)+++**Attributes**++- `tools`: `list[AgentTool[BaseModel]]`+- `subagents`: `dict[str, SubagentSpec]`+- `shared_tools`: `tuple[AgentTool[BaseModel], ...]`+++### SubagentRequest {#subagentrequest}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/subagents.py#L41)+++**Attributes**++- `name`: `str`+- `role`: `str`+- `task`: `str`+- `prompt`: `str`+- `subagent_type`: `str`+++### SubagentResult {#subagentresult}++_class_++```python+SubagentResult(self, agent_id: str, text: str, events: list[StructuredValue], error: str | None = None)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/subagents.py#L49)+++**Attributes**++- `agent_id`: `str`+- `text`: `str`+- `events`: `list[StructuredValue]`+- `error`: `str | None`+++### SubagentSpec {#subagentspec}++_class_++```python+SubagentSpec(self, name: str, description: str, system_prompt: str, model: BoundModel | None = None, tools: Sequence[AgentTool[BaseModel]] = tuple(), middleware: Sequence[Middleware] = tuple())+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/middleware/subagents.py#L31)+++**Attributes**++- `name`: `str`+- `description`: `str`+- `system_prompt`: `str`+- `model`: `BoundModel | None`+- `tools`: `Sequence[AgentTool[BaseModel]]`+- `middleware`: `Sequence[Middleware]`++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-providers.mdx b/website/versioned_docs/version-0.7/api/cubepi-providers.mdxnew file mode 100644index 0000000..06f3489--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-providers.mdx@@ -0,0 +1,791 @@+---+id: cubepi-providers+title: cubepi.providers+sidebar_position: 2+hide_table_of_contents: false+---++# `cubepi.providers`++### AssistantMessage {#assistantmessage}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L132)+++**Attributes**++- `role`: `Literal['assistant']`+- `content`: `list[Content | ThinkingContent | ToolCall]`+- `stop_reason`: `str`+- `error_message`: `str | None`+- `usage`: `Usage | None`+- `timestamp`: `float | None`+- `provider_id`: `str`+- `model_id`: `str`+- `response_id`: `str | None`+- `metadata`: `JsonObject`+++### BaseProvider {#baseprovider}++_class_++```python+BaseProvider(self, *, provider_id: str = '')+```++Concrete base class for built-in cubepi providers.++Built-in providers (Anthropic, OpenAI, OpenAI Responses, Faux) inherit+from this class to gain the persistent listener registry used by+`cubepi.tracing` and other observers. User-defined providers should+inherit from `BaseProvider` unless they implement the full `Provider`+protocol themselves.++Concrete subclasses must implement `stream()` and call+`_fire_listeners` at three points: after the request payload is+finalized, for each `StreamEvent` pushed onto the stream, and exactly+once in a `finally` block after the stream terminates.++Per-call mutators (`StreamOptions.on_payload`,+`StreamOptions.on_response`) retain their existing single-slot+semantics and fire independently of the persistent listener registry+below.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L603)+++**Attributes**++- `provider_id`+++**Methods**++#### `model` {#baseprovider-model}++```python+model(id: str, *, api: str = '', reasoning: bool = False, context_window: int = 200000, max_tokens: int = 8192, temperature: float = 0.7, cost: ModelCost | None = None, thinking_level_map: dict[str, str | None] | None = None) -> BoundModel+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L629)+++#### `stream` {#baseprovider-stream}++```python+stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L656)+++#### `generate` {#baseprovider-generate}++```python+generate(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None, max_output_tokens: int | None = None, temperature: float | None = None, thinking: ThinkingLevel | None = None, thinking_budgets: ThinkingBudgets | None = None) -> AssistantMessage+```++Run a single provider call and return the final assistant message.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L667)+++#### `subscribe_request` {#baseprovider-subscribe-request}++```python+subscribe_request(cb: OnRequestCallback) -> Callable[[], None]+```++Register a persistent observer for request payloads.++Returns a detach callable that removes this specific subscription.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L718)+++#### `subscribe_chunk` {#baseprovider-subscribe-chunk}++```python+subscribe_chunk(cb: OnChunkCallback) -> Callable[[], None]+```++Register a persistent observer for stream chunks.++Returns a detach callable.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L726)+++#### `subscribe_response` {#baseprovider-subscribe-response}++```python+subscribe_response(cb: OnResponseBodyCallback) -> Callable[[], None]+```++Register a persistent observer for assembled responses.++Returns a detach callable.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L734)++++### BoundModel {#boundmodel}++_class_++```python+BoundModel(self, provider: Provider, spec: Model)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L93)+++**Attributes**++- `provider`: `Provider`+- `spec`: `Model`+++### Content {#content}++_attribute_++```python+Content = TextContent | ImageContent+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L115)+++### ImageContent {#imagecontent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L104)+++**Attributes**++- `type`: `Literal['image']`+- `source`: `str`+- `media_type`: `str`+++### Message {#message}++_attribute_++```python+Message = UserMessage | AssistantMessage | ToolResultMessage+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L156)+++### MessageStream {#messagestream}++_class_++```python+MessageStream(self)+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L217)+++**Methods**++#### `attach_task` {#messagestream-attach-task}++```python+attach_task(task: asyncio.Task) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L225)+++#### `push` {#messagestream-push}++```python+push(event: StreamEvent) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L237)+++#### `set_result` {#messagestream-set-result}++```python+set_result(message: AssistantMessage) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L242)+++#### `result` {#messagestream-result}++```python+result() -> AssistantMessage+```++Return the final assistant message.++Blocks not just on the result future, but also on the producer+task's completion — the producer's `finally` block runs+`_fire_response_listeners` (after `set_result`), so+without waiting for the task, callers under `asyncio.run`+teardown could exit before async response listeners have run.+Producer exceptions are NOT re-raised here; they were already+surfaced via the result future or the stream's error events.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L255)++++### Model {#model}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L81)+++**Attributes**++- `id`: `str`+- `provider_id`: `str`+- `api`: `str`+- `reasoning`: `bool`+- `context_window`: `int`+- `max_tokens`: `int`+- `temperature`: `float`+- `cost`: `ModelCost | None`+- `thinking_level_map`: `dict[str, str | None] | None`+++### ModelCost {#modelcost}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L67)+++**Attributes**++- `input`: `float`+- `output`: `float`+- `cache_read`: `float`+- `cache_write`: `float`+++### OnChunkCallback {#onchunkcallback}++_attribute_++```python+OnChunkCallback = Callable[['StreamEvent', Model], Awaitable[None] | None]+```++Persistent observer. Fires for every StreamEvent pushed onto the stream+(start, text_delta, thinking_delta, toolcall_delta, done, error, ...).+Heavy listeners should early-return on irrelevant event types — this hook+fires hot. Return value is ignored.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L309)+++### OnPayloadCallback {#onpayloadcallback}++_attribute_++```python+OnPayloadCallback = Callable[[dict, Model], Awaitable[dict | None] | dict | None]+```++Optional callback for inspecting/replacing provider payloads before sending.+Return a dict to replace the payload, or None to keep unchanged.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L296)+++### OnRequestCallback {#onrequestcallback}++_attribute_++```python+OnRequestCallback = Callable[[dict, Model], Awaitable[None] | None]+```++Persistent observer. Fires just before HTTP send, after any per-call+`StreamOptions.on_payload` mutation has been applied. Receives the final+wire payload dict and the Model. Return value is ignored.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L304)+++### OnResponseBodyCallback {#onresponsebodycallback}++_attribute_++```python+OnResponseBodyCallback = Callable[[dict | None, Model, BaseException | None], Awaitable[None] | None]+```++Persistent observer. Fires exactly once per `stream()` call, in a+finally block, after the stream terminates.++- body: assembled provider response as a dict (same shape a non-streaming+ call to the provider would have returned), or None if the stream failed+ before a response could be assembled.+- exc: the exception that ended the stream (including+ `asyncio.CancelledError`), or None on normal completion.+Return value is ignored.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L315)+++### OnResponseCallback {#onresponsecallback}++_attribute_++```python+OnResponseCallback = Callable[['ProviderResponse', Model], Awaitable[None] | None]+```++Optional callback invoked after an HTTP response is received.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L300)+++### Provider {#provider}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L576)+++**Methods**++#### `stream` {#provider-stream}++```python+stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L578)+++#### `generate` {#provider-generate}++```python+generate(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None, max_output_tokens: int | None = None, temperature: float | None = None, thinking: ThinkingLevel | None = None, thinking_budgets: ThinkingBudgets | None = None) -> AssistantMessage+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L588)++++### ProviderResponse {#providerresponse}++_class_++```python+ProviderResponse(self, status: int, headers: dict[str, str] = dict())+```++HTTP response metadata exposed to on_response callbacks.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L288)+++**Attributes**++- `status`: `int`+- `headers`: `dict[str, str]`+++### StreamEvent {#streamevent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L165)+++**Attributes**++- `type`: `Literal['start', 'text_start', 'text_delta', 'text_end', 'thinking_start', 'thinking_delta', 'thinking_end', 'toolcall_start', 'toolcall_delta', 'toolcall_end', 'done', 'error']`+- `content_index`: `int | None`+- `delta`: `str | None`+- `partial`: `AssistantMessage | None`+- `error_message`: `str | None`+++### StreamOptions {#streamoptions}++_class_++Options bag for Provider.stream(), transparent to the agent loop.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L537)+++**Attributes**++- `model_config`+- `thinking`: `ThinkingLevel`+- `thinking_budgets`: `ThinkingBudgets | None`+- `signal`: `asyncio.Event | None`+- `on_payload`: `OnPayloadCallback | None`+- `on_response`: `OnResponseCallback | None`+++### TextContent {#textcontent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L99)+++**Attributes**++- `type`: `Literal['text']`+- `text`: `str`+++### ThinkingBudgets {#thinkingbudgets}++_class_++Token budgets for each thinking level.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L24)+++**Attributes**++- `minimal`: `int`+- `low`: `int`+- `medium`: `int`+- `high`: `int`+++### ThinkingContent {#thinkingcontent}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L110)+++**Attributes**++- `type`: `Literal['thinking']`+- `thinking`: `str`+++### ThinkingLevel {#thinkinglevel}++_attribute_++```python+ThinkingLevel = Literal['off', 'minimal', 'low', 'medium', 'high', 'xhigh']+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L21)+++### ToolCall {#toolcall}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L118)+++**Attributes**++- `type`: `Literal['tool_call']`+- `id`: `str`+- `name`: `str`+- `arguments`: `JsonObject`+++### ToolDefinition {#tooldefinition}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L159)+++**Attributes**++- `name`: `str`+- `description`: `str`+- `parameters`: `JsonObject`+++### ToolResultMessage {#toolresultmessage}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L145)+++**Attributes**++- `role`: `Literal['tool_result']`+- `tool_call_id`: `str`+- `tool_name`: `str`+- `content`: `list[Content]`+- `details`: `StructuredValue`+- `is_error`: `bool`+- `timestamp`: `float | None`+- `metadata`: `JsonObject`+++### Usage {#usage}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L74)+++**Attributes**++- `input_tokens`: `int`+- `output_tokens`: `int`+- `cache_read_tokens`: `int`+- `cache_write_tokens`: `int`+++### UserMessage {#usermessage}++_class_+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L125)+++**Attributes**++- `role`: `Literal['user']`+- `content`: `list[Content]`+- `timestamp`: `float | None`+- `metadata`: `JsonObject`+++### adjust_max_tokens_for_thinking {#adjust-max-tokens-for-thinking}++_function_++```python+adjust_max_tokens_for_thinking(base_max_tokens: int, model_max_tokens: int, reasoning_level: ThinkingLevel, custom_budgets: ThinkingBudgets | None = None) -> tuple[int, int]+```++Adjust max_tokens to reserve space for a thinking budget.++Given a base max_tokens (the desired output capacity), increases it to+accommodate the thinking budget while respecting the model's hard cap.+If the model cap is too small to fit both, the thinking budget is reduced+to leave at least `min_output_tokens` (1024) for output.+++**Returns**++- A `(max_tokens, thinking_budget)` tuple.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/base.py#L33)+++### FauxProvider {#fauxprovider}++_class_++```python+FauxProvider(self, *, tokens_per_second: float | None = None, token_size_min: int = 3, token_size_max: int = 5, provider_id: str = '')+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L156)+++**Attributes**++- `call_count`+- `pending_response_count`: `int`+- `prompt_cache`: `dict[str, str]` — Read-only access to the prompt cache for test assertions.+++**Methods**++#### `set_responses` {#fauxprovider-set-responses}++```python+set_responses(responses: list[FauxResponseStep]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L215)+++#### `append_responses` {#fauxprovider-append-responses}++```python+append_responses(responses: list[FauxResponseStep]) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L218)+++#### `clear_prompt_cache` {#fauxprovider-clear-prompt-cache}++```python+clear_prompt_cache() -> None+```++Clear the prompt cache, useful between test scenarios.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L225)+++#### `stream` {#fauxprovider-stream}++```python+stream(model: Model, messages: list[Message], *, system_prompt: str = '', tools: list[ToolDefinition] | None = None, options: StreamOptions | None = None) -> MessageStream+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L267)++++### faux_assistant_message {#faux-assistant-message}++_function_++```python+faux_assistant_message(content: str | FauxContentBlock | list[FauxContentBlock], *, stop_reason: str = 'stop', error_message: str | None = None) -> AssistantMessage+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L64)+++### faux_text {#faux-text}++_function_++```python+faux_text(text: str) -> TextContent+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L47)+++### faux_thinking {#faux-thinking}++_function_++```python+faux_thinking(thinking: str) -> ThinkingContent+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L51)+++### faux_tool_call {#faux-tool-call}++_function_++```python+faux_tool_call(name: str, arguments: dict[str, Any], *, id: str | None = None) -> ToolCall+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py#L55)+++### THINKING_LEVELS {#thinking-levels}++_attribute_++```python+THINKING_LEVELS: list[ThinkingLevel] = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh']+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/models.py#L12)+++### clamp_thinking_level {#clamp-thinking-level}++_function_++```python+clamp_thinking_level(model: Model, level: ThinkingLevel) -> ThinkingLevel+```++Clamp *level* to the nearest supported level for *model*.++If *level* is already supported, return it unchanged. Otherwise search+upward first (higher intensity), then downward, through the ordered level+list to find the closest available level.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/models.py#L54)+++### get_supported_thinking_levels {#get-supported-thinking-levels}++_function_++```python+get_supported_thinking_levels(model: Model) -> list[ThinkingLevel]+```++Return the thinking levels supported by *model*.++* Non-reasoning models only support `["off"]`.+* For reasoning models, levels are filtered through the model's+ `thinking_level_map`. A level mapped to `None` is unsupported.+ `"xhigh"` is only included when it has an explicit (non-None) mapping.+ All other levels are included by default when the map omits them.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/models.py#L22)+++### models_are_equal {#models-are-equal}++_function_++```python+models_are_equal(a: Model | None, b: Model | None) -> bool+```++Return `True` if *a* and *b* refer to the same model.++Comparison is by `id` and `provider_id`. Returns `False` when either+argument is `None`.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/models.py#L87)++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-tracing.mdx b/website/versioned_docs/version-0.7/api/cubepi-tracing.mdxnew file mode 100644index 0000000..bb67634--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-tracing.mdx@@ -0,0 +1,441 @@+---+id: cubepi-tracing+title: cubepi.tracing+sidebar_position: 6+hide_table_of_contents: false+---++# `cubepi.tracing`++### tracing_context {#tracing-context}++_function_++```python+tracing_context(*, tags: list[str] | tuple[str, ...] | None = None, metadata: dict[str, Any] | None = None) -> Iterator[None]+```++Scope tags / metadata onto agent runs started inside this block.++The recorder reads these contextvars on `AgentStartEvent` and+stamps them on the `invoke_agent` span as:++- `cubepi.tags` — tuple of strings (OTel attribute type)+- one attribute per metadata key, namespaced under+ `cubepi.metadata.*` (e.g. `metadata=\{"user_id": "u-42"\}` →+ `cubepi.metadata.user_id = "u-42"`). The dedicated+ sub-namespace keeps recorder-owned schema keys+ (`cubepi.run_id`, `cubepi.turn.index`, …) safe from+ caller-supplied collisions.++The contextvar nature means this works for concurrent agents:+each asyncio task tree gets its own value. Nested blocks merge+additively (tags concatenate; metadata is union with inner+keys winning).+++**Args**++- `tags` — Tags to apply to runs started in this scope. Stored as+ a tuple on the span so it round-trips through OTel's+ attribute serializer.+- `metadata` — Per-run key/value pairs. Values must be types that+ OTel attributes accept (str, bool, int, float, or a+ tuple/list of those); other shapes will be silently+ dropped by the recorder.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/context.py#L51)+++### JsonlSpanExporter {#jsonlspanexporter}++_class_++```python+JsonlSpanExporter(self, directory: str | os.PathLike[str] = './cubepi-traces')+```++Write each ReadableSpan as one JSON line.++Files: `<directory>/<YYYY-MM-DD>/<trace_id>.jsonl`, one file per trace+so the parent run and any nested subagent runs (same trace_id, different+`cubepi.run_id`) land together. The date used for the subdirectory is+the span's start time (UTC). The `trace_id` is the span's 32-hex trace+id; spans without a context fall back to `"unknown-trace"`.++Permissions: files are created mode `0o600` (user-only).++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/exporters/jsonl.py#L23)+++**Methods**++#### `export` {#jsonlspanexporter-export}++```python+export(spans: Sequence[ReadableSpan]) -> SpanExportResult+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/exporters/jsonl.py#L42)+++#### `shutdown` {#jsonlspanexporter-shutdown}++```python+shutdown() -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/exporters/jsonl.py#L69)+++#### `force_flush` {#jsonlspanexporter-force-flush}++```python+force_flush(timeout_millis: int = 30000) -> bool+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/exporters/jsonl.py#L72)++++### Meter {#meter}++_class_++```python+Meter(self, *, resource: Resource | None = None, exporters: list[MetricExporter] | None = None, export_interval_millis: int = 60000)+```++Emit OTel GenAI histograms alongside the cubepi Tracer.++Construct with a list of `MetricExporter` instances (e.g. the+OTLP metric exporter); call `attach` to start emitting.++Example:++```python+from cubepi.tracing import Tracer, Meter+from opentelemetry.exporter.otlp.proto.http.metric_exporter import (+ OTLPMetricExporter,+)++tracer = Tracer(service_name="my-bot", exporters=[...])+meter = Meter(+ resource=tracer.resource,+ exporters=[OTLPMetricExporter(endpoint="http://collector:4318/v1/metrics")],+)+tracer.attach(agent)+meter.attach(agent)+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/meter.py#L71)+++**Attributes**++- `otel_meter`: `Any` — The underlying `opentelemetry.metrics.Meter` — exposed so callers can register their own instruments.+++**Methods**++#### `attach` {#meter-attach}++```python+attach(agent: 'Agent') -> Callable[[], None]+```++Subscribe to `agent` and start emitting metrics.++Each `attach()` call gets its own `_MeterState` so+multiple concurrent agents attached to the same Meter don't+clobber each other's open-ns timestamps and attribute dicts+(codex overall-review MAJOR — previously all state lived on+the Meter instance, so a second agent's `provider_request`+overwrote the first agent's `_chat_open_ns` / `_chat_attrs`+before its response landed).++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/meter.py#L144)+++#### `force_flush` {#meter-force-flush}++```python+force_flush(timeout_seconds: float = 30.0) -> bool+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/meter.py#L194)+++#### `shutdown` {#meter-shutdown}++```python+shutdown(timeout_seconds: float = 30.0) -> None+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/meter.py#L197)+++#### `attached` {#meter-attached}++```python+attached(agent: 'Agent') -> AsyncIterator['Meter']+```++RAII wrapper around `attach`.++`async with` enters by attaching the per-attach state, exits+by calling the detach callable returned from `attach`.+Mirrors [`Tracer.attached`](./cubepi-tracing#tracer-attached) — use both for the cleanest+end-to-end shutdown:++```python+async with (+ Tracer(...) as tracer,+ Meter(resource=tracer.resource, ...) as meter,+ tracer.attached(agent),+ meter.attached(agent),+):+ await agent.prompt("...")+# auto: detach both + shutdown both+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/meter.py#L204)++++### SCHEMA_URL {#schema-url}++_attribute_++```python+SCHEMA_URL = 'https://opentelemetry.io/schemas/1.41.0'+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/schema.py#L13)+++### Tracer {#tracer}++_class_++```python+Tracer(self, *, service_name: str | None = None, service_version: str | None = None, service_namespace: str | None = None, service_instance_id: str | None = None, deployment_environment: str | None = None, agent_name: str | None = None, agent_id: str | None = None, agent_description: str | None = None, agent_version: str | None = None, exporters: list[SpanExporter] | None = None, record_content: bool = False, record_stream: bool = False, stream_dir: 'str | Path | None' = None, redact: 'Callable[[str, Any], Any] | None' = None, resource: Resource | None = None, atexit_flush: bool = True, atexit_flush_timeout_seconds: float = 5.0)+```++Attaches OTel-compatible tracing to a cubepi [`Agent`](./cubepi-agent#agent).++Construct once per process (or per service). Each `attach(agent)`+call wires the cubepi recorder to the agent's event stream and+provider listener registry; the returned callable detaches.++**Example**++```python+from cubepi.tracing import Tracer+from cubepi.tracing.exporters import JsonlSpanExporter++tracer = Tracer(+ service_name="my-bot",+ agent_name="coding-agent",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+)+detach = tracer.attach(agent)+try:+ ...+finally:+ detach()+ await tracer.shutdown()+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L117)+++**Attributes**++- `resource`: `Resource`+- `otel_tracer`: `Any` — The underlying `opentelemetry.trace.Tracer` instance.+- `redact`: `'Callable[[str, Any], Any] | None'` — Optional `(key, value) -> value` filter applied at every `set_attribute` site for content attributes.+++**Methods**++#### `attach` {#tracer-attach}++```python+attach(agent: 'Agent') -> Callable[[], Any]+```++Wire the cubepi recorder to `agent`.++Returns a `detach()` callable. When invoked:++- Synchronously: unsubscribes every hook, closes any spans+ still open from a cancelled run, sweeps MCP tool-span+ registrations — observable on the next line.+- Schedules a flush on the running event loop and returns the+ resulting `asyncio.Task` so callers can `await detach()`+ to block until buffered spans have been exported. Outside+ an async context returns `None` — call+ `await tracer.shutdown()` separately to flush.++Either `await detach()` or `await tracer.shutdown()` (or+both) must be used in the caller's `finally` block; the+synchronous `detach()` alone does not guarantee that ended+spans have left `BatchSpanProcessor` (codex overall-review+MAJOR).++Also registers this Tracer's private TracerProvider with+`cubepi.mcp._tracing` so MCP CLIENT spans flow through+the same exporters — without this step, the MCP module falls+back to the OTel global default (a no-op provider when the+caller didn't separately call `trace.set_tracer_provider`).+The detach callable unregisters.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L237)+++#### `force_flush` {#tracer-force-flush}++```python+force_flush(timeout_seconds: float = 30.0) -> bool+```++Block until all currently buffered spans are exported.++Returns `False` on timeout.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L319)+++#### `shutdown` {#tracer-shutdown}++```python+shutdown(timeout_seconds: float = 30.0) -> None+```++Flush and close all exporters. Tracer is unusable after this.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L327)+++#### `attached` {#tracer-attached}++```python+attached(agent: 'Agent') -> AsyncIterator['Tracer']+```++RAII wrapper around `attach`.++`async with` enters by attaching the recorder, exits by+running detach and (in an async context) awaiting its returned+flush `Task`. Equivalent to:++```python+detach = tracer.attach(agent)+try:+ ...+finally:+ result = detach()+ if result is not None: # async context: it's a Task+ await result+```++Use this instead of the bare `attach` / `finally: detach()`+pattern when you want the cleanup tied to a single ``async+with`` block. Combines with [`Tracer`](./cubepi-tracing#tracer)'s own context+manager:++```python+async with Tracer(...) as tracer, tracer.attached(agent):+ await agent.prompt("...")+# auto: detach (closes cancelled spans) + shutdown (flush + close)+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L368)+++#### `oneshot` {#tracer-oneshot}++```python+oneshot(*, model: 'BoundModel', operation: str = 'oneshot', metadata: 'dict[str, str | int | float | bool] | None' = None, record_content: bool | None = None) -> AsyncIterator['_OneShotSession']+```++Instrumented one-shot LLM call that produces a trace without a full Agent.++Creates an `invoke_agent` root span (so the `cubepi trace` CLI+indexes it alongside normal agent runs) and wires the provider's+listeners for the duration of the block, producing a `chat` child+span automatically when `_OneShotSession.generate` is called.++`metadata` keys are stamped as `cubepi.metadata.<key>` attributes on+the root span and are queryable with+`cubepi trace ls --meta <key>=<value>`.++`operation` is recorded in two places:++- `cubepi.oneshot.operation` — for span introspection and dashboards.+- `cubepi.metadata.oneshot_operation` — so the CLI `--meta` filter+ (which only reads `cubepi.metadata.*` attributes) can reach it:++```python+ uv run cubepi trace ls --meta oneshot_operation=consolidate_memory+ uv run cubepi trace ls --meta conversation_id=conv-123+```++Example:++```python+async with tracer.oneshot(+ model=model,+ operation="consolidate_memory",+ metadata={"conversation_id": conv_id, "user_id": user_id},+) as session:+ text = await session.generate(+ system=SYSTEM_PROMPT,+ messages=[UserMessage(...)],+ max_output_tokens=1500,+ )+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L418)++++### trace {#trace}++_function_++```python+trace(tracer: 'Tracer | None', agent: 'Agent') -> AsyncIterator[None]+```++Best-effort tracing scope for one agent run.++Attaches `tracer` to `agent` on enter and detaches + flushes its+buffered spans on exit. Every tracing fault — a failed attach, detach, or+flush — is logged and swallowed, so tracing can never break or fail the+work inside the `async with` block. Passing `tracer=None` makes the+block a no-op, which lets callers gate tracing on config without branching+at the call site.++This does **not** shut the tracer down: the tracer is reusable across runs,+so build it once (e.g. per process) and call `await tracer.shutdown()`+when the owning process stops.++Unlike [`Tracer.attached`](./cubepi-tracing#tracer-attached), which surfaces flush failures to the caller,+this helper swallows them — use it when tracing is auxiliary to the work and+must never affect its outcome.++**Example**++```python+async with trace(tracer, agent):+ await agent.prompt("...")+```++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/tracing/tracer.py#L741)++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/cubepi-utils.mdx b/website/versioned_docs/version-0.7/api/cubepi-utils.mdxnew file mode 100644index 0000000..2939ccb--- /dev/null+++ b/website/versioned_docs/version-0.7/api/cubepi-utils.mdx@@ -0,0 +1,67 @@+---+id: cubepi-utils+title: cubepi.utils+sidebar_position: 7+hide_table_of_contents: false+---++# `cubepi.utils`++### emit_event {#emit-event}++_function_++```python+emit_event(emit_fn: Callable, event: object) -> None+```+++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/utils/emit.py#L7)+++### parse_streaming_json {#parse-streaming-json}++_function_++```python+parse_streaming_json(text: str | None) -> dict+```++Parse potentially incomplete or malformed JSON from streaming.++Uses a 3-tier fallback:+ 1. `json.loads(text)`+ 2. `json.loads(repair_json(text))`+ 3. Partial-parse: repair + close open braces/brackets/strings+ 4. `\{\}` as last resort++Always returns a *dict* (or at minimum `\{\}`) so callers never need+to handle parse failures.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/utils/json_parse.py#L169)+++### repair_json {#repair-json}++_function_++```python+repair_json(text: str) -> str+```++Repair malformed JSON string literals.++Operates character-by-character, tracking whether the cursor is inside a+JSON string value. Inside strings it:++* Escapes raw control characters (0x00-0x1F) that are not already+ escaped (`\t`, `\n`, `\r` are kept when already escaped).+* Doubles a backslash before an invalid escape character+ (e.g. `\x` becomes `\\x`), making the output parseable.+* Handles truncated backslash at end-of-string.++[source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/utils/json_parse.py#L39)++++<!-- GENERATED by build_api_reference.py — DO NOT EDIT -->\ No newline at end of filediff --git a/website/versioned_docs/version-0.7/api/index.mdx b/website/versioned_docs/version-0.7/api/index.mdxnew file mode 100644index 0000000..0765f52--- /dev/null+++ b/website/versioned_docs/version-0.7/api/index.mdx@@ -0,0 +1,18 @@+---+title: API Reference+description: "Reference documentation for the CubePi Python API."+---++# API Reference++Auto-generated from CubePi's public modules.++- `cubepi.agent`+- `cubepi.providers`+- `cubepi.checkpointer`+- `cubepi.middleware`+- `cubepi.mcp`+- `cubepi.tracing`+- `cubepi.utils`++The module pages above are generated at build time from docstrings and are not editable by hand.diff --git a/website/versioned_docs/version-0.7/getting-started/core-concepts.md b/website/versioned_docs/version-0.7/getting-started/core-concepts.mdnew file mode 100644index 0000000..0f17f99--- /dev/null+++ b/website/versioned_docs/version-0.7/getting-started/core-concepts.md@@ -0,0 +1,267 @@+---+title: Core Concepts+description: "Learn the six core concepts of CubePi: Agent, Tool, Provider, Stream & Events, Middleware, and Checkpointer."+---++# Core Concepts++Six concepts cover everything CubePi does. Read this page once, then+the rest of the docs become a lookup table.++## Agent++`Agent` is the stateful façade: you construct it with a provider, a+model, optional tools, and optional middleware/checkpointer. You drive+it through three methods:++- `await agent.prompt(message)` — start a new turn from a user message.+- `await agent.resume()` — continue from the last persisted message+ (used with a checkpointer).+- `agent.steer(message)` / `agent.follow_up(message)` — queue a+ message mid-flight or after the current run.++The agent owns an `AgentState` (system prompt, tools, model, message+history, pending tool calls, streaming flag) and a list of subscribers:++```python+unsubscribe = agent.subscribe(my_listener)+# ...+unsubscribe()+```++Subscribers receive every `AgentEvent` the loop emits. They can be+sync or async.++## Tool++An `AgentTool` is a name + description + Pydantic parameter model ++async `execute` function:++```python+from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent++class SearchParams(BaseModel):+ query: str+ limit: int = 10++async def execute(tool_call_id, params: SearchParams, *, signal=None, on_update=None):+ # do work; respect `signal` if you can be aborted+ return AgentToolResult(content=[TextContent(text=f"…")])++search = AgentTool(+ name="search",+ description="Search the corpus",+ parameters=SearchParams,+ execute=execute,+)+```++The Pydantic schema is auto-converted to JSON Schema and passed to the+model. Arg parsing, error wrapping, and parallel execution are+handled by the framework. See [Tool Use](../guides/agents/tool-use)+for `execution_mode`, `on_update` (incremental progress), and+`terminate` (end the turn from a tool).++## Provider++A `Provider` is anything matching this Protocol:++```python+class Provider(Protocol):+ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream: ...++ async def generate(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ max_output_tokens: int | None = None,+ temperature: float | None = None,+ thinking: ThinkingLevel | None = None,+ thinking_budgets: ThinkingBudgets | None = None,+ ) -> AssistantMessage: ...+```++`stream()` returns a `MessageStream` — a single async iterator that+yields `StreamEvent`s and exposes the final `AssistantMessage` via+`await stream.result()`. `generate()` consumes one stream and returns+the final message directly; `BaseProvider` implements it for any+provider that implements `stream()`. Built-in providers:++- `AnthropicProvider` — Claude (Messages API, with thinking, caching,+ tool use).+- `OpenAIProvider` — GPT family (Chat Completions API).+- `OpenAIResponsesProvider` — GPT family (Responses API, server-side+ state).+- `FauxProvider` — deterministic test double (no network).++Write your own by subclassing `BaseProvider` and implementing+`stream()`. See [Providers / Custom](../guides/providers/custom).++## Stream and events++Streams and events are two layers:++- **Provider streams** — `MessageStream` yields *provider* events:+ `start`, `text_start`, `text_delta`, `text_end`, `thinking_*`,+ `toolcall_*`, `done`, `error`. This is the raw token stream.+- **Agent events** — what `agent.subscribe(...)` receives. 14+ types covering the entire loop + HITL: `agent_start`, `agent_end`,+ `turn_start`, `turn_end`, `message_start`, `message_update`,+ `message_end`, `tool_execution_start`, `tool_execution_update`,+ `tool_execution_end`, `hitl_request`, `hitl_answer`,+ `agent_suspended`, `agent_aborted`.++Subscribe to agent events for UI; for low-level token routing dig into+`event.stream_event`. See [Streaming Events](../guides/agents/streaming).++## Middleware++`Middleware` is a class with up to eight typed hooks:++| Hook | When it runs | Composition rule |+|---|---|---|+| `transform_context` | Before each model call, on the message list | Chained — each receives previous result |+| `convert_to_llm` | Right before serialisation to the provider | Last implementation wins |+| `transform_system_prompt` | Before each model call, on the system prompt | Chained |+| `before_tool_call` | Per tool call, after arg validation | First `block=True` short-circuits |+| `after_tool_call` | Per tool call, after `execute` | Later override earlier |+| `after_model_response` | After the assistant message lands | Returns a `TurnAction` controlling flow |+| `should_stop_after_turn` | At each turn boundary | Any `True` stops |+| `on_run_end` | Once after all turns complete, before `agent_end` | Messages concatenate; non-empty triggers one extra turn |++Pass middleware as a list to `Agent(middleware=[...])`. See+[Middleware → Composition](../guides/middleware/composition).++## Checkpointer++A `Checkpointer` is anything matching:++```python+class Checkpointer(Protocol):+ async def load(self, thread_id: str) -> CheckpointData | None: ...+ async def append(self, thread_id: str, messages: list[Message]) -> None: ...+ async def save_extra(self, thread_id: str, extra: dict) -> None: ...+```++Bind one to an agent with `Agent(checkpointer=cp, thread_id="…")` and+the loop will append each new message as it lands, restoring history+on the first `prompt()`. Built-in backends: `MemoryCheckpointer`,+`SQLiteCheckpointer`, `PostgresCheckpointer`, `MySQLCheckpointer`.++HITL adds two optional methods for cross-process suspend/resume:+`save_pending_request` / `load_pending_request`. All first-party+backends implement them. See [HITL guide](../guides/hitl/overview).++## HITL (Human-in-the-Loop)++CubePi ships a built-in `cubepi.hitl` module for scenarios where the+agent needs to **pause and wait for a human**:++- **Sandbox confirmation** — a dangerous tool (bash, file write) needs+ approve / deny / edit before running.+- **Mid-run questions** — the agent surfaces a structured form to the user+ and waits for the answer.++```python+from cubepi.hitl import InMemoryChannel, ConfirmToolCallMiddleware, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=…,+ tools=[bash_tool, ask_user_tool(channel)],+ middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],+ channel=channel,+)+```++The channel is an `await`-able coroutine collaborator: tool and+middleware authors write `await channel.ask(...)` or+`await channel.confirm(...)` and the channel handles the suspend. Host+code (your web app / TUI) subscribes to `channel.subscribe()` or polls+`channel.pending`, renders the request to the user, and posts the+answer via `channel.answer(qid, answer)`.++Two channel backends ship in-box:+- **`InMemoryChannel`** — single process (CLI, notebook, tests).+- **`CheckpointedChannel`** — cross-process (web service). The pending+ request is persisted to the checkpointer; a different process can+ answer hours later via `Agent.respond(question_id=, answer=)`.++Full details — the three HITL verbs, both built-in middlewares, the+cross-process suspend/resume protocol, events, trace spans, and error+reference — are in the [HITL guide](../guides/hitl/overview).++## Tracer (optional)++`Tracer` produces OpenTelemetry spans aligned with the+[GenAI Semantic Conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/),+so any OTLP backend (Jaeger, Tempo, Honeycomb, Datadog, AWS X-Ray, …)+can ingest agent runs without custom instrumentation. Install the+extra:++```bash+pip install "cubepi[tracing]" # OTel SDK+pip install "cubepi[tracing-otlp]" # + OTLP/HTTP exporter+```++then wrap your agent in an `async with`:++```python+from cubepi.tracing import Tracer+from cubepi.tracing.exporters import JsonlSpanExporter++async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ tracer.attached(agent),+):+ await agent.prompt("…")+```++Each run emits an `invoke_agent` root span containing one+`cubepi.turn` per LLM round-trip, plus `chat` (CLIENT) and+`execute_tool` children. By default **no prompt content or model+output is recorded** — opt in with `Tracer(record_content=True)` and+a `redact` callback for PII. Pair with `Meter(...)` for token /+duration / TTFC histograms. Full guide:+[Tracing → Overview](../guides/tracing/overview).++## Putting it together++```+User code+ │+ ▼+┌──────────────────────────────────────────┐+│ Agent │+│ ├─ AgentState (messages, tools, …) │+│ ├─ Middleware ── compose_middleware() │+│ ├─ Checkpointer ── append on message_end │+│ └─ run_agent_loop ◀──── the actual loop │+│ │ │+│ ▼ │+│ Provider.stream() → MessageStream │+│ │ │+│ └─ events → emit → subscribers │+└──────────────────────────────────────────┘+```++That diagram is the whole framework. The rest of this site is just+the details.diff --git a/website/versioned_docs/version-0.7/getting-started/installation.md b/website/versioned_docs/version-0.7/getting-started/installation.mdnew file mode 100644index 0000000..d7ee8dc--- /dev/null+++ b/website/versioned_docs/version-0.7/getting-started/installation.md@@ -0,0 +1,97 @@+---+title: Installation+description: "Install CubePi via pip. Python 3.11+ required. Supports Linux, macOS, and Windows."+---++# Installation++CubePi runs on **Python 3.11+**. The core has three runtime+dependencies: `pydantic`, `anthropic`, `openai`. Optional features+(SQLite, Postgres, MCP, OpenTelemetry tracing) are gated behind extras+so you only install what you use.++## With pip++```bash+pip install cubepi+```++Optional extras:++```bash+pip install "cubepi[sqlite]" # adds aiosqlite for SQLiteCheckpointer+pip install "cubepi[postgres]" # adds asyncpg + sqlalchemy + msgpack+pip install "cubepi[mcp]" # adds the MCP SDK for tool loaders+pip install "cubepi[tracing]" # adds opentelemetry-sdk for Tracer / Meter+pip install "cubepi[tracing-otlp]" # adds the OTLP/HTTP span exporter+pip install "cubepi[sqlite,mcp,tracing]" # combine+```++## With uv++[`uv`](https://github.com/astral-sh/uv) is significantly faster than+pip and is the recommended workflow:++```bash+uv add cubepi+uv add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+```++In an existing uv project, `uv sync` re-locks the environment after+edits to `pyproject.toml`.++## With Poetry++```bash+poetry add cubepi+poetry add "cubepi[sqlite,postgres,mcp,tracing,tracing-otlp]"+```++## Verifying the install++```bash+python -c "import cubepi; print(cubepi.__doc__)"+# cubepi — Pythonic async-native agent framework.+```++If you see an `ImportError`, your interpreter is likely \< 3.11 — check+`python --version`.++## Configuring provider credentials++CubePi providers read credentials from constructor arguments. Most+deployments pull them from environment variables:++```python+import os+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider++anthropic = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+openai = OpenAIProvider(provider_id="openai", api_key=os.environ["OPENAI_API_KEY"])+```++You can also pass `base_url=...` to either provider to point at a+self-hosted endpoint or compatible proxy (e.g. Anthropic Bedrock,+LiteLLM, vLLM).++For the [FauxProvider](../guides/providers/custom#using-fauxprovider-in-tests)+(used in tests), no credentials are required.++## Choosing extras: which to install++| Extra | Pulls in | Install when |+|---|---|---|+| (none) | core only | You only need in-memory state, no MCP, no tracing |+| `[sqlite]` | `aiosqlite` | Single-process app needs disk persistence |+| `[postgres]` | `asyncpg`, `sqlalchemy`, `msgpack` | Multi-instance / production — see [Postgres guide](../guides/checkpointing/postgres) |+| `[mcp]` | `mcp` | You want to mount MCP server tools into your agent |+| `[tracing]` | `opentelemetry-sdk` | You want OpenTelemetry traces (and optionally metrics) — see [Tracing guide](../guides/tracing/overview) |+| `[tracing-otlp]` | `opentelemetry-exporter-otlp-proto-http` | Ship traces to an OTLP/HTTP backend (Jaeger ≥1.50, Tempo, Honeycomb, Datadog, …) |+| `[docs]` | `griffe` | You're building the docs site (contributors only) |++## Next steps++- [Quick Start](./quick-start) — your first agent in five minutes.+- [Core Concepts](./core-concepts) — what `Agent` / `Tool` / `Provider`+ / `Checkpointer` actually mean before you start gluing them.diff --git a/website/versioned_docs/version-0.7/getting-started/quick-start.md b/website/versioned_docs/version-0.7/getting-started/quick-start.mdnew file mode 100644index 0000000..9994ca4--- /dev/null+++ b/website/versioned_docs/version-0.7/getting-started/quick-start.md@@ -0,0 +1,124 @@+---+title: Quick Start+description: "Build your first CubePi agent in minutes with this step-by-step quick-start guide."+---++# Quick Start++Ship a streaming, tool-using agent in five minutes. We'll build a+weather agent that calls a Python function as a tool, streams Claude's+response token-by-token, and exits cleanly.++## Prerequisites++- Python 3.11++- `cubepi` installed (`pip install cubepi`)+- An `ANTHROPIC_API_KEY` in your environment++## The full script++Save this as `weather_agent.py`:++```python title="weather_agent.py"+import asyncio+import os+from pydantic import BaseModel++from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # In a real app: call an HTTP weather API. Here we hard-code a reply.+ return AgentToolResult(+ content=[TextContent(text=f"72°F and sunny in {params.city}")]+ )+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])++ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a concise weather assistant.",+ tools=[+ AgentTool(+ name="get_weather",+ description="Get current weather for a city",+ parameters=GetWeatherParams,+ execute=get_weather,+ ),+ ],+ )++ # Subscribe BEFORE prompt() — that's how you see streaming events.+ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "agent_end":+ print() # final newline++ agent.subscribe(on_event)+ await agent.prompt("What's the weather in Tokyo?")+++asyncio.run(main())+```++Run it:++```bash+python weather_agent.py+```++You should see Claude stream a sentence like+*"The weather in Tokyo is currently 72°F and sunny…"*, with the+tool result threaded through.++## What just happened++CubePi ran a loop that looks (conceptually) like this:++1. `agent.prompt("What's the weather in Tokyo?")` enqueued a+ `UserMessage` and called the model.+2. The model decided to invoke `get_weather(city="Tokyo")` — CubePi+ parsed the JSON args with your Pydantic model, called your `async+ def`, and fed the result back as a `ToolResultMessage`.+3. The model produced a final assistant response, streamed back as+ `text_delta` events.+4. The loop emitted `agent_end` and returned.++`agent.subscribe(...)` registered a callback for every event the+runtime emits: `agent_start`, `turn_start`, `message_start`,+`text_delta`, `tool_execution_start`, `tool_execution_end`,+`message_end`, `turn_end`, `agent_end`. The script only cared about+`text_delta`, but you can hang any UI off the rest.++## Sources of confusion (read before you debug)++- **Subscribe before you prompt.** Listeners only receive events+ emitted *after* `subscribe` was called. Calling `prompt` first means+ the early events are gone.+- **`provider.model(...)` binds the model to its provider.** The provider holds credentials and optional `provider_id` metadata; the model id must match a model that provider supports.+- **The `execute` signature is fixed.** Keep `(tool_call_id, params,+ *, signal, on_update)` even if you don't use the keyword-only+ arguments. CubePi always passes them.+- **`agent.prompt()` can only run one prompt at a time.** While it's+ running, use [`agent.steer()`](../guides/agents/multi-turn) to+ inject a follow-up, or `agent.follow_up()` to queue one for after+ the current run.++## Next steps++- [Core Concepts](./core-concepts) — the mental model behind `Agent`,+ `Tool`, `Provider`, `Stream`, `Middleware`, `Checkpointer`.+- [Building Your First Agent](../guides/agents/first-agent) — full+ walkthrough of tools, streaming, and error handling.+- [Tool Use & Parallel Execution](../guides/agents/tool-use) — make+ the agent fan out across multiple tools at once.+- [Recipes → Weather Agent](../recipes/weather-agent) — a slightly+ beefier version of this script with real HTTP calls.diff --git a/website/versioned_docs/version-0.7/guides/agents/first-agent.md b/website/versioned_docs/version-0.7/guides/agents/first-agent.mdnew file mode 100644index 0000000..c0c9bc0--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/agents/first-agent.md@@ -0,0 +1,189 @@+---+title: Building Your First Agent+description: "Create and run your first CubePi agent — choose a provider, define a system prompt, and send a message."+---++# Building Your First Agent++This guide is the longer cousin of the [Quick Start](../../getting-started/quick-start).+We'll build a single-tool agent end-to-end, then layer on the things+you'll want next: streaming UI, error handling, and a cancel button.++## Step 1 — set up the provider and model++The provider is the connection to the LLM API; the `Model` describes+which model you want to invoke and a few capabilities.++```python+import os+from cubepi import Model+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+model = Model(+ id="claude-sonnet-4-5-20250929",+ provider="anthropic",+ max_tokens=4096, # response cap+ context_window=200_000, # hard model limit; defaults are usually fine+ temperature=0.7,+)+```++The `provider` field on `Model` is a string label (e.g. `"anthropic"`,+`"openai"`). It's used by the framework to clamp thinking levels and+tag responses — keep it stable, it doesn't have to match the+provider's internal name.++## Step 2 — declare a tool++Every tool is a Pydantic param model + an async `execute` function:++```python+from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # do real work here — call an HTTP API, query a DB, etc.+ return AgentToolResult(+ content=[TextContent(text=f"72°F and sunny in {params.city}")]+ )+++weather_tool = AgentTool(+ name="get_weather",+ description="Get current weather for a city. Returns a short text summary.",+ parameters=GetWeatherParams,+ execute=get_weather,+)+```++A few details:++- The Pydantic model is auto-converted to JSON Schema and sent to the+ model as part of the tool definition.+- The `execute` signature is fixed:+ `(tool_call_id, params, *, signal, on_update)`. The two+ keyword-only args are always passed — keep them in your signature+ even if you ignore them.+- `signal` is an `asyncio.Event` that's set when the user cancels.+ Check it inside long-running work and bail out early.+- `on_update(partial)` lets you stream incremental progress (covered+ in [Tool Use](./tool-use#streaming-tool-progress)).++## Step 3 — assemble the agent++```python+from cubepi import Agent++agent = Agent(+ model=model,+ system_prompt="You are a concise weather assistant.",+ tools=[weather_tool],+)+```++You can pass `tools=[]` (or omit it) for a plain chat agent.++## Step 4 — subscribe to events++`agent.subscribe(listener)` is how you observe the run. The listener+receives every `AgentEvent`:++```python+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "tool_execution_start":+ print(f"\n→ calling {event.tool_name}({event.args})")+ elif event.type == "tool_execution_end":+ print(f" ✓ done")++agent.subscribe(on_event)+```++You can register multiple listeners and they all receive every event.+Subscribe **before** `prompt()` — events fire as soon as the run+starts.++## Step 5 — prompt and run++```python+import asyncio++async def main():+ await agent.prompt("What's the weather in Tokyo?")++asyncio.run(main())+```++`agent.prompt()` does not return any value. The result lives on+`agent.state.messages` (the full history) and `agent.state.streaming_message`+(the current in-flight message, or `None` between turns).++## Adding error handling++When `provider.stream()` raises, the agent loop still produces an+`AssistantMessage` with `stop_reason="error"` and `error_message`+filled in. The event sequence is:+`message_start` → `message_end` → `turn_end` → `agent_end`.++You can either:++1. Catch in the subscriber, looking for `event.type == "agent_end"` and+ the last message's `stop_reason`:++ ```python+ def on_event(event, signal=None):+ if event.type == "agent_end":+ last = event.messages[-1]+ if getattr(last, "stop_reason", "") == "error":+ print(f"\nerror: {last.error_message}")+ ```++2. Or inspect `agent.state.error_message` after `await+ agent.prompt(...)` returns.++## Adding a cancel button++`agent.abort()` sets the run-level signal. The provider stream+short-circuits to `"aborted"`, in-flight tools see `signal.is_set()+== True`, and the loop emits `agent_end` cleanly.++```python+async def main():+ task = asyncio.create_task(agent.prompt("Search for…"))+ await asyncio.sleep(0.5)+ agent.abort()+ await task # always completes — never raises+ await agent.wait_for_idle()+```++## Common pitfalls++- **`RuntimeError: Agent is already processing a prompt.`** — You called+ `prompt()` twice without awaiting the first. Use `await+ agent.wait_for_idle()` or queue with `steer()` / `follow_up()`+ instead.+- **No `text_delta` events** — Did you subscribe *before* calling+ `prompt()`? Listeners only see events emitted after registration.+- **Tool not found** — The model invoked a tool whose `name` doesn't+ match any tool in `tools=[...]`. CubePi reports this as a tool result+ with `is_error=True` rather than crashing — check the+ `tool_execution_end` event's `result`.+- **Pydantic ValidationError swallowed** — If the model produces+ malformed JSON, CubePi captures the validation error and feeds it+ back as a tool error result. The model usually corrects itself on+ the next turn.++## Next++- [Tool Use & Parallel Execution](./tool-use) — multiple tools at once,+ sequential mode, `terminate`, incremental progress.+- [Streaming Events](./streaming) — the full event taxonomy.+- [Multi-turn Conversations](./multi-turn) — keeping state across+ turns, `steer`, `follow_up`, `resume`.diff --git a/website/versioned_docs/version-0.7/guides/agents/multi-turn.md b/website/versioned_docs/version-0.7/guides/agents/multi-turn.mdnew file mode 100644index 0000000..fc21806--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/agents/multi-turn.md@@ -0,0 +1,163 @@+---+title: Multi-turn Conversations+description: "Build multi-turn conversational agents with CubePi's stateful agent loop and message history."+---++# Multi-turn Conversations++A "turn" in CubePi is one round of: user input → model response (and+maybe tools) → optional more model responses to tool results. The+agent's `_messages` list grows across turns; this guide covers how to+drive multi-turn flows correctly and how to inject input while the+agent is mid-thought.++## The basic pattern++Just call `prompt` again after the previous call returns:++```python+await agent.prompt("Hi, my name is Sam.")+await agent.prompt("What's my name?")+# → "Your name is Sam."+```++History lives on `agent.state.messages`. CubePi appends each user+message, each assistant message, and each tool result. The provider+gets the full list every time, so context windows matter (see+[Context Management](#context-management) below).++## In-flight steering: `agent.steer()`++Sometimes the user has a correction or extra context while the model+is still mid-turn. Use `steer()`:++```python+import asyncio++async def main():+ task = asyncio.create_task(agent.prompt("Plan a 5-day trip to Kyoto."))+ await asyncio.sleep(2)+ # User changed their mind:+ agent.steer(UserMessage(content=[TextContent(text="Make it 3 days, not 5.")]))+ await task+```++A steering message is enqueued; the loop picks it up between turns (in+practice, between a tool batch and the next model call). The agent+sees it before its next response — it's not lost.++`steering_mode` on `Agent` controls drain behaviour:++- `"one-at-a-time"` (default) — one queued message per pickup point.+- `"all"` — every queued message is drained at once.++## Queued follow-ups: `agent.follow_up()`++`follow_up` is for *"after the current run, start a new turn with+this."* It's the typical pattern for a chat UI: the user types while+the assistant is still responding.++```python+agent.follow_up(UserMessage(content=[TextContent(text="And what about Osaka?")]))+# When the current prompt() finishes, the loop picks this up+# automatically and starts a new turn.+```++If the agent is idle when you call `follow_up`, you still need to+trigger a run — most apps call `await agent.resume()` once `prompt()`+returns, to drain the queue.++## `resume()` — continue from the last message++`resume()` is the "pick up where we left off" entry point. Two uses:++1. **After loading from a checkpointer.** The state has messages but+ no in-flight prompt. `resume()` looks at the last message and acts:+ - assistant → expects a queued steer/follow_up to convert into a+ new user turn; otherwise raises.+ - tool_result → re-invokes the model with the tool output.+ - user → re-invokes the model on the user message.+2. **After abort.** Once `agent.abort()` has cleanly torn down a run,+ you can resume.++```python+async with SQLiteCheckpointer("conv.db") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id="conv-1")+ await agent.prompt("hello") # loads existing history first+ await agent.prompt("how are you?")+```++The first `prompt()` after instantiation loads any existing thread.+Subsequent calls just append.++## Context management++CubePi does **not** truncate or summarise context on your behalf. The+full message list is sent to the model on every turn. Strategies:++- **Manual truncation** — Implement a+ [`transform_context`](../middleware/hooks#transform_context)+ middleware that returns a sliding window.+- **Summarisation pass** — Periodically inject a summary message and+ drop older ones via `transform_context`.+- **Different `convert_to_llm`** — Reshape the history just before+ serialisation (the last opportunity), without mutating+ `agent.state.messages`. The user-visible history stays full; the+ model sees less.++See [Middleware → Examples](../middleware/examples#sliding-window-truncation)+for a working example.++## Cancellation and idle waits++```python+agent.abort() # signals the current run to stop+await agent.wait_for_idle() # awaits the run-cleanup+```++`wait_for_idle()` is a no-op if the agent is already idle. It's safe+to call anywhere.++## Restoring state from disk++```python+from cubepi.checkpointer import SQLiteCheckpointer++async with SQLiteCheckpointer("conv.db") as cp:+ agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id="user-42",+ )+ # First prompt() restores the saved history if any.+ await agent.prompt("continue our chat")+```++The `_extra` slot (an arbitrary `dict[str, Any]`) is also restored.+Middleware that wants to persist per-thread state should write into+`context.extra`; the checkpointer's `save_extra` is called at+`agent_end`.++## Common pitfalls++- **`prompt()` while another `prompt()` is in flight** raises+ `RuntimeError`. Use `steer()` or `follow_up()`, or `wait_for_idle()`+ first.+- **`resume()` with last message = assistant and no queue** raises+ `"Cannot continue from message role: assistant"`. Either queue a+ follow-up first or call `prompt()` instead.+- **History grows unbounded** — without a `transform_context`+ middleware you'll eventually hit context limits. Plan for+ truncation/summarisation early.+- **Concurrent agents on the same `thread_id`** — Append-only is safe+ for ordering but two agents writing the same thread will interleave+ messages. Use one agent instance per thread or coordinate at the+ application layer.++## See also++- [Streaming Events](./streaming) — exact event order around+ steering/follow_up.+- [Checkpointing → SQLite](../checkpointing/sqlite) — persisting history.+- [Recipes → Persistent Chat](../../recipes/persistent-chat) — full+ multi-turn app with history reload.diff --git a/website/versioned_docs/version-0.7/guides/agents/streaming.md b/website/versioned_docs/version-0.7/guides/agents/streaming.mdnew file mode 100644index 0000000..288931d--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/agents/streaming.md@@ -0,0 +1,171 @@+---+title: Streaming Events+description: "Stream events and tokens from CubePi agents in real time using subscribers and MessageStream."+---++# Streaming Events++CubePi exposes two streams, layered:++1. **Provider stream** — `MessageStream` you get from+ `provider.stream(...)`. Yields `StreamEvent`s describing the raw+ wire output: text deltas, thinking deltas, tool-call deltas, then+ `done` or `error`.+2. **Agent event stream** — what listeners registered with+ `agent.subscribe(...)` see. Eleven event types covering the full+ lifecycle of a `prompt()` call, including provider events wrapped+ inside `MessageUpdateEvent`.++Most app code only needs the agent event stream.++## The eleven agent events++| Event | Fires when |+|---|---|+| `agent_start` | At the very beginning of `prompt()` / `resume()` |+| `turn_start` | Before each model invocation (one or more per `prompt`) |+| `message_start` | Right before a new message (user / assistant / tool result) is added to history |+| `message_update` | On every provider `StreamEvent` (deltas, etc.); has `event.stream_event` attached |+| `message_end` | After a message is finalised |+| `tool_execution_start` | A tool call is dispatched (one per call, before parallel `asyncio.gather`) |+| `tool_execution_update` | A tool reported partial progress via `on_update(...)` |+| `tool_execution_end` | A tool finished (or failed) |+| `turn_end` | After all tools in a batch settled, or after a tool-less assistant response |+| `agent_end` | The whole `prompt()` call has finished — clean exit, abort, or error |++`MessageStartEvent` and `MessageEndEvent` apply to *every* message,+not just assistant ones. User and tool-result messages also get them.++## Event order for a tool-using turn++A typical "user asks question → model calls one tool → model responds"+sequence:++```+agent_start+turn_start+ message_start (UserMessage from prompt)+ message_end (UserMessage)+ message_start (AssistantMessage — empty partial)+ message_update × N (text_delta, toolcall_delta, …)+ message_end (AssistantMessage — finalised)+ tool_execution_start+ tool_execution_end+ message_start (ToolResultMessage)+ message_end (ToolResultMessage)+turn_end+turn_start (loop continues with the tool result in context)+ message_start+ message_update × N+ message_end+turn_end+agent_end+```++## Subscribing++```python+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)++unsubscribe = agent.subscribe(on_event)+```++`agent.subscribe(...)` never receives an event whose top-level `.type`+equals `"text_delta"` — that's a *provider* event type. The agent+wraps every provider event in a `MessageUpdateEvent` and tucks the+original under `event.stream_event`. Match on both fields, as above.++Listeners can be sync or async; async ones are awaited. The second+argument is the run-level `asyncio.Event` (the abort signal) — you can+inspect `signal.is_set()` to know if the run was cancelled.++To stop receiving events, call the function returned by `subscribe`.++## Filtering for text deltas (the common case)++```python+def on_event(event, signal=None):+ if event.type == "message_update":+ sub = event.stream_event+ if sub.type == "text_delta":+ print(sub.delta, end="", flush=True)+```++Equivalent defensive form using `getattr`, since only+`MessageUpdateEvent` carries a `stream_event` attribute:++```python+def on_event(event, signal=None):+ if getattr(event, "stream_event", None) and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+```++The shape that CubePi guarantees is the one in the table above+(`MessageUpdateEvent.stream_event.delta`). Always match the outer+event's `type == "message_update"` (or check `stream_event` exists)+before reaching into `stream_event.type`.++## Provider `StreamEvent` types++Inside a `MessageUpdateEvent.stream_event`, the type tells you what+the model is emitting:++| `stream_event.type` | Meaning | Useful fields |+|---|---|---|+| `start` | Start of an assistant message | `partial` |+| `text_start` | Beginning of a text block | `content_index` |+| `text_delta` | Token chunk | `delta` |+| `text_end` | End of a text block | `content_index` |+| `thinking_start` / `thinking_delta` / `thinking_end` | Extended thinking blocks | `delta` |+| `toolcall_start` / `toolcall_delta` / `toolcall_end` | Streaming JSON args for a tool call | `delta` (partial JSON) |+| `done` | Stream finished normally | — |+| `error` | Stream errored | `error_message` |++The `partial` field on every event is a deep-copied snapshot of the+in-progress `AssistantMessage` — handy for UIs that re-render+on every event without tracking deltas themselves.++## Iterating a raw provider stream++If you're skipping `Agent` entirely (rare — usually means you're+writing a test or a custom orchestrator), iterate the stream+directly:++```python+stream = await provider.stream(+ model=model,+ messages=[UserMessage(content=[TextContent(text="hello")])],+)+async for event in stream:+ if event.type == "text_delta":+ print(event.delta, end="", flush=True)+final = await stream.result() # waits for the AssistantMessage+```++`stream.result()` returns even after iteration ends; it's the+canonical way to get the final message.++## Common pitfalls++- **Subscribing after `prompt()`** — Early events are gone.+ `agent.subscribe(...)` first, always.+- **Listener exceptions crash the loop?** — They don't, but they+ propagate up to the next `await`. Wrap risky work in `try/except`.+- **Order on parallel tools** — `tool_execution_start` events come in+ the model's emit order; `tool_execution_end` events come in+ completion order. Don't rely on pairs being adjacent.+- **`message_update` fires for every delta** — High-frequency tokens+ can flood a slow listener (e.g. a websocket). Batch on the consumer+ side if needed.+- **`text_delta` for thinking?** — No. Thinking blocks emit+ `thinking_*` events. Filter on `event.stream_event.type` if you only+ want visible text.++## See also++- [Tool Use](./tool-use) — the `tool_execution_*` triplet in detail.+- [Multi-turn](./multi-turn) — event order around steering and resume.+- [API Reference → StreamEvent](../../api/cubepi-providers#streamevent)+ for the field-level schema.diff --git a/website/versioned_docs/version-0.7/guides/agents/tool-use.md b/website/versioned_docs/version-0.7/guides/agents/tool-use.mdnew file mode 100644index 0000000..01f80c8--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/agents/tool-use.md@@ -0,0 +1,194 @@+---+title: Tool Use & Parallel Execution+description: "Register tools, execute them in parallel or sequentially, and handle results with Pydantic validation in CubePi."+---++# Tool Use & Parallel Execution++Tools are how an agent acts on the world. CubePi turns each `AgentTool`+into a JSON Schema for the model, validates arguments with Pydantic,+runs the work, and feeds the result back as a `ToolResultMessage`. By+default tools run in parallel when the model calls more than one in a+single turn.++## Anatomy of a tool++```python+from pydantic import BaseModel, Field+from cubepi import AgentTool, AgentToolResult, TextContent+++class SearchParams(BaseModel):+ query: str = Field(..., description="The natural-language query")+ limit: int = Field(10, ge=1, le=100)+++async def search(tool_call_id, params: SearchParams, *, signal=None, on_update=None):+ results = await my_search_backend(params.query, params.limit)+ return AgentToolResult(+ content=[TextContent(text="\n".join(results))],+ details={"raw_results": results}, # passes through to ToolResultMessage.details+ )+++search_tool = AgentTool(+ name="search",+ description="Search the internal knowledge base.",+ parameters=SearchParams,+ execute=search,+)+```++The `description` is shown to the model verbatim — write it for the+model, not for humans. Pydantic `Field(description=...)` propagates+into the JSON Schema and helps the model understand each parameter.++## Parallel by default++When the model emits multiple tool calls in one assistant message,+CubePi schedules them on `asyncio.create_task()` and gathers them.+That's almost always what you want.++```python+agent = Agent(+ model=model,+ tools=[search_tool, fetch_url_tool, summarise_tool],+)+```++The event stream emits `tool_execution_start` for all of them up+front, interleaved `tool_execution_update` events as each tool reports+progress, then `tool_execution_end` per tool in completion order.++## Forcing sequential execution++There are two ways to opt out of parallel mode:++1. **Per agent** — `Agent(tool_execution="sequential")`. All tool+ batches run one-by-one in the order the model emitted them.++2. **Per tool** — set `execution_mode="sequential"` on the+ `AgentTool`. If *any* tool in the current batch is sequential, the+ whole batch falls back to sequential.++ ```python+ write_db_tool = AgentTool(+ name="write_db",+ description="Persist a record.",+ parameters=WriteDbParams,+ execute=write_db,+ execution_mode="sequential", # opt out of parallelism for safety+ )+ ```++The built-in `ask_user` HITL tool (see [HITL guide](../hitl/overview)) sets+`execution_mode="sequential"` — it pauses the agent for human input, so+the tool batch runs one-by-one.++Use sequential mode when tools mutate shared state (a DB, a counter)+and you want a deterministic order.++## Streaming tool progress++Long-running tools can stream partial updates that show up as+`tool_execution_update` events on the agent stream:++```python+async def slow_search(tool_call_id, params, *, signal=None, on_update=None):+ for i, page in enumerate(await fetch_pages(params.query)):+ if signal and signal.is_set():+ break+ if on_update:+ on_update({"progress": i, "total": len(pages), "url": page.url})+ await process(page)+ return AgentToolResult(content=[TextContent(text="done")])+```++`partial_result` in the event is whatever object you handed to+`on_update`. Use a small dict; it doesn't end up in the model's+context.++## Cancelling in-flight tools++The `signal` argument is the same `asyncio.Event` that+`agent.abort()` sets. Check it in any loop:++```python+async def long_running(tool_call_id, params, *, signal=None, on_update=None):+ for chunk in big_dataset:+ if signal and signal.is_set():+ return AgentToolResult(content=[TextContent(text="cancelled")])+ await process_chunk(chunk)+```++If your work is one big `await`, wrap it in+`asyncio.wait_for(..., timeout=…)` or use the abort callbacks of the+underlying library.++## Returning errors++Two ways:++1. **Raise an exception.** CubePi catches it, turns it into an+ `AgentToolResult` with `is_error=True` and the exception string as+ `TextContent`.+2. **Return `is_error=True` explicitly.** Useful when you want a+ structured error body:++ ```python+ return AgentToolResult(+ content=[TextContent(text="Rate limit exceeded; try again in 60s")],+ is_error=True,+ )+ ```++Either way, the model receives a tool result that's clearly marked as+an error and usually adapts (retries with different args, asks the+user, etc.).++## Ending the turn from a tool: `terminate`++A tool can declare *"after this, stop looping — don't ask the model+again."* Set `terminate=True`:++```python+async def submit_final_answer(tool_call_id, params, *, signal=None, on_update=None):+ save_answer(params.answer)+ return AgentToolResult(+ content=[TextContent(text="submitted")],+ terminate=True,+ )+```++CubePi only terminates if *every* tool result in the current batch is+`terminate=True`. The agent loop emits `turn_end`, then `agent_end`,+and exits.++## Common pitfalls++- **Forgetting the keyword-only args** — `execute(tool_call_id,+ params)` will work in dev but crash when the framework passes+ `signal=`. Keep `*, signal=None, on_update=None` in the signature.+- **Heavy `details` payloads** — `details` is preserved through the+ agent event but is *not* shown to the model. Don't pack massive blobs+ there unless you have a downstream consumer.+- **Pydantic strictness surprises** — `Field(..., min_length=1)` lets+ the model see the constraint via JSON Schema; constraints help, but+ remember the model still sometimes sends bad JSON. CubePi turns the+ `ValidationError` into a tool error result; you don't need to wrap+ validation yourself.+- **`tools=[]` and the model still asks for a tool** — Usually means+ the prompt mentions one. Either remove the suggestion from the+ system prompt or pass the tool.++## See also++- [Streaming Events](./streaming) — how `tool_execution_*` events fit+ the larger event taxonomy.+- [Middleware → before_tool_call](../middleware/hooks#before_tool_call)+ and [after_tool_call](../middleware/hooks#after_tool_call) —+ interception, policy, retries.+- [Recipes → Weather Agent](../../recipes/weather-agent) — a real+ HTTP-calling tool, end-to-end.+- [MCP Loading](../mcp/loading) — pull an entire toolset off an MCP+ server in one call.diff --git a/website/versioned_docs/version-0.7/guides/checkpointing/custom.md b/website/versioned_docs/version-0.7/guides/checkpointing/custom.mdnew file mode 100644index 0000000..7e8e0a8--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/checkpointing/custom.md@@ -0,0 +1,202 @@+---+title: Custom Backends+description: "Implement a custom checkpointer backend for CubePi using the Checkpointer protocol."+---++# Custom Checkpointing Backends++The `Checkpointer` protocol is three async methods:++```python+class Checkpointer(Protocol):+ async def load(self, thread_id: str) -> CheckpointData | None: ...+ async def append(self, thread_id: str, messages: list[Message]) -> None: ...+ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None: ...+```++That's the whole contract. Implement it for Redis, DynamoDB, S3, a+filesystem, an in-memory dict — anything that can append-and-list.++## When the agent calls each method++| Method | Called when |+|---|---|+| `load(thread_id)` | Once, on the first `prompt()` after agent construction |+| `append(thread_id, messages)` | Inside `message_end`, every time a message is finalised |+| `save_extra(thread_id, extra)` | At `agent_end`, with the current `_extra` dict |++`load` returns `None` for unknown threads. `append` and `save_extra`+do nothing useful if you don't already have a thread; create the row+on first call.++## A minimal Redis example++```python+import json+from typing import Any+import redis.asyncio as aredis+from cubepi.checkpointer.base import CheckpointData+from cubepi.providers.base import AssistantMessage, Message, ToolResultMessage, UserMessage+++_ROLE_TO_CLS: dict[str, type[Message]] = {+ "user": UserMessage,+ "assistant": AssistantMessage,+ "tool_result": ToolResultMessage,+}+++class RedisCheckpointer:+ def __init__(self, redis_url: str, prefix: str = "cubepi:") -> None:+ self._url = redis_url+ self._prefix = prefix+ self._r: aredis.Redis | None = None++ async def __aenter__(self):+ self._r = aredis.from_url(self._url)+ return self++ async def __aexit__(self, *args):+ if self._r is not None:+ await self._r.aclose()+ self._r = None++ def _msgs_key(self, thread_id: str) -> str:+ return f"{self._prefix}msgs:{thread_id}"++ def _extra_key(self, thread_id: str) -> str:+ return f"{self._prefix}extra:{thread_id}"++ async def load(self, thread_id: str) -> CheckpointData | None:+ raw_msgs = await self._r.lrange(self._msgs_key(thread_id), 0, -1)+ raw_extra = await self._r.get(self._extra_key(thread_id))++ if not raw_msgs and raw_extra is None:+ return None++ messages: list[Message] = []+ for item in raw_msgs:+ data = json.loads(item)+ cls = _ROLE_TO_CLS.get(data.get("role"))+ if cls is not None:+ messages.append(cls.model_validate(data))++ extra: dict[str, Any] = json.loads(raw_extra) if raw_extra else {}+ return CheckpointData(messages=messages, extra=extra)++ async def append(self, thread_id: str, messages: list[Message]) -> None:+ if not messages:+ return+ pipe = self._r.pipeline()+ for m in messages:+ pipe.rpush(self._msgs_key(thread_id), json.dumps(m.model_dump(mode="json")))+ await pipe.execute()++ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None:+ # Merge-style: load existing, update, write back.+ raw = await self._r.get(self._extra_key(thread_id))+ existing = json.loads(raw) if raw else {}+ existing.update(extra)+ await self._r.set(self._extra_key(thread_id), json.dumps(existing))+```++Usage:++```python+async with RedisCheckpointer("redis://localhost:6379") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id="user-42")+ await agent.prompt("hi")+```++## Invariants worth holding++1. **Append-only.** Don't update past messages. The agent assumes the+ history it appended is what you'll return on `load`.+2. **Order preserved.** `load` returns messages in the order they were+ appended. Use a list, a sorted key, or a sequence column.+3. **Idempotent re-`load`.** Calling `load` twice on the same thread+ should yield identical results. (CubePi calls it once, but tools+ often need to too.)+4. **`extra` is a merge.** `save_extra({"a": 1})` followed by+ `save_extra({"b": 2})` should leave `{"a": 1, "b": 2}` — not just+ `{"b": 2}`. The agent calls this with the full dict, but middleware+ composes its writes.+5. **Reconstruct messages with `model_validate`.** Use the role+ discriminator (`UserMessage` / `AssistantMessage` /+ `ToolResultMessage`) to pick the right class.++## Custom backend without async context manager++The `Checkpointer` Protocol doesn't require `__aenter__` /+`__aexit__`. Built-in checkpointers use it because they manage+network resources, but a pure in-memory or local-file backend can+skip it:++```python+class FileCheckpointer:+ def __init__(self, dir_path: str) -> None:+ self._dir = pathlib.Path(dir_path)+ self._dir.mkdir(parents=True, exist_ok=True)++ async def load(self, thread_id: str) -> CheckpointData | None:+ ...+ async def append(self, thread_id: str, messages: list[Message]) -> None:+ ...+ async def save_extra(self, thread_id: str, extra: dict[str, Any]) -> None:+ ...++# Pass directly:+agent = Agent(model=…, checkpointer=FileCheckpointer("/tmp/cp"), thread_id="x")+```++## Testing your backend++Drop-in test pattern using `FauxProvider`:++```python+from cubepi import Agent, Model+from cubepi.providers import FauxProvider, faux_assistant_message++async def test_roundtrip():+ cp = MyCheckpointer(…)+ provider = FauxProvider(provider_id="faux")+ provider.set_responses([faux_assistant_message("hello")])++ agent1 = Agent(model=provider.model("t"),+ checkpointer=cp, thread_id="t1")+ agent1.subscribe(lambda e, s=None: None)+ await agent1.prompt("hi")++ # Fresh agent, same thread — should restore history.+ provider2 = FauxProvider(provider_id="faux")+ provider2.set_responses([faux_assistant_message("hello again")])+ agent2 = Agent(model=provider2.model("t"),+ checkpointer=cp, thread_id="t1")+ agent2.subscribe(lambda e, s=None: None)+ await agent2.prompt("hi again")++ assert len(agent2.state.messages) == 4 # 2 user + 2 assistant+```++## Common pitfalls++- **Mutating returned `CheckpointData`** — Either deep-copy on the way+ in, or document that the agent owns the list. CubePi's built-ins+ copy.+- **Losing `metadata`** — `model_dump(mode="json")` preserves+ `metadata`. If you serialise via `__dict__` you'll drop it.+- **Race on `save_extra` merge** — A read-modify-write pattern can+ lose concurrent writes. Use a SQL `JSONB ||` or a Redis Lua script+ if you have concurrent writers per thread.+- **Forgetting to register the role for tool results** — Easy to map+ `"user"` and `"assistant"` and forget `"tool_result"`. All three are+ needed.++## See also++- [`Checkpointer` Protocol API](../../api/cubepi-checkpointer) — full+ signature.+- [SQLiteCheckpointer source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/sqlite.py)+ — a complete reference implementation.+- [PostgresCheckpointer source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/checkpointer.py)+ — production-grade reference.diff --git a/website/versioned_docs/version-0.7/guides/checkpointing/mysql.md b/website/versioned_docs/version-0.7/guides/checkpointing/mysql.mdnew file mode 100644index 0000000..fdb82ae--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/checkpointing/mysql.md@@ -0,0 +1,205 @@+---+title: MySQL Checkpointing+description: "Use MySQLCheckpointer for persistent agent state in CubePi with the MySQL backend."+---++# MySQL Checkpointing++`MySQLCheckpointer` is a production-grade persistence backend for+deployments standardized on MySQL. It mirrors+[`PostgresCheckpointer`](./postgres): `aiomysql` connection pool,+`msgpack` payloads, a queryable JSON `metadata` column, and a+host-managed schema verified on connect. Per-thread `seq` allocation is+serialized with a `SELECT … FOR UPDATE` row lock so multiple processes+can append to the same `thread_id` safely.++**Requires MySQL 8.0.13+** (for JSON expression defaults) on the InnoDB+engine.++Install the extra:++```bash+pip install "cubepi[mysql]"+```++This pulls in `aiomysql`, `sqlalchemy`, and `msgpack`.++## Basic usage++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import MySQLCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with MySQLCheckpointer("mysql://user:pass@host:3306/dbname") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("hello")+++asyncio.run(main())+```++The connection is configured from a `mysql://user:pass@host:port/db`+DSN. Pool sizing:++```python+async with MySQLCheckpointer(+ "mysql://…",+ min_pool_size=2,+ max_pool_size=20,+) as cp:+ …+```++## Schema++The checkpointer expects three tables: `cubepi_threads`,+`cubepi_messages`, and `cubepi_schema_version`. Like Postgres (and+unlike SQLite), CubePi **does not create these for you** — it verifies+on `__aenter__` that they exist with the expected `schema_version`.++If they're missing, you get `CubepiSchemaUninitialized`. If the version+doesn't match this CubePi release, you get `CubepiSchemaMismatch`. A+production database belongs to the host application's migration system+(Alembic, …), not to a third-party library.++### Bootstrapping via Alembic++CubePi exposes the SQLAlchemy `MetaData` so your migrations can adopt+the schema:++```python+# alembic/env.py+from cubepi.checkpointer.mysql import cubepi_metadata, EXPECTED_SCHEMA_VERSION++target_metadata = [my_app_metadata, cubepi_metadata]+```++Then autogenerate a revision:++```bash+alembic revision --autogenerate -m "add cubepi checkpointer"+```++The `cubepi_messages` table is `KEY`-partitioned, which SQLAlchemy's+autogenerate cannot express, so append the partition clause and the+schema-version write with the provided helpers:++```python+# In a migration's upgrade():+from cubepi.checkpointer.mysql.alembic_helpers import (+ messages_partition_clause,+ write_schema_version_op,+)++def upgrade():+ op.create_table("cubepi_threads", ...)+ op.create_table(+ "cubepi_messages", ...,+ mysql_engine="InnoDB",+ )+ # KEY(thread_id) partitioning is not autogenerated — apply it explicitly:+ op.execute(+ "ALTER TABLE cubepi_messages " + messages_partition_clause()+ )+ op.create_table("cubepi_schema_version", ...)+ # write_schema_version_op() returns two ';'-separated statements+ # (DELETE then INSERT). MySQL/pymysql runs one statement per execute,+ # so split and execute each:+ for stmt in write_schema_version_op().split(";"):+ if stmt.strip():+ op.execute(stmt)+```++`write_schema_version_op()` is idempotent: it deletes any rows from a+prior CubePi version and inserts the current one. When CubePi later+bumps `EXPECTED_SCHEMA_VERSION`, generate a new revision and run it+again.++## Data model++```+cubepi_threads+ thread_id (PK) -- VARCHAR(255) utf8mb4_bin (case-sensitive)+ parent_thread_id -- self-FK, for forks+ forked_at_seq -- seq number at fork point+ extra -- JSON+ created_at / updated_at++cubepi_messages+ thread_id, seq -- composite PK; partitioned by KEY(thread_id) into 64+ role -- "user" | "assistant" | "tool"+ metadata -- JSON (not indexed; see below)+ payload -- LONGBLOB (msgpack)+ created_at++cubepi_schema_version+ version (PK)+```++Important properties:++- **`(thread_id, seq)` is the message identity.** `seq` is monotonic+ per thread, allocated under a `SELECT … FOR UPDATE` row lock on the+ thread row. Two concurrent writers on the same thread serialize+ cleanly.+- **`payload` is msgpack-encoded `model.model_dump(mode="json")`.**+ CubePi reconstructs the Pydantic model on read.+- **`metadata` is JSON, queryable** via MySQL JSON functions. It is+ **not** indexed — MySQL can't index a JSON column directly. If you+ need to query messages by metadata, add a generated-column index+ host-side.+- **Messages are partitioned by `KEY(thread_id)` into 64 partitions.**+ Even distribution, no per-thread bottleneck. The table has **no+ foreign key** to `cubepi_threads` (MySQL forbids FKs on partitioned+ tables); integrity is preserved by the lazy thread-row insert on+ `append`.++## Differences from the Postgres backend++| | Postgres | MySQL |+|---|---|---|+| Driver | `asyncpg` | `aiomysql` |+| `seq` lock | `pg_advisory_xact_lock` | `SELECT … FOR UPDATE` |+| Messages partitioning | `HASH(thread_id)` + FK | `KEY(thread_id)`, no FK |+| `metadata` index | GIN | none (add a generated column) |+| Thread IDs | `TEXT` | `VARCHAR(255) utf8mb4_bin` |+| Min version | — | MySQL 8.0.13+, InnoDB |++## `save_extra` semantics++`save_extra` does a shallow top-level merge, not a replace — the same+behavior as Postgres and SQLite. Writing `{"foo": 1}` then `{"bar": 2}`+leaves `{"foo": 1, "bar": 2}`. (Internally it reads the current `extra`+under a row lock and writes the merged dict, rather than using+`JSON_MERGE_PATCH`, whose null-deletion and deep-merge semantics differ+from `dict.update`.)++## Common pitfalls++- **`CubepiSchemaUninitialized`** — Your DB is empty, your migrations+ didn't run, or the `cubepi_schema_version` table is malformed. Apply+ the host alembic upgrade first.+- **`CubepiSchemaMismatch`** — You upgraded CubePi but didn't generate a+ new migration. Generate one, apply it, and CubePi will start.+- **JSON default errors on older MySQL** — JSON columns use+ `DEFAULT (JSON_OBJECT())`, which requires MySQL 8.0.13+.+- **Case-insensitive thread-ID collisions** — Thread IDs use the+ `utf8mb4_bin` collation so `UserA` and `usera` stay distinct. If you+ hand-roll the DDL, keep that collation.++## See also++- [Postgres Checkpointing](./postgres) — the Postgres-backed sibling.+- [SQLite Checkpointing](./sqlite) — single-process alternative.+- [Custom Backends](./custom) — Protocol details.+- [Package README](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/mysql/README.md)+ — the full host-integration runbook next to the code.diff --git a/website/versioned_docs/version-0.7/guides/checkpointing/postgres.md b/website/versioned_docs/version-0.7/guides/checkpointing/postgres.mdnew file mode 100644index 0000000..05b9495--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/checkpointing/postgres.md@@ -0,0 +1,218 @@+---+title: Postgres Checkpointing+description: "Use PostgresCheckpointer for production-grade agent state persistence with asyncpg and advisory locks."+---++# Postgres Checkpointing++`PostgresCheckpointer` is the production-grade persistence backend.+It uses `asyncpg` with a connection pool, `msgpack` for payloads, and+a per-thread Postgres advisory lock so multiple processes can write+the same `thread_id` without trampling each other.++Install the extra:++```bash+pip install "cubepi[postgres]"+```++This pulls in `asyncpg`, `sqlalchemy`, and `msgpack`.++## Basic usage++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import PostgresCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with PostgresCheckpointer("postgresql://user:pass@host/dbname") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("hello")+++asyncio.run(main())+```++The DSN is whatever `asyncpg.create_pool(...)` accepts. Pool sizing:++```python+async with PostgresCheckpointer(+ "postgresql://…",+ min_pool_size=2,+ max_pool_size=20,+) as cp:+ …+```++## Schema++The checkpointer expects three tables: `cubepi_threads`,+`cubepi_messages`, and `cubepi_schema_version`. Unlike SQLite, CubePi+**does not create these for you** — it verifies on `__aenter__` that+they exist with the expected `schema_version`.++If they're missing, you get `CubepiSchemaUninitialized`. If the+version doesn't match this CubePi release, you get+`CubepiSchemaMismatch`.++The reason: a production database belongs to the host application's+migration system (Alembic, Atlas, …), not to a third-party library+that might fight your existing migrations.++### Bootstrapping via Alembic++CubePi exposes the SQLAlchemy `MetaData` so your migrations can adopt+the schema:++```python+# alembic/env.py+from cubepi.checkpointer.postgres import cubepi_metadata, EXPECTED_SCHEMA_VERSION++target_metadata = [my_app_metadata, cubepi_metadata]+```++Then autogenerate a revision:++```bash+alembic revision --autogenerate -m "add cubepi checkpointer"+```++Autogenerate emits the `CREATE TABLE`s from `cubepi_metadata`, but+SQLAlchemy `MetaData` **cannot model two things CubePi needs**, so add+them to the generated migration by hand: the 64 hash partitions of+`cubepi_messages` (via `create_message_partitions_op()`) and the+`cubepi_schema_version` row (via `write_schema_version_op()`). Use the+helpers:++```python+# In a migration's upgrade():+from cubepi.checkpointer.postgres.alembic_helpers import (+ create_message_partitions_op,+ write_schema_version_op,+)++def upgrade():+ op.create_table(...) # auto-generated from cubepi_metadata+ op.execute(create_message_partitions_op()) # creates the 64 hash partitions+ op.execute(write_schema_version_op()) # records EXPECTED_SCHEMA_VERSION+```++Both helpers return a SQL string — you pass them to `op.execute(...)`.+`write_schema_version_op()` is idempotent: it deletes any rows from a+prior CubePi version and inserts the current one.++When CubePi later upgrades and bumps `EXPECTED_SCHEMA_VERSION`, you+generate a new revision and call `op.execute(write_schema_version_op())`+again.++## Data model++```+cubepi_threads+ thread_id (PK)+ parent_thread_id -- for forks+ forked_at_seq -- seq number at fork point+ extra -- JSONB+ created_at / updated_at++cubepi_messages+ thread_id, seq -- composite PK; partitioned by HASH(thread_id) into 64+ role -- "user" | "assistant" | "tool"+ metadata -- JSONB (indexed via GIN)+ payload -- bytea (msgpack)+ created_at++cubepi_schema_version+ version (PK)+```++Important properties:++- **`(thread_id, seq)` is the message identity.** `seq` is monotonic+ per thread, allocated under a `pg_advisory_xact_lock(hashtext(thread_id))`.+ Two concurrent writers on the same thread serialize cleanly.+- **`payload` is msgpack-encoded `model.model_dump(mode="json")`.**+ CubePi reconstructs the Pydantic model on read.+- **`metadata` is JSONB, queryable.** The full message also has+ `metadata` inside the payload, but the column is the canonical view+ for SQL queries.+- **Tables are partitioned by `HASH(thread_id)` into 64 partitions.**+ Even distribution across partitions, no per-thread bottleneck.++## Concurrency++The advisory lock makes append-on-the-same-thread safe across+processes:++```python+# Process A and Process B both append to thread "user-42" at the same time.+# They serialize through pg_advisory_xact_lock and each gets a consecutive seq.+```++Reads (`load`) take no lock — they're snapshot-consistent within the+transaction.++The pool default of `min=1, max=10` is fine for most apps; bump+`max_pool_size` if you have many concurrent agents.++## `save_extra` semantics++`save_extra` does a JSONB merge, not a replace:++```sql+extra = cubepi_threads.extra || EXCLUDED.extra+```++So writing `{"foo": 1}` then `{"bar": 2}` leaves `{"foo": 1, "bar":+2}`. Middleware can safely write partial dicts without losing prior+keys.++## Forks++The `parent_thread_id` + `forked_at_seq` columns exist for future fork+support. CubePi v0.3 doesn't expose a fork API yet — they're written+to so the schema is forward-compatible.++## Common pitfalls++- **`CubepiSchemaUninitialized`** — Your DB is empty or your+ migrations didn't run. Apply the host alembic upgrade first.+- **`CubepiSchemaMismatch`** — You upgraded CubePi but didn't generate+ a new migration. Generate one, apply it, and CubePi will start.++ :::info Schema v2 (HITL)++ CubePi ≥ the HITL feature bumps `EXPECTED_SCHEMA_VERSION` from 1 to 2+ and adds a `pending_request JSONB NULL` column to `cubepi_threads`.+ Your host alembic upgrade must call+ `add_pending_request_column_op()` (from+ `cubepi.checkpointer.postgres.alembic_helpers`) before bumping the+ schema_version row. See the [HITL guide](../hitl/overview) for the full+ cross-process flow.+ :::+- **Connection pool exhaustion under load** — Default `max_pool_size=10`.+ Bump it if your app's concurrent agent count is higher than that.+- **`asyncpg.exceptions.UndefinedTableError` outside `__aenter__`** —+ Means you're using the checkpointer outside of `async with`. The+ pool isn't connected yet. Wrap in the context manager.+- **Mixing host SQLAlchemy `MetaData`** — CubePi ships its own+ `MetaData` instance precisely so it can coexist with your app's+ models without colliding. Don't merge them into your global metadata+ — pass both to Alembic separately.++## See also++- [SQLite Checkpointing](./sqlite) — single-process alternative.+- [Custom Backends](./custom) — Protocol details.+- [Recipes → Postgres + FastAPI Service](../../recipes/postgres-fastapi)+ — a deployable HTTP-fronted agent.+- [Package README](https://github.com/cubeplexai/cubepi/blob/main/cubepi/checkpointer/postgres/README.md)+ — the full host-integration runbook next to the code.diff --git a/website/versioned_docs/version-0.7/guides/checkpointing/sqlite.md b/website/versioned_docs/version-0.7/guides/checkpointing/sqlite.mdnew file mode 100644index 0000000..1b65c5f--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/checkpointing/sqlite.md@@ -0,0 +1,190 @@+---+title: SQLite Checkpointing+description: "Use SQLiteCheckpointer for lightweight single-process agent state persistence in CubePi."+---++# SQLite Checkpointing++`SQLiteCheckpointer` is the lightweight persistence backend: a single+local file, no server, append-only message log. It's the default+choice for laptops, single-process apps, desktop tools, and any+environment where one Python process owns the conversation.++Install the extra:++```bash+pip install "cubepi[sqlite]"+```++This pulls in `aiosqlite`.++## Basic usage++```python+import asyncio+from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key="…")+ async with SQLiteCheckpointer("agent.db") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ checkpointer=cp,+ thread_id="user-42",+ )+ await agent.prompt("Remember: my favourite colour is teal.")++ # Restart the script later — the file persists.+ await agent.prompt("What did I say my favourite colour was?")+ # → "You said teal."+++asyncio.run(main())+```++Two things to internalise:++1. **`thread_id`** is your conversation identifier — usually a user id+ or a session id. Two agents on the same `thread_id` share the same+ history.+2. **`async with SQLiteCheckpointer(...)`** is required: the context+ manager opens the connection and runs the one-time table creation+ on `__aenter__`. Using it without the context manager will raise+ `AssertionError`.++## What gets persisted++`SQLiteCheckpointer` writes two tables on first use:++```sql+CREATE TABLE messages (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ thread_id TEXT NOT NULL,+ message_json TEXT NOT NULL,+ created_at REAL NOT NULL DEFAULT (julianday('now'))+);++CREATE TABLE thread_extra (+ thread_id TEXT PRIMARY KEY,+ extra_json TEXT NOT NULL DEFAULT '{}'+);+```++- Every `UserMessage`, `AssistantMessage`, and `ToolResultMessage`+ becomes one row in `messages`. Pydantic `model_dump()` is used for+ the JSON payload.+- The `extra` dict on `AgentContext` is persisted on `agent_end` into+ `thread_extra`. Middleware that wants thread-scoped state should+ write into `context.extra`.++This schema is append-only. CubePi never updates or deletes rows.++## HITL pending table++When the [HITL](../hitl/overview) module is in use, an additional table is+created automatically on `__aenter__`:++```sql+CREATE TABLE IF NOT EXISTS thread_pending_request (+ thread_id TEXT PRIMARY KEY,+ request_json TEXT NOT NULL,+ created_at REAL NOT NULL DEFAULT (julianday('now'))+);+```++No manual migration is needed — `CREATE TABLE IF NOT EXISTS` is+idempotent.++## When CubePi reads++On the **first** `prompt()` after instantiation, CubePi calls+`load(thread_id)`. If the thread exists, history is restored into+`agent.state.messages` and `extra` is restored into the agent's+private `_extra` dict.++Subsequent `prompt()` calls don't re-read — the in-memory state is+authoritative.++This means: **don't share one `Agent` instance across processes**.+Process A's in-memory state will diverge from Process B's writes.++## Multi-thread isolation++```python+async with SQLiteCheckpointer("agent.db") as cp:+ alice = Agent(model=…, checkpointer=cp, thread_id="alice")+ bob = Agent(model=…, checkpointer=cp, thread_id="bob")+ # Each call only loads/appends its own thread.+```++You can pool one checkpointer across many users; the `thread_id`+isolates them.++## Concurrency model++The checkpointer uses an `asyncio.Lock` around every read and write.+SQLite itself can be written from multiple processes, but CubePi's+expectation is that a single agent instance owns a thread. If you+have multiple processes writing the same `agent.db`:++- Reads are safe.+- Concurrent writes to **different** threads are safe.+- Concurrent writes to the **same** thread will interleave and the+ resulting history won't make sense — you'll see two assistant+ messages back-to-back, etc.++If you need multi-process writers on shared threads, jump to+[Postgres](./postgres) which uses an advisory lock per thread.++## Where the file lives++Use an absolute path in production:++```python+SQLiteCheckpointer("/var/lib/myapp/agent.db")+```++Relative paths resolve against `os.getcwd()` at the moment of+`__aenter__`. The directory must exist; create it ahead of time.++## Backup and inspection++The file is a normal SQLite database. You can:++```bash+# Inspect history for a thread+sqlite3 agent.db "SELECT message_json FROM messages WHERE thread_id='user-42' ORDER BY id"++# Back up+cp agent.db agent.db.bak++# Vacuum to reclaim space (optional — file grows linearly with history)+sqlite3 agent.db "VACUUM"+```++## Common pitfalls++- **Used without `async with`** — `AssertionError: self._db is not+ None`. Always wrap in `async with`.+- **Two processes writing the same thread** — Interleaved history.+ Use Postgres or coordinate at the application layer.+- **WAL mode not enabled** — CubePi uses the default journal mode for+ portability. For a single-writer, many-reader app, enable WAL once+ via `sqlite3 agent.db "PRAGMA journal_mode=WAL"` for better read+ concurrency.+- **Forgetting to set `thread_id`** — Without it, the agent has no+ persistence binding. The checkpointer is silently ignored. Always+ pass both.++## See also++- [Postgres Checkpointing](./postgres) — for multi-instance deployments.+- [Custom Backends](./custom) — implement the protocol for Redis,+ DynamoDB, etc.+- [Recipes → Persistent Chat](../../recipes/persistent-chat) —+ end-to-end app with SQLite.+- [Recipes → Resumable Long Tasks](../../recipes/resumable-tasks) —+ agents that survive a process restart mid-tool.diff --git a/website/versioned_docs/version-0.7/guides/hitl/durable.md b/website/versioned_docs/version-0.7/guides/hitl/durable.mdnew file mode 100644index 0000000..328c950--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/hitl/durable.md@@ -0,0 +1,119 @@+---+title: Cross-process & durability+sidebar_position: 3+description: "Suspend a CubePi agent on a HITL request and resume it in another process with a CheckpointedChannel; durable-scope rules."+---++# Cross-process flow & durability++## Cross-process (web service) flow++Full lifecycle of a suspend/resume cycle:++```python+import asyncio+from cubepi.agent.agent import Agent+from cubepi.checkpointer.postgres import PostgresCheckpointer+from cubepi.hitl import (+ ApproveAnswer, CheckpointedChannel, ConfirmToolCallMiddleware,+)++# ---------- Process 1: HTTP POST /chat ----------++async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")++ agent = Agent(+ model=...,+ tools=[bash_tool],+ middleware=[+ ConfirmToolCallMiddleware(channel, require_confirm={"bash"}),+ ],+ channel=channel,+ checkpointer=cp,+ thread_id="conv-42",+ )++ # prompt() blocks until the HITL request is answered OR the host+ # calls detach(). Same-process host coroutines can answer directly+ # via channel.answer(); cross-process hosts use respond().+ task = asyncio.create_task(agent.prompt("delete temp files"))++ # Poll for pending (or subscribe to channel for SSE push)+ for _ in range(1000):+ pending = channel.pending+ if pending is not None:+ break+ await asyncio.sleep(0.1)++ assert pending is not None+ # Render pending.payload to the frontend (an ApproveRequest:+ # tool_name="bash", args={"cmd":"rm /tmp/foo"}, ...)++ # Graceful suspend — persist the assistant message + unresolved+ # tool_calls, keep pending_request in DB, emit AgentSuspendedEvent.+ # The HTTP handler returns 200 { status: "awaiting_approval" }.+ await agent.detach()+ await task # prompt() unwinds with HitlDetached+++# ---------- Process 2: HTTP POST /respond ----------++async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="conv-42")++ agent = Agent(+ model=...,+ tools=[bash_tool],+ middleware=[+ ConfirmToolCallMiddleware(channel, require_confirm={"bash"}),+ ],+ channel=channel,+ checkpointer=cp,+ thread_id="conv-42",+ )++ # Loads the persisted history + pending, validates the question_id+ # matches, attaches the answer to the channel, re-enters the loop+ # where the last assistant message had unresolved tool calls.+ await agent.respond(+ question_id=request.json["call_id"],+ answer=ApproveAnswer(decision="approve"),+ )+ # The bash tool executes, the model receives the tool_result and+ # produces the next assistant turn. Conversation continues normally.+```++**If the user closes the tab without answering:**++```python+await agent.abort_pending(reason="user closed tab")+# Phase 1: signals the in-flight HITL await (if any) to raise HitlAborted.+# Phase 2: appends synthetic deny ToolResultMessage(s) for unresolved+# tool_calls, appends a terminal AssistantMessage(stop_reason="aborted"),+# clears persisted pending, emits AgentAbortedEvent.+# No model call is made. The conversation is closed.+```+++## Durable scope++Durable cross-process resume (survives process death) is supported at two+well-defined safe suspension points:++1. **`before_tool_call` approval gate** — the approval middleware calls+ `channel.approve(...)` *before* the tool's `execute()` body runs. No tool+ side effects exist yet. Resume re-enters the loop and either runs the+ (possibly edited) tool body or substitutes a synthetic deny tool_result.+2. **`ask_user` tool body** — whose entire `execute()` body is+ `return await channel.ask(...)`. Resume replays nothing because nothing+ else happened.++**Custom tools that mix HITL with other work inside `execute()` are NOT+durable cross-process by default.** If such a tool's process dies mid-execute,+anything that ran before the channel call would be lost. CubePi will raise+`HitlDurabilityNotGuaranteed` unless the `CheckpointedChannel` is constructed+with `allow_inside_custom_tool=True` — the caller must acknowledge the+idempotency contract (the tool body must be a pure HITL wait with no+preceding observable side effects).+diff --git a/website/versioned_docs/version-0.7/guides/hitl/middlewares.md b/website/versioned_docs/version-0.7/guides/hitl/middlewares.mdnew file mode 100644index 0000000..1184c41--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/hitl/middlewares.md@@ -0,0 +1,124 @@+---+title: Middlewares & ask_user+sidebar_position: 2+description: "Gate tool calls with ConfirmToolCallMiddleware / ApprovalPolicyMiddleware, and let the model ask structured questions with the ask_user tool."+---++# Middlewares & the `ask_user` tool++## Built-in middlewares++### `ConfirmToolCallMiddleware`++"Always ask the human for tool *names* in this set."++```python+from cubepi.hitl import ConfirmToolCallMiddleware++# Set-based matching — only ask for these tool names+agent = Agent(+ ...,+ middleware=[+ ConfirmToolCallMiddleware(+ channel,+ require_confirm={"bash", "write_file", "http_post"},+ timeout_seconds=180,+ ),+ ],+)+```++`require_confirm` options:++| Value | Behavior |+|---|---|+| `None` (default) | Confirm **every** tool. |+| `set[str]` | Confirm if `tool_call.name` is in the set. |+| `Callable[[BeforeToolCallContext], bool]` | Custom predicate — inspect args, context, etc. |++An optional `details_fn(ctx: BeforeToolCallContext) -> dict` enriches the+approve request payload with extra context the frontend can render (e.g.+matched rule name, impact preview, affected file list).++### `ApprovalPolicyMiddleware`++For hosts with a **policy engine** that classifies tool calls into three+tiers — auto-allow, hard-deny, or human-confirm.++```python+from cubepi.hitl import Approve, ApprovalPolicyMiddleware, AskUser, Deny++def my_policy(ctx):+ if ctx.tool_call.name == "read_file":+ return Approve() # passthrough+ if ctx.tool_call.name.startswith("dangerous_"):+ return Deny(reason="blocked by policy") # hard block, no human asked+ return AskUser(timeout_seconds=180) # human confirm++agent = Agent(+ ...,+ middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],+)+```++The policy function can be sync or async (`await`-able). It returns one of:++| Return | Effect |+|---|---|+| `Approve()` | Tool runs; channel never invoked. |+| `Deny(reason)` | Tool blocked; `hitl_trace["decision"]="policy_deny"`. |+| `AskUser(timeout_seconds=..., details=...)` | Channel invoked; human chooses approve/deny/edit. |++Policy-deny and human-deny produce different `hitl_trace` keys (`policy_deny`+vs `human_deny`) so audit and trace can distinguish them.++## `ask_user` built-in tool++A tool the **model** invokes when it needs structured input from the user.+The factory returns an `AgentTool` named `"ask_user"` with+`execution_mode="sequential"` — it can't run in parallel with other tools.++```python+from cubepi.hitl import ask_user_tool++agent = Agent(+ ...,+ tools=[bash_tool, ask_user_tool(channel)],+)+```++The tool description explicitly steers the model away from using `ask_user`+for free-form clarification ("for free-form questions, end your turn with+text — the user's next message is your answer"). The model should only+invoke it when a **structured** answer is needed.++The `Parameters` prompt schema the model sees:++| Field | Type | Description |+|---|---|---|+| `questions` | array | One or more question objects. |+| `questions[].key` | string | Field name in the answer dict. |+| `questions[].prompt` | string | The question text. |+| `questions[].options` | array (optional) | Selection options. `None` = free text. |+| `questions[].options[].label` | string | Human-facing label. |+| `questions[].options[].value` | string | Value returned to agent. |+| `questions[].options[].allow_input` | bool (default `false`) | "Other / please specify." |+| `questions[].multi_select` | bool (default `false`) | Allow multiple selections. |+| `questions[].required` | bool (default `true`) | Can the user skip this? |++Cancel and timeout are surfaced as `tool_result.is_error=True` with+`details["hitl"]["outcome"]="cancelled"` / `"timed_out"` — the model sees+a clean error tool result and can react. Other HITL control exceptions+(HitlDetached, HitlAborted) propagate to the Agent layer, not the model.+++## When to use `ask_user` vs end of turn++| Goal | Use |+|---|---|+| Free-text follow-up question to user | Just end the turn with the question as text; the user's next message is your answer. |+| Structured selection (one of N) | `ask_user` tool with `options`. |+| Multi-select ("pick any of") | `ask_user` tool with `multi_select=True`. |+| "Other" with free-text input | `ask_user` tool option with `allow_input=True`. |+| Confirm/edit tool args before run | `ConfirmToolCallMiddleware` or `ApprovalPolicyMiddleware`. |+diff --git a/website/versioned_docs/version-0.7/guides/hitl/overview.md b/website/versioned_docs/version-0.7/guides/hitl/overview.mdnew file mode 100644index 0000000..35d95c2--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/hitl/overview.md@@ -0,0 +1,171 @@+---+title: Overview+sidebar_position: 1+description: "CubePi human-in-the-loop: the channel, the three verbs (confirm, approve, ask), and timeouts."+---++# Human-in-the-Loop (HITL)++CubePi's HITL channel lets an agent **pause and ask a human** before proceeding.+It handles two recurring patterns with a single primitive:++1. **Sandbox tool confirmation** — a dangerous tool (bash, file writes, API+ mutations) needs approve / deny / edit from a human before running.+2. **Mid-run structured questions** — the agent needs a specific selection or+ form answer from the user before it can continue.++The channel is an `await`-able coroutine collaborator. Tool authors write+`await channel.ask(...)` and the channel handles the pause. Host code+(subscribers) renders the pending request and posts an answer. Two backends+cover the full spectrum:++- `InMemoryChannel` — CLI, notebooks, tests. Process dies, pending is lost.+- `CheckpointedChannel` — web services. Persists the pending request to a+ `Checkpointer` so a different process (or the same process after restart)+ can pick up and answer hours later.++## Architecture++```text+┌────────────────────────────────────────────────────────┐+│ Host (cubebox web service / CLI / TUI) │+│ │+│ subscribe to channel.pending ◄──── answer / cancel │+└──────────────────────┬─────────────────────────────────┘+ │+ ┌─────────────▼──────────────┐+ │ HitlChannel (Protocol) │+ │ confirm / approve / ask │+ │ pending / answer / cancel │+ └──────┬──────────────┬──────┘+ │ │+ ┌──────────▼───┐ ┌──────▼────────────┐+ │InMemoryChannel│ │CheckpointedChannel│+ │ (Future+Queue)│ │ (Future + persi- │+ │ │ │ sts to Checkpo- │+ │ │ │ inter) │+ └───────────────┘ └───────────────────┘+ │ │+ │ used by ────┤+ │ │+ ┌──────────▼──────────────▼────────────┐+ │ ask_user tool ConfirmToolCallMW │+ │ (structured form) (approve/deny/ │+ │ edit per-tool) │+ └──────────────────────────────────────┘+ │+ ┌─────────────▼──────────────┐+ │ cubepi Agent loop │+ │ (BeforeToolCallResult │+ │ carries hitl_trace) │+ └────────────────────────────┘+```++## Channel types++### `InMemoryChannel`++For single-process use. Holds an `asyncio.Future` internally — the host+calls `channel.answer()` from the same event loop.++```python+from cubepi.hitl import InMemoryChannel++channel = InMemoryChannel(+ default_timeout=180.0, # per-call timeout; None = no timeout (default)+)+```++### `CheckpointedChannel`++For cross-process use. On every `confirm / approve / ask`, the `HitlRequest`+is persisted via `Checkpointer.save_pending_request(thread_id, ...)`. On+`channel.answer()`, the pending is cleared. On `HitlDetached` (graceful+suspend), the pending stays so a later `Agent.respond()` can resume.++```python+from cubepi.hitl import CheckpointedChannel+from cubepi.checkpointer.sqlite import SQLiteCheckpointer # or postgres / mysql++async with SQLiteCheckpointer("path/to.db") as cp:+ channel = CheckpointedChannel(+ checkpointer=cp,+ thread_id="conversation-42",+ default_timeout=None, # cross-process: typical to disable+ allow_inside_custom_tool=False, # safety gate (see Cross-process & durability)+ )+```++`CheckpointedChannel.__init__` validates the checkpointer implements+`save_pending_request` and `load_pending_request` — it fails early with+`HitlError` if not.++## The three verbs++### `confirm(prompt, *, details, timeout, signal) → bool`++A simple yes/no question. The host answers `True` or `False`.++```python+if await channel.confirm("Deploy to production?", details={"env": "prod"}):+ await deploy()+```++### `approve(tool_name, tool_call_id, args, *, details, timeout, signal) → ApproveAnswer`++The sandbox-confirm verb. Returns an `ApproveAnswer` with one of three decisions:++| Decision | Result |+|---|---|+| `"approve"` | Tool runs with the original args. |+| `"deny"` | Tool is blocked; `tool_result.is_error=True` with `details["hitl"]["decision"]="human_deny"`. |+| `"edit"` | Tool runs with the edited args (re-validated against the tool's pydantic parameter model). |++For `approve` requests, the envelope's `question_id` is set to the LLM's+`tool_call_id` — no separate UUID, so host code can correlate by the same+ID it already tracks from the tool stream.++### `ask(questions, *, timeout, signal) → dict[str, str | list[str]]`++A structured form with one or more `Question` objects. Each question can be:++- **Free-text** (`options=None`)+- **Single-select** (`options=[...]`, `multi_select=False`)+- **Multi-select** (`options=[...]`, `multi_select=True`)+- **"Other" with input** (option has `allow_input=True` — user types free text)++```python+from cubepi.hitl.types import Question, Option++answers = await channel.ask([+ Question(key="framework", prompt="Which framework?", options=[+ Option(label="React", value="react"),+ Option(label="Vue", value="vue"),+ Option(label="Other", value="other", allow_input=True),+ ]),+ Question(key="features", prompt="Enable:", multi_select=True, options=[+ Option(label="Auth", value="auth"),+ Option(label="Payments", value="payments"),+ ]),+])+# answers == {"framework": "react", "features": ["auth", "payments"]}+```++## Timeout++Both channels accept a `default_timeout` constructor arg and every verb+accepts a per-call `timeout` kwarg (per-call overrides default).++| Layer | API |+|---|---|+| Channel constructor | `InMemoryChannel(default_timeout=30.0)` |+| Per-call override | `channel.confirm("ok?", timeout=10.0)` |+| Per-call off | `channel.approve(..., timeout=None)` when channel default is set |++Timeout expiry raises `HitlTimedOut(BaseException)` from the agent-side+`await`. The surrounding tool or middleware translates it to+`tool_result.is_error=True` with `details["hitl"]["decision"]="timed_out"`,+so the model sees a clean denial and can react naturally. The envelope's+`HitlRequest.timeout_seconds` is filled automatically so the frontend can+render a countdown.+diff --git a/website/versioned_docs/version-0.7/guides/hitl/reference.md b/website/versioned_docs/version-0.7/guides/hitl/reference.mdnew file mode 100644index 0000000..345a0fb--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/hitl/reference.md@@ -0,0 +1,132 @@+---+title: API, events & reference+sidebar_position: 4+description: "CubePi HITL reference: Agent API, events, trace spans, error reference, testing helpers, and architecture notes."+---++# API, events & reference++## Agent API++### Constructor++```python+Agent(+ ...,+ channel: HitlChannel | None = None, # bind a channel to this agent+)+```++If a channel is bound, the Agent wires its `_process_event` as the channel's+emit callback so `HitlRequestEvent` / `HitlAnswerEvent` are dispatched to all+agent listeners. A `_run_lock` (`asyncio.Lock`) serialises `prompt`,+`resume`, and `respond` calls.++### Properties++| Property | Returns | Description |+|---|---|---|+| `agent.channel` | `HitlChannel \| None` | The bound channel or `None`. |+| `agent.in_flight_hitl_request` | `HitlRequest \| None` | The channel's current pending payload. Raises `HitlError` if no channel bound. |++### Methods++| Method | Signature | Description |+|---|---|---|+| `load_pending_hitl_request()` | `async → HitlRequest \| None` | Reads the pending from the checkpointer (even post-detach). |+| `detach()` | `async → None` | Emits `AgentSuspendedEvent(pending_request=...)` then triggers `HitlDetached` on the channel future. The loop exits silently; assistant message retains unresolved tool calls; `pending_request` stays persisted. |+| `respond(*, question_id=, answer=)` | `async → None` | Resumes a suspended run. Validates qid matches persistent pending, attaches answer to channel, re-enters the loop via `run_agent_loop_resume`. |+| `abort_pending(reason=)` | `async → None` | Closes the conversation. Two-phase: Phase 1 signals in-flight await (no lock). Phase 2 appends synthetic deny tool_results + terminal `stop_reason="aborted"` assistant (under lock). |++`in_flight_hitl_request` is a synchronous property (reads the in-memory+channel slot). `load_pending_hitl_request()` is async (reads from the+checkpointer; useful post-detach or in a fresh process).++## Events++Four new events are emitted on the agent's event stream:++| Event | When | Key fields |+|---|---|---|+| `HitlRequestEvent` | Channel receives a new `confirm/approve/ask`. | `request: HitlRequest` |+| `HitlAnswerEvent` | `channel.answer()` or `channel.cancel()` fires. | `question_id: str`, `answer: Any`, `cancelled: bool`, `timed_out: bool` |+| `AgentSuspendedEvent` | `agent.detach()` called while HITL was pending. | `pending_request: HitlRequest` |+| `AgentAbortedEvent` | `agent.abort_pending()` closes the conversation. | `reason: str` |++These are all included in the `AgentEvent` union, so typed listeners+automatically cover them. `HitlRequestEvent` and `HitlAnswerEvent` are+emitted by the channel through the agent's emit binding. `AgentSuspendedEvent`+and `AgentAbortedEvent` are emitted by the Agent layer (not the loop — the+Agent has the channel handle to populate the real `pending_request` payload).++## Trace spans++When the `cubepi[tracing]` extra is installed, each HITL await is wrapped+in an OpenTelemetry span:++| Span name | Attributes |+|---|---|+| `hitl.approve` | `hitl.tool_name`, `hitl.tool_call_id`, `hitl.outcome`, `hitl.from_resume`, `hitl.duration_seconds` |+| `hitl.confirm` | `hitl.question_id`, `hitl.outcome`, `hitl.duration_seconds` |+| `hitl.ask` | `hitl.question_id`, `hitl.outcome`, `hitl.duration_seconds` |++`hitl.outcome` is one of: `approved`, `denied`, `edited`, `answered`,+`cancelled`, `timed_out`, `aborted`, `detached`.++The tracing import is lazy — if `opentelemetry` is not installed, the+channel silently falls back to a no-op span.++## Error reference++| Exception | Base class | Meaning |+|---|---|---|+| `HitlCancelled(reason)` | `BaseException` | Host called `channel.cancel(qid)`. |+| `HitlTimedOut(seconds)` | `BaseException` | Per-call or channel-default timeout expired. |+| `HitlDetached` | `BaseException` | `agent.detach()` called during HITL await. |+| `HitlAborted` | `BaseException` | `agent.abort_pending()` signalled the agent. |+| `HitlConcurrencyError` | `Exception` | `confirm/approve/ask` called while channel already has a pending request. |+| `HitlStaleAnswer` | `Exception` | `channel.answer(qid)` with a `question_id` that doesn't match the current pending. |+| `HitlNoPendingRequest` | `Exception` | `agent.respond(…)` called but no `pending_request` on the thread. |+| `HitlDurabilityNotGuaranteed` | `Exception` | Custom tool called `CheckpointedChannel.ask()` without `allow_inside_custom_tool=True`. |++`HitlControlException` (the parent of the four `BaseException` subclasses)+is intentionally NOT caught by the existing broad `except Exception:` handlers+in `cubepi.agent.tools._prepare_tool_call` and `_execute_prepared` — this+mirrors `asyncio.CancelledError`'s pattern.+++## Testing helpers++```python+from cubepi.hitl.testing import ScriptedChannel, NoopChannel++# ScriptedChannel: pre-programmed answers, consumed in order.+ch = ScriptedChannel(answers=[+ ApproveAnswer(decision="approve"),+ {"color": "red"}, # ask answer+ lambda req: ApproveAnswer(decision="deny", reason="test") # callable+])+assert len(ch.history) == 3 # all HitlRequests ever seen++# NoopChannel: auto-approves everything. Useful for subagents.+ch = NoopChannel()+assert (await ch.approve("bash", "tc", {})).decision == "approve"+assert (await ch.confirm("?")) is True+assert await ch.ask([Question(key="k", prompt="p")]) == {"k": ""}+```++## Architecture notes++- **Single pending per thread.** The agent loop is sequential — at most one+ HITL request is outstanding per `thread_id`. Concurrent `confirm/approve/ask`+ raises `HitlConcurrencyError`.+- **Prompt-cache prefix invariant.** Between pause and resume, the messages+ list changes only by appending tool-result messages and the next assistant+ turn at the tail. No inserting, reordering, or mutating prior messages —+ that would invalidate the provider-side prompt cache.+- **`question_id == tool_call_id` for approve requests.** No aliasing or+ mapping needed — hosts that already track `call_id` from the tool stream+ pass it directly.+- **Resume does not replay.** It re-enters the loop with the answer pre-loaded+ into the channel. The last assistant message's unresolved tool calls dictate+ what executes next. No node-based replay semantics.diff --git a/website/versioned_docs/version-0.7/guides/mcp/auth.md b/website/versioned_docs/version-0.7/guides/mcp/auth.mdnew file mode 100644index 0000000..b67f8e5--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/mcp/auth.md@@ -0,0 +1,213 @@+---+title: MCP Server Authentication+description: "Configure authentication — API keys, OAuth, and custom headers — for MCP servers in CubePi."+---++# MCP Server Authentication++The MCP transport layer doesn't dictate an auth scheme — servers+decide. In practice, three patterns cover almost every case:++1. **Bearer token in `Authorization` header** (HTTP transport).+2. **Arbitrary custom headers** (HTTP transport).+3. **Environment variables** (stdio transport — server reads them+ from its own process env).++This page walks through each.++## HTTP: bearer tokens++The dominant pattern for hosted MCP servers (GitHub, Sentry,+internal). Pass an `Authorization` header to `load_mcp_tools_http`:++```python+import os+from cubepi.mcp import load_mcp_tools_http++tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": f"Bearer {os.environ['MCP_TOKEN']}"},+)+```++`headers` is forwarded to *every* request the transport makes,+including subsequent `tools/call` invocations. There's no separate+"login then call" step — the token rides every connection.++## HTTP: custom headers++Some servers use API-key headers instead:++```python+tools = await load_mcp_tools_http(+ server_url="https://mcp.internal/sse",+ headers={+ "X-API-Key": os.environ["MCP_API_KEY"],+ "X-Tenant-Id": "acme-corp",+ },+)+```++Combine as needed:++```python+headers = {+ "Authorization": f"Bearer {token}",+ "X-Trace-Id": str(uuid.uuid4()),+}+```++## HTTP: short-lived tokens / refresh++CubePi's loaders take a static `headers` dict at load time. For tokens+that expire (OAuth, JWT with short TTL), you have two options:++### Option A — Re-load on expiry++Catch the error, re-fetch the token, re-load the tools:++```python+async def load_with_fresh_token():+ token = await fetch_token()+ return await load_mcp_tools_http(+ server_url="…",+ headers={"Authorization": f"Bearer {token}"},+ )++tools = await load_with_fresh_token()+```++Practical for tokens that live longer than a single agent run.++### Option B — Wrap and refresh inside the tool++Build the `AgentTool` yourself with a closure that knows how to+refresh:++```python+from cubepi.mcp._adapter import make_mcp_agent_tool+from cubepi.mcp import load_mcp_tools_http++async def call_remote_with_refresh(tool_name, args):+ headers = {"Authorization": f"Bearer {await fetch_token()}"}+ # Re-implement the http_loader's call_remote with fresh headers each time+ from mcp.client.sse import sse_client+ from mcp import ClientSession+ async with sse_client(server_url, headers=headers, timeout=30) as streams:+ async with ClientSession(*streams) as session:+ await session.initialize()+ resp = await session.call_tool(tool_name, args)+ return _serialize_call_tool_response(resp)++# Use the adapter directly:+my_tool = make_mcp_agent_tool(+ name="…",+ description="…",+ input_schema={…},+ call_remote=call_remote_with_refresh,+)+```++Only worth it for tokens with very short TTLs. For anything longer,+Option A is simpler.++## stdio: environment variables++stdio servers read credentials from their own process environment.+Pass an `env` dict:++```python+import os+from cubepi.mcp import load_mcp_tools_stdio++tools = await load_mcp_tools_stdio(+ command="npx",+ args=["-y", "@modelcontextprotocol/server-github"],+ env={+ "GITHUB_PERSONAL_ACCESS_TOKEN": os.environ["GH_TOKEN"],+ **os.environ, # inherit the rest+ },+)+```++If you omit `env`, the subprocess inherits **all** parent env vars+(standard subprocess behaviour). For a clean slate, pass an explicit+dict:++```python+env = {+ "PATH": os.environ["PATH"],+ "HOME": os.environ["HOME"],+ "GITHUB_PERSONAL_ACCESS_TOKEN": token,+}+```++## stdio: file-based credentials++When a server reads credentials from `~/.config/...`, pass `cwd` to+ensure consistent path resolution, and rely on the inherited+environment:++```python+tools = await load_mcp_tools_stdio(+ command="/usr/local/bin/my-mcp",+ args=["--config", "config.yaml"],+ cwd="/etc/myapp",+)+```++## Per-user / per-tenant credentials++In a multi-tenant service, each agent invocation needs different+credentials. **Load tools per-request**:++```python+async def build_agent_for_user(user_id: str) -> Agent:+ token = await fetch_user_token(user_id)+ tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": f"Bearer {token}"},+ )+ return Agent(model=model, tools=tools)+```++Don't cache a single `tools` list across users — the closures retain+the auth headers from load time.++## Auditing / observability++Log MCP calls inside a [`before_tool_call`](../middleware/hooks#before_tool_call)+middleware. Tools loaded from MCP look identical to hand-written+tools in event streams, so existing logging middleware Just Works.++To tag MCP tools specifically, check for the synthesised parameter+model's name (prefix `MCP_`):++```python+class MCPAuditMiddleware(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ param_name = type(ctx.args).__name__+ if param_name.startswith("MCP_"):+ log.info("mcp_call", extra={"tool": ctx.tool_call.name})+ return None+```++## Common pitfalls++- **`401 Unauthorized` only on call, not on `list_tools`** — Some+ servers gate per-tool. Ensure the token has scopes for every tool+ you intend the agent to use.+- **Tokens leaking into logs** — Don't log the `headers` dict.+ Especially watch out for exception messages that include URLs with+ query-string credentials.+- **stdio server fails silently** — Server prints auth errors to its+ own stderr. Add `stdout`/`stderr` redirection or use the MCP SDK's+ diagnostic logging.+- **Re-loading every request is slow** — Cache the loaded tools per+ `(user_id, token)` pair. Just remember tokens have TTLs.++## See also++- [Loading MCP Tools](./loading) — the basic loader API.+- [Middleware → Examples → Logging](../middleware/examples#structured-logging)+ — pairs naturally with MCP audit needs.diff --git a/website/versioned_docs/version-0.7/guides/mcp/loading.md b/website/versioned_docs/version-0.7/guides/mcp/loading.mdnew file mode 100644index 0000000..1c0ccd3--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/mcp/loading.md@@ -0,0 +1,168 @@+---+title: Loading MCP Tools+description: "Load tools from MCP servers into your CubePi agent, including SSE-based remote servers."+---++# Loading MCP Tools++The [Model Context Protocol](https://modelcontextprotocol.io) defines+a standard way for tool servers to expose capabilities to agents.+CubePi ships two loaders that connect to an MCP server, enumerate its+tools, and turn each one into a regular `AgentTool` you can hand to+`Agent(tools=…)`.++Install the extra:++```bash+pip install "cubepi[mcp]"+```++This pulls in the `mcp` SDK.++## stdio transport: local subprocess++For tools that run as a local process (e.g. npm-published servers, a+Python module, an internal CLI):++```python+import asyncio+import sys+from cubepi import Agent, Model+from cubepi.mcp import load_mcp_tools_stdio+from cubepi.providers.anthropic import AnthropicProvider+++async def main():+ # Spawn a stdio MCP server and discover its tools.+ tools = await load_mcp_tools_stdio(+ command="npx",+ args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp/sandbox"],+ timeout=30.0,+ )++ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ tools=tools, # all server tools, ready to use+ )+ agent.subscribe(lambda e, s=None: None)+ await agent.prompt("List files in /tmp/sandbox.")+++asyncio.run(main())+```++`load_mcp_tools_stdio` connects, calls `list_tools`, disconnects, and+returns the `AgentTool` list. **Each tool call spawns a fresh+subprocess** — v0.3 keeps things simple, no process pool.++Arguments:++| Parameter | Purpose |+|---|---|+| `command` | The executable (e.g. `"npx"`, `sys.executable`, `/usr/bin/uvx`) |+| `args` | argv for the server |+| `env` | Environment variables (optional) |+| `cwd` | Working directory (optional) |+| `timeout` | Per-call wall-clock timeout for `initialize` / `list` / `call` |++## HTTP/SSE transport: remote server++For hosted MCP servers (Sentry, GitHub, internal services):++```python+from cubepi.mcp import load_mcp_tools_http++tools = await load_mcp_tools_http(+ server_url="https://mcp.example.com/sse",+ headers={"Authorization": "Bearer <token>"},+ timeout=30.0,+)+```++`load_mcp_tools_http` uses the MCP SDK's SSE client. Same model as+stdio: one connection to enumerate, then a fresh connection per tool+call.++See [MCP Auth](./auth) for the auth patterns you'll likely need with+HTTP servers.++## What you get back++Each entry in the returned list is an `AgentTool`:++- `name` — the MCP tool name.+- `description` — straight from the server (no rewrites).+- `parameters` — a Pydantic model synthesised from the MCP+ `inputSchema` (JSON Schema → Pydantic via `pydantic.create_model`).+- `execute` — closure that calls `tools/call` over the same transport.++The synthesised Pydantic model covers: `string`, `integer`, `number`,+`boolean`, `array`, `object` (as `dict[str, Any]`), and enum (via+`Literal`). Top-level constraints are preserved: `description`,+`pattern`, `minLength`/`maxLength`, `minimum`/`maximum` (incl.+exclusive variants), `minItems`/`maxItems`.++## Mixing MCP tools with hand-written tools++They're the same type. Just concatenate the lists:++```python+mcp_tools = await load_mcp_tools_stdio(command="…", args=[…])+my_tools = [weather_tool, search_tool]++agent = Agent(+ model=model,+ tools=my_tools + mcp_tools,+)+```++The model sees one combined JSON Schema; the loop dispatches each+call to the right implementation.++## Per-call vs reusable connections++CubePi opens a new transport per `execute` call. That's:++- ✅ Simple — no pool lifecycle to manage.+- ✅ Robust — a hung connection can't poison other tools.+- ⚠️ Slower for stdio servers with heavy startup (a `npx` server can+ add seconds per call).++For high-throughput stdio servers, run the server as a persistent+HTTP service instead and use `load_mcp_tools_http`.++## Image and structured content++If an MCP tool returns image content blocks, CubePi maps them to+`ImageContent` and includes them in the `AgentToolResult.content`.+Anthropic provider relays these as image blocks in tool results;+OpenAI providers currently strip them (the wire format doesn't+support image-bearing tool results).++If the server returns `structuredContent`, it's exposed under+`AgentToolResult.details["structuredContent"]` — useful for+downstream programmatic access, but not shown to the model.++## Common pitfalls++- **`asyncio.TimeoutError` immediately on first call** — Server didn't+ finish `initialize` within `timeout`. Bump `timeout=60` or higher;+ some servers do heavy setup.+- **Each tool call is slow** — stdio subprocess spawn overhead. Run+ the server as HTTP, or write a custom adapter that keeps the+ subprocess alive.+- **Tools missing from the list** — Server failed to advertise them.+ Run the server in isolation and call `list_tools` manually with the+ MCP CLI to inspect.+- **Pydantic validation rejects model output** — The MCP `inputSchema`+ has constraints the model isn't honouring. Either loosen the schema+ on the server side or add a `before_tool_call` middleware that+ coerces.++## See also++- [MCP Auth](./auth) — bearer tokens, headers, env-based credentials.+- [Tool Use](../agents/tool-use) — how tools (MCP or otherwise) are+ dispatched.+- [`make_mcp_agent_tool` source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/mcp/_adapter.py)+ — the schema → Pydantic adapter, if you need to customise.diff --git a/website/versioned_docs/version-0.7/guides/middleware/compaction.md b/website/versioned_docs/version-0.7/guides/middleware/compaction.mdnew file mode 100644index 0000000..5c96073--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/middleware/compaction.md@@ -0,0 +1,83 @@+---+title: Compaction+description: "Use CompactionMiddleware to summarize older turns while preserving full CubePi history."+---++# Compaction++`CompactionMiddleware` keeps long conversations inside a model's context window+without deleting agent history. It summarizes older turns into `ctx.extra`, then+sends the model a compressed view: one summary message plus the most recent+messages. `agent.state.messages` and checkpointer history stay complete.++## Basic setup++Use a cheaper model for the summary pass and your normal model for the agent:++```python+from cubepi import Agent+from cubepi.middleware import CompactionMiddleware++model = main_provider.model("claude-sonnet-4-5-20250929")+summary_model = cheap_provider.model("claude-haiku-4-5")++agent = Agent(+ model=model,+ checkpointer=checkpointer,+ thread_id="conv_123",+ middleware=[+ CompactionMiddleware(+ summary_model=summary_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+ ),+ ],+)+```++The summary call uses `Provider.generate(...)` with `temperature=0.0`,+`thinking="off"`, and `max_output_tokens=max_summary_tokens`.++## What gets persisted++The middleware writes two keys into `AgentContext.extra`:++- `compaction` — the summary state and the message refs it covers.+- `compaction_until_msg_index` — the history boundary summarized so far.++When a checkpointer is attached, CubePi saves `ctx.extra` at `agent_end`, so the+next process can resume with the existing summary. If the message refs no longer+match the current history, the middleware clears the stale state and starts over+rather than sending an invalid summary.++## Choosing thresholds++Start with conservative values:++```python+CompactionMiddleware(+ summary_model=cheap_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ max_summary_tokens=1024,+)+```++Raise `max_tokens_before_compact` if your model has a large context window and+you want fewer summary calls. Raise `keep_recent_messages` when recent tool+outputs or user corrections are especially important. Increase+`max_summary_tokens` for long-running research or coding sessions where the+summary needs more detail.++## Failure behavior++If the summary provider fails, CubePi logs a warning and continues with the+previous compressed view or the original messages. The agent run is not failed+only because compaction could not refresh.++## When not to use it++Skip compaction for short tasks, stateless agents, or workflows where every+token of old tool output must be visible to the model. In those cases a simple+sliding-window `transform_context` hook can be easier to reason about.diff --git a/website/versioned_docs/version-0.7/guides/middleware/composition.md b/website/versioned_docs/version-0.7/guides/middleware/composition.mdnew file mode 100644index 0000000..4b669b4--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/middleware/composition.md@@ -0,0 +1,185 @@+---+title: Composition Rules+description: "Learn how CubePi composes multiple middlewares with per-hook rules: chain, last-wins, first-block-stops, and more."+---++# Composition Rules++When you pass multiple middlewares — `Agent(middleware=[m1, m2,+m3])` — CubePi composes them according to **per-hook rules** that+differ on purpose. The right way to think about it is: each hook has+the composition rule that makes sense for its job, and you don't have+to remember "before" or "after" precedence guesses.++## The rules at a glance++| Hook | Rule | Order matters? |+|---|---|---|+| `transform_context` | **Chain** — each sees previous output | Yes |+| `convert_to_llm` | **Last wins** | Only the last one runs |+| `transform_system_prompt` | **Chain** | Yes |+| `before_tool_call` | **First block stops**; non-block accumulates | Block wins; `edited_args` last-writer-wins; `hitl_trace` merges |+| `after_tool_call` | **Later overrides earlier** | Last write wins |+| `should_stop_after_turn` | **Any `True` stops** (OR) | No |+| `after_model_response` | **Chain with merge semantics** | See below |+| `on_run_end` | **Messages concatenate** — non-empty triggers one extra turn | No |++## `transform_context` and `transform_system_prompt`++Chain: `m1`'s output becomes `m2`'s input becomes `m3`'s input. Useful+for layered transforms:++```python+agent = Agent(+ middleware=[+ SlidingWindow(max_messages=20), # m1: drop oldest+ InjectSummary(), # m2: prepend a summary block+ ],+)+```++`m2` sees the truncated list. The user-visible+`agent.state.messages` is untouched — middleware only changes what+the model receives.++## `convert_to_llm`++Last-wins on purpose: this is the final transform before wire+serialisation. Multiple owners would fight; pick one. CubePi enforces+that the **last** middleware in the list that implements+`convert_to_llm` is the one that runs.++If you find yourself needing two `convert_to_llm` middlewares,+collapse them into one (call site composition: write one that calls+both).++## `before_tool_call`++First `block=True` short-circuits the rest. **Non-block returns+accumulate:** `edited_args` propagates downstream (each middleware sees+the edited form from the one above), and `hitl_trace` merges across the+chain (with older keys archived under `_chain` when overwritten).++Use to chain policy layers from most-restrictive to least:++```python+agent = Agent(+ middleware=[+ RateLimiter(), # blocks on rate quota+ SafetyFilter(), # blocks on dangerous args; may edit+ AuditLogger(), # never blocks; records for observability+ ],+)+```++If `RateLimiter` returns `block=True`, `SafetyFilter` and+`AuditLogger`'s `before_tool_call` don't run. If `SafetyFilter` returns+`edited_args={"cmd": "rm /tmp/foo"}`, the *tool* runs with the edited+args and `AuditLogger` sees them via the rebuilt `ctx.args`.+`AuditLogger.after_tool_call` still fires because that's a different+hook.++## `after_tool_call`++Each middleware can return an `AfterToolCallResult` with some fields+set; CubePi merges them, with later results overriding earlier ones+for any field that's not `None`. The full result:++```python+class AfterToolCallResult(BaseModel):+ content: list[Content] | None = None+ details: Any = None+ is_error: bool | None = None+ terminate: bool | None = None+```++Pattern: an early middleware adds rich `details`, a later one+sanitises `content` for the model. Both run; the merged result+combines `details` from one with the redacted `content` from the+other.++## `should_stop_after_turn`++Any middleware returning `True` ends the run. The rest of the chain+isn't evaluated.++```python+agent = Agent(+ middleware=[+ MaxTurns(10),+ BudgetCap(usd=0.5),+ FinalAnswerSentinel(), # stops when assistant says "FINAL ANSWER"+ ],+)+```++## `after_model_response`++Chain with structured merge. Each middleware sees the **current+response** (which may have been replaced by an earlier middleware) and+returns a `TurnAction`:++- `response: AssistantMessage | None` — if non-None, replaces the+ current response for downstream middlewares and for what the loop+ ultimately persists.+- `inject_messages: list[Message]` — appended into a single list+ across the whole chain, then added to context before the next turn.+- `decision: "natural" | "stop" | "loop_to_model"` — the **last+ middleware's value wins**.++```python+agent = Agent(+ middleware=[+ ProfanityRedactor(), # rewrites response+ StructuredOutputValidator(), # may decide="loop_to_model"+ EventLogger(), # decision unchanged+ ],+)+```++If `StructuredOutputValidator` returns `decision="loop_to_model"` and+`EventLogger` returns `decision="natural"`, the loop sees `"natural"`+— because last wins. Reorder if that's not what you wanted.++## Mixing middleware with constructor callables++`Agent(...)` also accepts explicit hook callables (`convert_to_llm=…`,+`before_tool_call=…`, etc.). When both are present, the **explicit+callable wins**:++```python+agent = Agent(+ middleware=[LoggingMiddleware()],+ before_tool_call=my_explicit_hook, # overrides the middleware version+)+```++Use the explicit form for one-off hooks; use middleware classes when+behaviour is a coherent bundle.++## A note on `Middleware` base class++The base `Middleware` class's unimplemented methods raise+`NotImplementedError`. `compose_middleware` detects this by comparing+to the base method and **only** wires hooks the middleware actually+overrides. You don't need to `pass`-implement every method.++```python+class JustTransform(Middleware):+ async def transform_context(self, messages, *, ctx, signal=None):+ return messages[-10:]+ # No other hooks. CubePi won't call them.+```++## `on_run_end`++All middleware returning non-empty lists contribute; their messages are+concatenated into a single list and injected before the extra model+turn. Middleware returning `None` or `[]` are skipped. Because all+contribute, order doesn't affect whether messages are injected — only+their relative order within the injected list.++## See also++- [The 8 Hooks](./hooks) — what each hook does and when it fires.+- [Examples](./examples) — composition in practice.diff --git a/website/versioned_docs/version-0.7/guides/middleware/examples.md b/website/versioned_docs/version-0.7/guides/middleware/examples.mdnew file mode 100644index 0000000..f8391f6--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/middleware/examples.md@@ -0,0 +1,360 @@+---+title: Examples+description: "Working middleware examples for CubePi: rate limiting, retries with backoff, structured logging, context truncation, and HITL."+---++# Middleware Examples++Working middleware for the four most common needs: rate limiting,+retries, structured logging, and context truncation.++## Rate limiting++Block tool calls when a user exceeds quota. Combine+`before_tool_call` with an external rate-limiter (a token bucket, a+Redis INCR, …).++```python+import time+from cubepi import Middleware+from cubepi.agent.types import BeforeToolCallResult+++class RateLimitMiddleware(Middleware):+ def __init__(self, max_calls_per_min: int) -> None:+ self.max = max_calls_per_min+ self._timestamps: list[float] = []++ async def before_tool_call(self, ctx, *, signal=None):+ now = time.monotonic()+ # Drop entries older than 60 s.+ self._timestamps = [t for t in self._timestamps if now - t < 60]+ if len(self._timestamps) >= self.max:+ return BeforeToolCallResult(+ block=True,+ reason=f"Rate limit: {self.max} tool calls/min exceeded. Try again shortly.",+ )+ self._timestamps.append(now)+ return None+```++Use:++```python+agent = Agent(model=…, middleware=[RateLimitMiddleware(max_calls_per_min=30)])+```++When the limit hits, the model sees a tool result that says "Rate+limit exceeded…" and usually waits or asks the user.++## Retries with backoff++Retry failed tool calls inside `after_tool_call`. Up to N times, with+exponential backoff, only for transient errors.++```python+import asyncio+from cubepi import Middleware+from cubepi.agent.types import AfterToolCallResult+++class RetryMiddleware(Middleware):+ def __init__(self, max_retries: int = 3, base_delay: float = 0.5) -> None:+ self.max_retries = max_retries+ self.base_delay = base_delay++ async def after_tool_call(self, ctx, *, signal=None):+ if not ctx.is_error:+ return None++ # Find the tool by name and re-execute up to max_retries times.+ tool = next(+ (t for t in (ctx.context.tools or []) if t.name == ctx.tool_call.name),+ None,+ )+ if tool is None:+ return None++ for attempt in range(1, self.max_retries + 1):+ await asyncio.sleep(self.base_delay * (2 ** (attempt - 1)))+ try:+ new_result = await tool.execute(+ ctx.tool_call.id,+ ctx.args,+ signal=signal,+ on_update=None,+ )+ return AfterToolCallResult(+ content=new_result.content,+ details={"retried": attempt, "original_error": ctx.result.content},+ is_error=False,+ )+ except Exception:+ continue++ return None # give up — original error stays+```++Combine with caution: retrying non-idempotent tools (writes, sends,+deletes) can cause real damage. Mark such tools `execution_mode="sequential"`+and skip them here based on `ctx.tool_call.name`.++## Structured logging++Log every tool call with its arguments, duration, and outcome.+Pairs `before_tool_call` (to record start time) with `after_tool_call`+(to record the result). Stash the start time in `ctx.context.extra`.++```python+import time, logging+from cubepi import Middleware++log = logging.getLogger("cubepi.tools")+++class ToolLoggingMiddleware(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ ctx.context.extra.setdefault("_tool_starts", {})[ctx.tool_call.id] = time.monotonic()+ return None++ async def after_tool_call(self, ctx, *, signal=None):+ started = ctx.context.extra.get("_tool_starts", {}).pop(ctx.tool_call.id, None)+ duration_ms = int((time.monotonic() - started) * 1000) if started else None+ log.info(+ "tool_call",+ extra={+ "tool_name": ctx.tool_call.name,+ "args": ctx.args.model_dump() if hasattr(ctx.args, "model_dump") else ctx.args,+ "is_error": ctx.is_error,+ "duration_ms": duration_ms,+ },+ )+ return None+```++`ctx.context.extra` is the right place to stash per-run state because+it's:++- Visible to other middleware via the same `ctx.context`.+- Persisted by checkpointers via `save_extra` at `agent_end`.+- Reset when a new conversation starts (a new `thread_id`).++## Sliding-window truncation++Keep the model's context bounded by retaining only the most recent N+messages, plus the system prompt:++```python+from cubepi import Middleware+++class SlidingWindow(Middleware):+ def __init__(self, max_messages: int = 20) -> None:+ self.max_messages = max_messages++ async def transform_context(self, messages, *, ctx, signal=None):+ if len(messages) <= self.max_messages:+ return messages+ return messages[-self.max_messages:]+```++`transform_context` doesn't touch `agent.state.messages` — the user+keeps seeing the full history. The model just sees the last N.++Pairs well with a `transform_system_prompt` that injects a summary of+what was dropped:++```python+class SummaryInjector(Middleware):+ async def transform_system_prompt(self, system_prompt, *, ctx, signal=None):+ summary = "Earlier in this conversation we discussed: …"+ return f"{system_prompt}\n\nContext: {summary}".strip()+```++## Built-in compaction++`CompactionMiddleware` summarizes older turns into `ctx.extra` and+passes the model a compressed view: one summary message plus recent+messages. The full conversation history remains in `agent.state`.++```python+from cubepi.middleware import CompactionMiddleware++main_model = main_provider.model("claude-sonnet-4-5-20250929")+summary_model = cheap_provider.model("claude-haiku-4-5")++agent = Agent(+ model=main_model,+ middleware=[+ CompactionMiddleware(+ summary_model=summary_model,+ max_tokens_before_compact=80_000,+ keep_recent_messages=8,+ ),+ ],+)+```++The summary call uses `Provider.generate(...)` with+`temperature=0.0`, `thinking="off"`, and `max_output_tokens` set from+`max_summary_tokens`.++## Built-in subagents++`SubagentMiddleware` adds one `subagent` tool that runs a temporary+child `Agent` with a self-contained prompt. CubePi captures child+events and returns the child agent's final assistant text as the tool+result.++```python+from cubepi.middleware import SubagentMiddleware, SubagentSpec++subagents = {+ "researcher": SubagentSpec(+ name="researcher",+ description="Researches a narrow question",+ system_prompt="You are a concise research assistant.",+ )+}++agent = Agent(+ model=model,+ middleware=[+ SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ shared_tools=[web_search],+ ),+ ],+)+```++Host applications can pass `event_mapper` and `event_handler` to map+child `AgentEvent`s into their own UI stream or audit log. Billing,+SSE payload shape, and product-specific tool filtering stay in the+host application.++## Max turns / budget cap++Hard-stop the agent after a maximum number of turns or a cost cap:++```python+class MaxTurns(Middleware):+ def __init__(self, max_turns: int) -> None:+ self.max_turns = max_turns+ self.turns = 0++ async def should_stop_after_turn(self, ctx):+ self.turns += 1+ return self.turns >= self.max_turns+++class BudgetCap(Middleware):+ def __init__(self, usd: float, model_cost) -> None:+ self.cap = usd+ self.cost = model_cost # cubepi.providers.ModelCost or similar+ self.spent = 0.0++ async def should_stop_after_turn(self, ctx):+ m = ctx.message+ if m.usage:+ self.spent += (+ (m.usage.input_tokens / 1_000_000) * self.cost.input+ + (m.usage.output_tokens / 1_000_000) * self.cost.output+ )+ return self.spent >= self.cap+```++## Structured output with `after_model_response`++Validate JSON output and re-prompt if it doesn't parse:++```python+import json+from cubepi import Middleware+from cubepi.middleware.base import TurnAction+from cubepi.providers.base import TextContent, UserMessage+++class JSONOutputValidator(Middleware):+ def __init__(self, schema_cls) -> None:+ self.schema = schema_cls++ async def after_model_response(self, response, ctx, *, signal=None):+ text = "".join(+ c.text for c in response.content if isinstance(c, TextContent)+ )+ try:+ obj = json.loads(text)+ self.schema.model_validate(obj)+ return None # valid — proceed naturally+ except Exception as e:+ return TurnAction(+ inject_messages=[+ UserMessage(content=[TextContent(text=f"Invalid output: {e}. Return valid JSON.")]),+ ],+ decision="loop_to_model",+ )+```++The agent will skip tool execution and immediately re-prompt the+model with the feedback message in context.++## Human-in-the-loop tool confirmation++CubePi ships two built-in HITL middlewares in `cubepi.hitl`:++**`ConfirmToolCallMiddleware`** — "always ask the human for this tool":++```python+from cubepi.hitl import ConfirmToolCallMiddleware, InMemoryChannel++channel = InMemoryChannel()+agent = Agent(+ model=…,+ middleware=[+ ConfirmToolCallMiddleware(+ channel,+ require_confirm={"bash", "write_file"},+ ),+ ],+)+```++The agent pauses on every `bash` or `write_file` call and waits for the+host to `channel.answer(qid, ApproveAnswer(decision="approve"))`. The+result drives the tool: `approve` runs it, `deny` blocks with a reason,+`edit` re-validates and runs the edited args.++**`ApprovalPolicyMiddleware`** — for hosts that classify tool calls via+a policy engine:++```python+from cubepi.hitl import Approve, ApprovalPolicyMiddleware, AskUser, Deny++def my_policy(ctx):+ if ctx.tool_call.name in ("read_file", "grep"):+ return Approve()+ if ctx.tool_call.name.startswith("dangerous_"):+ return Deny(reason="blocked")+ return AskUser(timeout_seconds=180)++agent = Agent(+ model=…,+ middleware=[ApprovalPolicyMiddleware(channel, policy=my_policy)],+)+```++`Deny` skips the channel entirely (hard block). `AskUser` triggers the+channel's approve flow. `Approve` returns immediately.++Full details — timeout semantics, edit semantics, events, trace spans,+cross-process suspend/resume — are in the [HITL guide](../hitl/overview).++## See also++- [The 7 Hooks](./hooks) — exact semantics of each hook.+- [Composition Rules](./composition) — how multiple middlewares+ combine.+- [Recipes](../../recipes/weather-agent) — middleware composed into+ real-world apps.diff --git a/website/versioned_docs/version-0.7/guides/middleware/hooks.md b/website/versioned_docs/version-0.7/guides/middleware/hooks.mdnew file mode 100644index 0000000..125840b--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/middleware/hooks.md@@ -0,0 +1,263 @@+---+title: The 8 Hooks+description: "Reference for the 8 middleware hooks in CubePi — transform_context, convert_to_llm, before_tool_call, after_tool_call, on_run_end, and more."+---++# The 8 Hooks++`Middleware` is a class with up to eight optional async methods. Each+hook fires at a precise point in the agent loop. Implement only the+ones you need — CubePi only wires in the ones you override.++```python+from cubepi import Middleware++class MyMiddleware(Middleware):+ async def transform_context(self, messages, *, ctx, signal=None):+ ...+```++Pass instances to `Agent(middleware=[MyMiddleware(), …])`.++## `transform_context`++```python+async def transform_context(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+ signal=None,+) -> list[Message]:+ ...+```++Fires **before each model call**, on the full message list. `ctx` is+the current `AgentContext`; use `ctx.extra` for per-run or+checkpointer-persisted middleware state. Use to:++- Truncate or summarise to fit context windows.+- Inject system reminders (better: use `transform_system_prompt`).+- Add or remove specific messages dynamically.++Return the (possibly new) list. **Don't mutate the input** — return a+new list so other code that holds the original isn't surprised.++Composition: chained — each middleware sees the previous one's+output.++## `convert_to_llm`++```python+async def convert_to_llm(+ self,+ messages: list[Message],+ *,+ ctx: AgentContext,+) -> list[Message]:+ ...+```++Fires **right before serialisation** to the provider. This is the last+chance to reshape what the LLM sees. Use for:++- Stripping tool results to text-only.+- Replacing image content with text descriptions for non-multimodal+ providers.+- Compacting long tool outputs.++Composition: **last implementation wins** (not chained). Use this when+multiple middlewares would conflict and you want a single owner.++## `transform_system_prompt`++```python+async def transform_system_prompt(+ self,+ system_prompt: str,+ *,+ ctx: AgentContext,+ signal=None,+) -> str:+ ...+```++Fires **before each model call**, on the system prompt string. Use to:++- Inject runtime info (current time, user role).+- Compose modular system-prompt fragments.+- A/B test prompt variants.++Composition: chained.++## `before_tool_call`++```python+async def before_tool_call(self, ctx: BeforeToolCallContext, *, signal=None) -> BeforeToolCallResult | None:+ ...+```++Fires **per tool call**, after argument validation, before+`tool.execute`. The context provides:++- `ctx.assistant_message` — the message that initiated the call.+- `ctx.tool_call` — the `ToolCall` block.+- `ctx.args` — the *validated* Pydantic instance.+- `ctx.context` — the full `AgentContext`.++Return `BeforeToolCallResult(block=True, reason="…")` to short-circuit+— CubePi feeds the reason back as the tool result with+`is_error=True`. Return `None` (or no return) to proceed.++Use for: permissions, rate limiting, dry-run modes, sandboxing,+human-in-the-loop confirmation (see [HITL guide](../hitl/overview)).++Composition: **first `block=True` short-circuits** the chain.++## `after_tool_call`++```python+async def after_tool_call(self, ctx: AfterToolCallContext, *, signal=None) -> AfterToolCallResult | None:+ ...+```++Fires **per tool call**, after `tool.execute` returns (or raises).+The context adds:++- `ctx.result` — the `AgentToolResult` from execute.+- `ctx.is_error` — whether the tool errored.++Return `AfterToolCallResult(content=…, details=…, is_error=…, terminate=…)`+to override individual fields of the result (any `None` field keeps+the original). Return `None` to pass through unchanged.++Use for: redaction, retries, logging, transforming results.++Composition: later overrides earlier (each non-`None` field in a+return value overrides the prior).++## `should_stop_after_turn`++```python+async def should_stop_after_turn(self, ctx: ShouldStopAfterTurnContext) -> bool:+ ...+```++Fires **at each turn boundary** (after any tool batch). Return `True`+to end the run without another model call.++Use for: max-turn limits, budget caps, application-defined stop+conditions.++Composition: **any `True` stops** (logical OR across the chain).++## `after_model_response`++```python+async def after_model_response(+ self,+ response: AssistantMessage,+ ctx: AgentContext,+ *,+ signal=None,+) -> TurnAction | None:+ ...+```++Fires **immediately after** the assistant message lands, **before**+`message_end` is emitted and **before** any tool calls dispatch. The+hook returns a `TurnAction`:++```python+from cubepi.middleware.base import TurnAction+from cubepi.providers.base import UserMessage, TextContent++TurnAction(+ response=modified_message, # replace the message; None to keep+ inject_messages=[UserMessage(...)], # extra messages to append before next turn+ decision="natural", # "natural" | "stop" | "loop_to_model"+)+```++Three control-flow knobs:++- `decision="natural"` (default) — proceed to tool execution / next+ turn as normal.+- `decision="stop"` — end the run after emitting `turn_end` and+ `agent_end`. No tools run, no more model calls.+- `decision="loop_to_model"` — skip tool execution and re-invoke the+ model immediately (use with `inject_messages` to add context first).++Use for: response moderation, structured-output validation with+re-prompts, conditional routing.++Composition: chain — each middleware sees the previous+middleware's `response`; `inject_messages` concatenate across the+chain; the last middleware's `decision` wins.++## `on_run_end`++```python+async def on_run_end(+ self,+ ctx: AgentContext,+ *,+ signal=None,+) -> list[Message] | None:+ ...+```++Fires **once per `prompt()` call**, after all turns and tool calls+complete, before `AgentEndEvent` is emitted. Return a non-empty+`list[Message]` to inject those messages into context and run **one+extra model turn** (the run-end pass). Return `None` or `[]` to do+nothing.++The extra turn fires exactly once — a `_reflection_fired` guard in the+loop prevents the injected turn from triggering another `on_run_end`.++**When it fires:**+- Normal completion (loop breaks naturally after all turns).+- `should_stop_after_turn` returns `True`.+- `after_model_response` returns `decision="stop"`.++**When it does NOT fire:**+- The run ends with `stop_reason` `"error"` or `"aborted"`.+- HITL interruption (`HitlDetached` / `HitlAborted`) — the run is+ paused, not finished.++Use for: post-run memory consolidation, conversation summarisation,+audit logging that needs the full turn context.++Composition: messages from all middleware **concatenate** into a single+list; all are injected together before the extra model turn.++## Anatomy of a middleware++A middleware doesn't have to implement every hook. Only override the+ones you need; the base class's unimplemented hooks raise+`NotImplementedError`, but `compose_middleware` skips them+automatically.++```python+from cubepi import Middleware++class MaxTurnsMiddleware(Middleware):+ def __init__(self, max_turns: int) -> None:+ self.max_turns = max_turns+ self.turns = 0++ async def should_stop_after_turn(self, ctx) -> bool:+ self.turns += 1+ return self.turns >= self.max_turns+++agent = Agent(model=…, middleware=[MaxTurnsMiddleware(5)])+```++## See also++- [Composition Rules](./composition) — exact semantics when multiple+ middlewares define the same hook.+- [Examples](./examples) — working middleware for rate limiting,+ logging, retries, sliding-window context, post-run memory.diff --git a/website/versioned_docs/version-0.7/guides/middleware/subagents.md b/website/versioned_docs/version-0.7/guides/middleware/subagents.mdnew file mode 100644index 0000000..2ce3303--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/middleware/subagents.md@@ -0,0 +1,101 @@+---+title: Subagents+description: "Use SubagentMiddleware to delegate self-contained work to child CubePi agents."+---++# Subagents++`SubagentMiddleware` adds a `subagent` tool. When the model calls that tool,+CubePi creates an ephemeral child `Agent`, runs a self-contained prompt, and+returns the child agent's final assistant text as the tool result.++Use it when a parent agent needs to delegate bounded work such as research,+review, extraction, or a focused implementation pass.++## Define subagent specs++```python+from cubepi import Agent+from cubepi.middleware import SubagentMiddleware, SubagentSpec++subagents = {+ "researcher": SubagentSpec(+ name="researcher",+ description="Researches a narrow question and returns concise notes.",+ system_prompt="You are a precise research assistant.",+ ),+ "reviewer": SubagentSpec(+ name="reviewer",+ description="Reviews code for bugs, regressions, and missing tests.",+ system_prompt="You are a rigorous senior code reviewer.",+ ),+}++model = provider.model("claude-sonnet-4-5-20250929")++agent = Agent(+ model=model,+ middleware=[+ SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ shared_tools=[web_search],+ ),+ ],+)+```++If the model requests an unknown `subagent_type`, CubePi falls back to the+`general-purpose` subagent. If you do not define one, the middleware supplies a+basic default.++## Tool access and middleware inheritance++Subagents only receive the tools you pass in:++- `shared_tools` are available to every child agent.+- `SubagentSpec.tools` are available only to that subagent type.+- `excluded_tool_names` prevents recursive or host-only tools from being shared.++Use `inherited_middleware` for middleware every child should run. Use+`SubagentSpec.middleware` for behavior specific to one subagent type.++## Stream child events to your host++Applications often need child events in their own UI or audit log. Provide an+`event_mapper` and optional `event_handler`:++```python+def map_event(event):+ if event.type == "text_delta":+ return {"type": "subagent_text_delta", "delta": event.delta}+ return None++async def handle_event(agent_id, payload):+ await ui_stream.send({"agent_id": agent_id, **payload})++SubagentMiddleware(+ subagents=subagents,+ default_model=model,+ event_mapper=map_event,+ event_handler=handle_event,+)+```++Mapped payloads are also stored in the parent tool result under+`details["subagent_events"]`.++## Tracing and aborts++Pass a `Tracer` via `tracer=...` to attach tracing to each child run. Nested+subagent spans share the parent trace, so `cubepi trace view <trace_id>` renders+the parent tool call and child run together.++The parent run's abort signal is forwarded to the child agent. If the parent is+aborted while a subagent is running, the child agent is aborted as well.++## Prompting guidelines++The parent model should send a self-contained `prompt` in the `subagent` tool+call. Do not rely on the child seeing the parent's full hidden context. Include+the goal, relevant facts, constraints, and desired output shape in the prompt.diff --git a/website/versioned_docs/version-0.7/guides/providers/anthropic.md b/website/versioned_docs/version-0.7/guides/providers/anthropic.mdnew file mode 100644index 0000000..086d8f6--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/providers/anthropic.md@@ -0,0 +1,201 @@+---+title: Anthropic+description: "Use Anthropic Claude models with CubePi's AnthropicProvider — supports thinking, caching, and tool use."+---++# Anthropic Provider++`AnthropicProvider` wraps the official `anthropic` SDK against the+Messages API. It supports streaming, extended thinking, prompt+caching, and tool use.++## Construction++```python+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(+ api_key="sk-ant-…", # or read from os.environ["ANTHROPIC_API_KEY"]+ base_url=None, # set to point at a proxy / Bedrock-compatible endpoint+ cache_retention="short", # "short" (5 min, default) | "long" (1 h) | "none"+)+```++`api_key=None` lets the underlying SDK read `ANTHROPIC_API_KEY` from+the environment.++## The `Model`++```python+provider = AnthropicProvider(provider_id="anthropic")+model = provider.model(+ "claude-sonnet-4-5-20250929",+ reasoning=True, # enables thinking levels (see below)+ max_tokens=8192, # response cap+ context_window=200_000, # hard model limit+ temperature=0.7,+)+```++The first argument is the model name exactly as you'd pass it to the+SDK. `provider_id` is a free-form label used by CubePi internals — keep+it stable across your codebase, and set it when you want tracing and+error messages to show a specific source label.++## Extended thinking++CubePi maps a `ThinkingLevel` enum onto Anthropic's `budget_tokens`:++| Level | Default budget |+|---|---|+| `"off"` | thinking disabled |+| `"minimal"` | 1024 |+| `"low"` | 2048 |+| `"medium"` | 8192 |+| `"high"` | 16384 |+| `"xhigh"` | clamped to `"high"` |++Set it per-agent:++```python+agent = Agent(model=model, thinking="medium")+```++To change the per-level budgets, supply a+[`CapabilityDescriptor`](./overview) with a+`reasoning_level` of `kind="int_budget"` at construction — its+`level_budgets` map is the single source of truth for budget values.++```python+from cubepi import CapabilityDescriptor, ReasoningLevelSpec+from cubepi.providers.anthropic import AnthropicProvider++provider = AnthropicProvider(+ api_key="sk-ant-…",+ capability=CapabilityDescriptor(+ reasoning_off_payload={"thinking": {"type": "disabled"}},+ reasoning_on_payload={"thinking": {"type": "enabled"}},+ reasoning_level=ReasoningLevelSpec(+ path="thinking.budget_tokens",+ kind="int_budget",+ level_budgets={"off": 0, "minimal": 1024, "low": 4096,+ "medium": 12288, "high": 16384, "xhigh": 16384},+ ),+ ),+)+```++:::note Changed in 0.5+`AnthropicProvider` no longer honors+`StreamOptions(thinking_budgets=…)` — the capability descriptor's+`level_budgets` is now the only way to override budgets.+:::++When thinking is on, CubePi **omits `temperature`** because the+Anthropic API rejects non-default temperatures alongside extended+thinking ([feature compatibility](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#feature-compatibility)).+Set `Model.temperature` to the value you want when thinking is off;+CubePi handles the rest.++Thinking content streams as `thinking_start` / `thinking_delta` /+`thinking_end` events and ends up in `AssistantMessage.content` as+`ThinkingContent` blocks, preserved on subsequent turns so the model+keeps continuity.++## Prompt caching++By default the provider marks three cache breakpoints on each request:++- The **system prompt** (most stable).+- The **last tool definition** (changes rarely).+- The **last message** (the cache moves forward each turn so prior+ history stays warm).++Cache retention is `"short"` (5 minutes, free). Bump to `"long"` if+your turns are slower than that:++```python+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="long") # 1-hour TTL+AnthropicProvider(provider_id="anthropic", api_key=…, cache_retention="none") # disable entirely+```++The `Usage` object on each `AssistantMessage` reports+`cache_read_tokens` and `cache_write_tokens` so you can see your hit+rate.++For custom cache strategies (a different breakpoint policy), implement+the `CacheMarkerPolicy` Protocol and pass `cache_policy=…`. The+default policy lives at `cubepi.providers.anthropic.DefaultCacheMarkerPolicy`.++## Custom payloads with `on_payload`++`on_payload` lets you inspect or replace the request dict right before+it's sent:++```python+async def my_payload(payload, model):+ payload.setdefault("metadata", {})["user_id"] = "u-42"+ return payload # return None or no return to keep the original++agent = Agent(model=model, on_payload=my_payload)+```++Use this for: adding `metadata.user_id` (for billing), forcing+beta-header flags, or tracking the exact payloads you sent for a+debug pane.++## Custom response handling with `on_response`++`on_response` fires after the HTTP response is received (status,+headers), before streaming begins:++```python+async def my_response(resp, model):+ if resp.status >= 400:+ logger.warning("bad status %s", resp.status)+ rate = resp.headers.get("anthropic-ratelimit-requests-remaining")+ if rate is not None:+ metrics.gauge("rate_remaining", int(rate))++agent = Agent(model=model, on_response=my_response)+```++Both callbacks may be sync or async.++## Pointing at Bedrock / Vertex / proxies++The Anthropic SDK accepts a `base_url`; CubePi forwards it:++```python+provider = AnthropicProvider(+ api_key="…",+ base_url="https://my-litellm.internal/v1",+)+```++For Bedrock specifically, use the `anthropic-bedrock` adapter directly+and inject it via a [custom provider](./custom).++## Common pitfalls++- **`temperature` ignored** — Expected. CubePi drops it when thinking+ is on; that's an API constraint, not a bug.+- **`xhigh` looks the same as `high`** — Anthropic doesn't expose a+ budget tier above `high`, so CubePi clamps `xhigh` down. The token+ budget is the same.+- **Cache misses you didn't expect** — Caches are keyed by content ++ ttl. Changing the system prompt invalidates everything; changing the+ tool list invalidates from the tools onward. Make those two stable+ across turns to maximise hits.+- **`anthropic.RateLimitError`** — Propagates as a stream error event+ with the SDK's `str(exc)`. Catch in `agent_end` and decide whether+ to retry.++## See also++- [OpenAI Provider](./openai) — same protocol, different shape.+- [Providers Overview](./overview) — tune+ reasoning budgets and temperature handling as data.+- [Custom Provider](./custom) — wrap a non-built-in API.+- [Recipes → Multi-Provider Failover](../../recipes/multi-provider-failover)+ — fall back to OpenAI when Anthropic is down.diff --git a/website/versioned_docs/version-0.7/guides/providers/custom.md b/website/versioned_docs/version-0.7/guides/providers/custom.mdnew file mode 100644index 0000000..278dbec--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/providers/custom.md@@ -0,0 +1,246 @@+---+title: Writing a Custom Provider+description: "Write a custom provider for CubePi by implementing the Provider protocol."+---++# Writing a Custom Provider++A provider is any class matching the `Provider` protocol. In practice,+subclass `BaseProvider` and implement `stream()`; `BaseProvider`+supplies `generate()` for one-shot calls.++```python+class Provider(Protocol):+ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream: ...+```++That is the only method custom providers usually implement. The+inherited `generate()` consumes `stream()` and returns the final+`AssistantMessage`, with per-call overrides for `max_output_tokens`,+`temperature`, `thinking`, and `thinking_budgets`.++This page covers two scenarios:++1. **A new real provider** — Bedrock, Vertex, Replicate, an internal+ LLM gateway, …+2. **`FauxProvider`** — the built-in deterministic provider that's+ essential for unit tests.++## A minimal real provider++The pattern: create a `MessageStream`, kick off a producer task that+pushes events into it, return the stream immediately.++```python+import asyncio+import time+from cubepi.providers.base import (+ AssistantMessage,+ BaseProvider,+ Message,+ MessageStream,+ Model,+ StreamEvent,+ StreamOptions,+ TextContent,+ ToolDefinition,+ Usage,+)+++class MyProvider(BaseProvider):+ def __init__(self, *, api_key: str) -> None:+ super().__init__()+ self._api_key = api_key++ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream:+ opts = options or StreamOptions()+ ms = MessageStream()++ async def _produce():+ try:+ partial = AssistantMessage(+ content=[TextContent(text="")],+ usage=Usage(),+ timestamp=time.time(),+ provider_id=model.provider_id,+ model_id=model.id,+ )+ ms.push(StreamEvent(type="start", partial=partial.model_copy(deep=True)))++ # Call your backend. Stream tokens:+ async for token in call_my_backend(messages, model.id, signal=opts.signal):+ if opts.signal and opts.signal.is_set():+ ms.push(StreamEvent(type="error", error_message="aborted"))+ ms.set_result(partial.model_copy(update={"stop_reason": "aborted"}))+ return+ partial.content[-1] = TextContent(text=partial.content[-1].text + token)+ ms.push(StreamEvent(+ type="text_delta",+ delta=token,+ partial=partial.model_copy(deep=True),+ ))++ ms.push(StreamEvent(type="done"))+ ms.set_result(partial)++ except Exception as exc:+ error_msg = AssistantMessage(+ content=[],+ stop_reason="error",+ error_message=str(exc),+ usage=Usage(),+ timestamp=time.time(),+ )+ ms.push(StreamEvent(type="error", error_message=str(exc)))+ ms.set_result(error_msg)++ ms.attach_task(asyncio.create_task(_produce()))+ return ms+```++Things to get right:++1. **Always push `start` first.** Subscribers count on it for UI+ bootstrapping.+2. **Always end with `done` or `error`.** The agent loop waits on the+ `MessageStream` until one of those arrives.+3. **Always call `ms.set_result(...)`** so `await stream.result()` can+ complete. Even on error.+4. **`ms.attach_task(...)` is required** if the producer is its own+ task — it wires the task's exception state into the stream so a+ crash surfaces as `error`, not a hang.+5. **Respect `opts.signal`.** Check it inside your read loop; emit an+ `aborted` stop_reason so the agent can shut down cleanly.++## Supporting tool calls++If your model emits tool calls, append `ToolCall` blocks to+`partial.content` as they stream in and emit `toolcall_start` /+`toolcall_delta` / `toolcall_end`:++```python+from cubepi.providers.base import ToolCall++tc = ToolCall(id=block_id, name=tool_name, arguments={})+partial.content.append(tc)+ms.push(StreamEvent(type="toolcall_start", content_index=len(partial.content)-1,+ partial=partial.model_copy(deep=True)))+# ... as JSON args arrive:+ms.push(StreamEvent(type="toolcall_delta", delta=partial_json_chunk, …))+# ... on completion:+# replace tc.arguments with the parsed dict, push toolcall_end+```++CubePi's agent loop will dispatch the tool calls automatically once+`done` is emitted.++## Hooking `on_payload` / `on_response`++If your provider sends an HTTP request, call the helpers in+`cubepi.providers.base`:++```python+from cubepi.providers.base import (+ ProviderResponse,+ invoke_on_payload,+ invoke_on_response,+)++payload = await invoke_on_payload(opts.on_payload, payload, model)+http_resp = await self._client.post(..., json=payload)+await invoke_on_response(+ opts.on_response,+ ProviderResponse(status=http_resp.status_code, headers=dict(http_resp.headers)),+ model,+)+```++This mirrors what built-in providers do; your users get the same+inspection points for free.++## Using `FauxProvider` in tests++CubePi ships `FauxProvider` for deterministic tests — no network, no+flakiness, real streaming events:++```python+from cubepi import Agent, Model+from cubepi.providers import FauxProvider, faux_assistant_message, faux_text, faux_tool_call+++def test_my_agent():+ provider = FauxProvider(provider_id="faux")+ provider.set_responses([+ faux_assistant_message([+ faux_tool_call("search", {"query": "python"}),+ ]),+ faux_assistant_message("Here are the results: …"),+ ])++ agent = Agent(+ model=provider.model("test"),+ tools=[my_search_tool],+ )+ events = []+ agent.subscribe(lambda e, signal=None: events.append(e))+ await agent.prompt("Search for python")++ assert any(e.type == "tool_execution_start" for e in events)+ assert events[-1].type == "agent_end"+```++`set_responses` is a FIFO queue: each model call pops one. The faux+provider replays it with realistic deltas (token-by-token), so your+streaming code paths actually exercise.++Helpers:++- `faux_text("Hello!")` — wraps a string into a `TextContent` block.+- `faux_thinking("Pondering…")` — a `ThinkingContent` block.+- `faux_tool_call("name", {"arg": …})` — a `ToolCall` block.+- `faux_assistant_message(content_or_text)` — builds a complete+ `AssistantMessage`.++## Common pitfalls++- **Missing `start` event** — Subscribers don't see the partial+ message. Always push it first.+- **Forgot `ms.set_result(...)`** — `await agent.prompt()` hangs+ forever. Set the result on both happy and error paths.+- **Synchronous `produce`** — `stream()` must return *immediately* —+ do the work inside the task. If you `await` your backend before+ `return ms`, you've blocked the caller.+- **Modifying `partial` after pushing** — Push `deep=True` copies; the+ caller iterates events asynchronously, and a mutation during+ iteration creates very hard-to-debug aliasing.++## See also+- [Image Generation](./image-generation) — generate images with `openai-images` and image editing.++- [Providers Overview](./overview) — before+ writing a class from scratch, check whether your backend is just an+ OpenAI/Anthropic-compatible endpoint a `CapabilityDescriptor` or+ bundled preset already covers.+- [API Reference → providers/base](../../api/cubepi-providers) — the+ full type list.+- [Anthropic Provider source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/anthropic.py)+ — a real, complete example.+- [`FauxProvider` source](https://github.com/cubeplexai/cubepi/blob/main/cubepi/providers/faux.py)+ — the testing primitive, including stream-realism details.diff --git a/website/versioned_docs/version-0.7/guides/providers/image-generation.md b/website/versioned_docs/version-0.7/guides/providers/image-generation.mdnew file mode 100644index 0000000..687d1a1--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/providers/image-generation.md@@ -0,0 +1,141 @@+---+title: Image Generation+description: "Generate images with CubePi image providers."+---++# Image Generation++This page documents CubePi's separate image generation path. It is intentionally+independent from `Agent`: chat models use the `Model` path, while image models+use `ImagesModel` and run through an image provider instance directly.++## `ImagesModel` types++Image generation uses three small types from `cubepi.providers.images.types`:++```python+from cubepi.providers.images.types import AssistantImages, ImagesContext, ImagesModel+```++- `ImagesModel`: minimal model binding for image APIs (`id`, `provider`, `api`).+- `ImagesContext`: request body for generation (`prompt`, optional `input_images`).+- `AssistantImages`: provider response with `output`, `stop_reason`, and+ optional `error_message`.++`ImageContent` (from `cubepi.providers.base`) is used in `input_images` and+`AssistantImages.output`.++## Create image models++For OpenAI image models, use `create_images_provider("openai-images", ...)` and+an `ImagesModel`:++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++image_provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")+```++`api` is an identifier used by downstream systems for routing and reporting.++## Generate text-to-image++```python+import os++from cubepi.providers.images import create_images_provider+from cubepi.providers.images.types import ImagesContext, ImagesModel++provider = create_images_provider(+ "openai-images",+ api_key=os.environ["OPENAI_API_KEY"],+)+model = ImagesModel(id="gpt-image-1", provider="openai", api="openai-images")++resp = await provider.generate_images(+ model=model,+ context=ImagesContext(prompt="A cute robot at sunrise"),+ options={"size": "1024x1024", "quality": "high", "n": 2},+)++if resp.stop_reason == "error":+ raise RuntimeError(resp.error_message or "image generation failed")++for block in resp.output:+ print(block.type, block.source[:30], block.media_type)+```++If `options` is omitted, the provider sends defaults (`n=1`) and no optional+image-only params.++## Edit with input images++Passing `input_images` switches to the edit path automatically:++```python+import base64+from cubepi.providers.base import ImageContent+from cubepi.providers.images.types import ImagesContext++with open("source.png", "rb") as fh:+ source_b64 = base64.b64encode(fh.read()).decode("ascii")++ctx = ImagesContext(+ prompt="Make it brighter and warmer.",+ input_images=[ImageContent(source=source_b64, media_type="image/png")],+)+```++`generate_images(model, context=ctx)` calls `edit` instead of `generate`.++## `options` forwarding++`generate_images(..., options)` is a free-form dict passed straight into the+backend call (after adding the required `model`, `prompt`, and default `n`).+The OpenAI image provider currently supports common keys like:++- `size`+- `quality`+- `n`+- `output_format` (`png`, `jpeg`, `webp`)+- `background`+- any backend extension fields accepted by your gateway++The provider only filters output media type for `output_format`:+`png` -> `image/png`, `jpeg` -> `image/jpeg`, `webp` -> `image/webp`.++## Error handling++On backend failures, `generate_images(...)` returns `AssistantImages` with:++- `stop_reason="error"`+- `output=[]`+- `error_message=<provider error>`++This keeps the API failure-in-band and lets callers render a single+success/error branch.++## Testing and stubs++For local tests, `FauxImagesProvider` provides a deterministic image output:++```python+from cubepi.providers.images.faux import FauxImagesProvider++provider = FauxImagesProvider(png_b64="iVBORw0KGgoAAAANSUh...")+```++It is useful for end-to-end UI tests where you only need a stable `ImageContent`.++## See also++- [Providers Overview](./overview) — when model-level wiring differs from defaults.+- [OpenAI Provider](./openai) — shared OpenAI config patterns.+- [API Reference → Image types in providers](../../api/cubepi-providers).diff --git a/website/versioned_docs/version-0.7/guides/providers/openai.md b/website/versioned_docs/version-0.7/guides/providers/openai.mdnew file mode 100644index 0000000..71661f3--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/providers/openai.md@@ -0,0 +1,187 @@+---+title: OpenAI+description: "Use OpenAI GPT models with CubePi's OpenAIProvider — Chat Completions API integration."+---++# OpenAI Provider++CubePi ships two OpenAI providers covering the two API surfaces:++- **`OpenAIProvider`** — Chat Completions API+ (`/v1/chat/completions`). Use this for the GPT-4/5 family and most+ OpenAI-compatible servers (vLLM, LiteLLM, DeepSeek, Qwen, MiniMax,+ DouBao, …).+- **`OpenAIResponsesProvider`** — Responses API+ (`/v1/responses`). Use this when you want server-side state and+ reasoning summaries.++Both implement the same `Provider` protocol; pick one per agent.++## Chat Completions: `OpenAIProvider`++```python+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ provider_id="openai",+ api_key="sk-…", # or reads OPENAI_API_KEY+ base_url=None, # set for OpenAI-compatible servers+ extra_body=None, # merged into every request+ extra_headers=None,+)++model = provider.model(+ "gpt-5",+ reasoning=True, # enables thinking level mapping+ max_tokens=8192,+ context_window=128_000,+)+```++### Thinking on Chat Completions++OpenAI exposes reasoning content through `delta.reasoning_content` on+o-series and gpt-5 models. CubePi captures it as `ThinkingContent` and+emits `thinking_*` events identically to Anthropic. The same+`ThinkingLevel` enum (`"off"` → `"high"`) works.++Many OpenAI-compatible OSS backends emit reasoning under different+fields. CubePi understands three in priority order:++1. `delta.reasoning_content` (DeepSeek, Qwen, DouBao)+2. `delta.reasoning` (vLLM)+3. `delta.reasoning_details` (MiniMax)++No configuration needed — the provider picks whichever field is+present.++### `extra_body` for OSS quirks++Most OpenAI-compatible servers accept extensions through the request+body. Set them once at construction:++```python+provider = OpenAIProvider(+ api_key="…",+ base_url="https://api.deepseek.com/v1",+ extra_body={"enable_thinking": True, "stream_options": {"include_usage": True}},+)+```++If you need per-request mutation, use `on_payload` (see below).++### Capability descriptor++Wire-shape differences between OpenAI and OpenAI-compatible backends+(e.g. `max_tokens` vs `max_completion_tokens`, reasoning field names,+temperature handling) are configured through a+[`CapabilityDescriptor`](pathname:///pydoc/cubepi/providers/capability.html)+passed at construction. For example, `max_tokens_field="max_completion_tokens"`+renames the key on the way out. See [Providers Overview](./overview)+for the full set of knobs (CubePi `0.5+`).++### Pointing at vLLM / LiteLLM / DeepSeek++```python+provider = OpenAIProvider(+ api_key="dummy", # vLLM ignores it+ base_url="http://localhost:8000/v1",+ extra_headers={"Authorization": "Bearer dummy"},+)+```++For LiteLLM:++```python+provider = OpenAIProvider(+ api_key=os.environ["LITELLM_KEY"],+ base_url="https://litellm.internal/v1",+)+```++For the wire quirks of these backends (reasoning toggles, token-field+renames, …), see [Providers Overview](./overview).++## Responses API: `OpenAIResponsesProvider`++```python+from cubepi.providers.openai_responses import OpenAIResponsesProvider++provider = OpenAIResponsesProvider(provider_id="openai_responses", api_key="sk-…")+model = provider.model("gpt-5", reasoning=True)+```++The Responses API keeps state server-side (referenced by+`previous_response_id`). CubePi tracks `AssistantMessage.response_id`+and feeds it back automatically — your code looks identical to the+Chat Completions path.++Use the Responses provider when:++- You want reasoning **summaries** (not just text) surfaced as+ thinking blocks.+- You're using the `o`-series and want the server to hold the+ reasoning chain across turns (smaller payloads, faster reuse).++Stay on `OpenAIProvider` when you want full control over the message+list and prompt caching strategy.++## `on_payload` / `on_response`++Same shape as the [Anthropic](./anthropic) provider. The payload dict+differs (`messages` instead of `messages` + `system` separately,+OpenAI-style `tools` schema), so inspect it once before mutating.++```python+async def add_user_metadata(payload, model):+ payload["user"] = "u-42" # billable user attribution+ return payload++agent = Agent(model=model, on_payload=add_user_metadata)+```++## Tool calling++Tool definitions are auto-converted to OpenAI's+`{"type": "function", "function": {...}}` shape. The streaming format+emits incremental JSON arguments under `toolcall_delta`; CubePi+buffers and parses them through+[`cubepi.utils.json_parse.parse_streaming_json`](../../api/cubepi-utils)+so partials always validate to the closest well-formed object.++Multiple parallel tool calls in one assistant message just work —+they're routed through the same parallel executor as the Anthropic+provider.++## Common pitfalls++- **`stream_options.include_usage` rejected** — Some compatibles+ reject the whole `stream_options` field. **`on_payload` cannot fix+ this**: CubePi 0.3 calls `kwargs.setdefault("stream_options", {})`+ *after* your callback runs, so deleting the key in `on_payload` is+ silently undone. Workarounds:+ - Subclass `OpenAIProvider` and override `stream()` to skip the+ `setdefault` for your backend.+ - Set `include_usage=False` in `on_payload` (the field still goes+ out, but is usually accepted as a no-op even by strict+ backends).+ - Use a [`CapabilityDescriptor`](./overview) (CubePi `0.5+`) to+ describe your backend's reasoning wiring declaratively.+- **Thinking events but no `thinking_*` events** — Your backend+ surfaces reasoning under a non-standard field. Either add a fourth+ branch via PR or transcode it with `on_payload`.+- **Mixed providers in one process** — Each provider holds its own+ HTTP client. Reuse a single instance per `(base_url, api_key)`+ pair instead of creating one per agent.+- **Usage shows 0 input tokens** — Most compatibles omit usage+ entirely or only emit it on the final chunk. Inspect the trailing+ chunk in `on_payload` for a hint, or treat token counts as+ best-effort on those backends.++## See also+- [Image Generation](./image-generation) — generate images with `openai-images` and image editing.++- [Anthropic Provider](./anthropic) — the other built-in.+- [Custom Provider](./custom) — write your own from scratch.+- [Recipes → Multi-Provider Failover](../../recipes/multi-provider-failover)+ — combine both providers for resilience.diff --git a/website/docs/guides/providers/capability-and-presets.md b/website/versioned_docs/version-0.7/guides/providers/overview.mdsimilarity index 61%rename from website/docs/guides/providers/capability-and-presets.mdrename to website/versioned_docs/version-0.7/guides/providers/overview.mdindex bd97f84..dab9733 100644--- a/website/docs/guides/providers/capability-and-presets.md+++ b/website/versioned_docs/version-0.7/guides/providers/overview.md@@ -1,15 +1,17 @@----title: Capability Descriptors-description: "Provider capabilities and model presets in CubePi — compare features across providers."+title: Providers Overview+description: "Provider setup, capabilities, and presets in CubePi."----# Capability Descriptors+# Providers Overview-_Capability descriptors added in CubePi `0.5`._+_Start here for provider setup in CubePi._-This page is about reaching **any** model — the big SaaS APIs, a regional-endpoint, a coding-plan tier, or your own vLLM box — without writing-per-vendor glue. The progression is deliberate:+This page is the entry point for provider configuration. It covers the+default path for Anthropic and OpenAI, then explains how to describe+wire-level differences and preset overrides when you need to reach a+different backend without writing per-vendor glue. The progression is+deliberate:1. **The default is zero config.** For Anthropic and OpenAI you write aprovider and a model. Nothing on this page is required.@@ -17,6 +19,75 @@ per-vendor glue. The progression is deliberate:`CapabilityDescriptor` captures the differences declaratively — nosubclassing, no forking.+## `provider.model(...)` parameters++Use `provider.model(model_id, ...)` to create a bound model for an agent.+`model_id` is required and positional; everything else is optional keyword+arguments:++- `api: str` — alternate API name/route tag for downstream integrations.+- `reasoning: bool` — enable reasoning mode and thinking-level negotiation.+- `context_window: int` — context-capacity hint used for validation and prompt planning.+- `max_tokens: int` — default max generation cap for this model.+- `temperature: float` — default sampling temperature for this model.+- `cost: ModelCost | None` — optional cost metadata object.+- `thinking_level_map: dict[str, str | None] | None` — optional map for level+ overrides and unsupported levels (`None` disables a level).++## `CapabilityDescriptor` is what to use when behavior differs by backend++`CapabilityDescriptor` is passed to a provider to express wire differences for+all models served by that provider:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://api.deepseek.com",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ reasoning_off_payload={"extra_body": {"thinking": False}},+ max_tokens_field="max_completion_tokens",+ ),+)+```++If only one model needs an override, use+`model_capability_overrides`:++```python+from cubepi import CapabilityDescriptor+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ base_url="https://openrouter.ai/api/v1",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": True}},+ ),+ model_capability_overrides={+ "deepseek-r1": CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"thinking": "enabled"}},+ ),+ },+)+```++`model_capability_overrides` is matched by exact `model_id`.++`CapabilityDescriptor` supports these fields:++- `reasoning_on_payload / reasoning_off_payload` — payload merged when+ reasoning is on/off.+- `reasoning_level` (`ReasoningLevelSpec`) — map `off`/`minimal`/... to backend+ payload paths.+- `temperature` (`TemperatureSpec`) — clip, force, or strip temperature.+- `max_tokens_field` — pick `max_tokens` or `max_completion_tokens`.+- `supports_tools` / `supports_images` / `supports_streaming` — metadata consumed+ by host UI and product code.+:::note Preset catalogs live in the host applicationCubePi ships the **mechanism** (the `CapabilityDescriptor` and the wireruntime that applies it), not a catalog of vendors. A ready-made list of@@ -34,13 +105,11 @@ sensible defaults:```pythonimport cubepi-from cubepi import Agent, Model+from cubepi import Agentfrom cubepi.providers.anthropic import AnthropicProvider-agent = Agent(- provider=AnthropicProvider(), # reads ANTHROPIC_API_KEY- model=Model(id="claude-sonnet-4-6", provider="anthropic"),-)+provider = AnthropicProvider(provider_id="anthropic") # reads ANTHROPIC_API_KEY+agent = Agent(model=provider.model("claude-sonnet-4-6"))await agent.prompt("Hello!")```@@ -133,6 +202,39 @@ Beyond on/off, CubePi maps a `ThinkingLevel`(`off`/`minimal`/`low`/`medium`/`high`/`xhigh`) onto a concrete wire valuewritten at a dotted `path`. `kind` picks the shape:+`ReasoningLevelSpec` only changes how that level is serialized. You still need+two call-site controls:++- set `reasoning=True` when binding the model (enable reasoning for that model)+- set the Agent's `thinking` argument to one of `off|minimal|low|medium|high|xhigh`+ (defaults to `off`).++```python+from cubepi import Agent, CapabilityDescriptor, ReasoningLevelSpec+from cubepi.providers.openai import OpenAIProvider++provider = OpenAIProvider(+ api_key="...",+ capability=CapabilityDescriptor(+ reasoning_on_payload={"extra_body": {"reasoning": {"enabled": True}}},+ reasoning_level=ReasoningLevelSpec(+ path="reasoning.effort",+ kind="effort",+ level_to_effort={+ "off": "low",+ "minimal": "low",+ "low": "low",+ "medium": "medium",+ "high": "high",+ "xhigh": "high",+ },+ ),+ ),+)++agent = Agent(model=provider.model("deepseek-r1", reasoning=True), thinking="high")+```+```pythonfrom cubepi import ReasoningLevelSpecdiff --git a/website/versioned_docs/version-0.7/guides/tracing/cli.md b/website/versioned_docs/version-0.7/guides/tracing/cli.mdnew file mode 100644index 0000000..d1b5c0e--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/cli.md@@ -0,0 +1,185 @@+---+title: The trace CLI+description: "Use the CubePi trace CLI to inspect, filter, and display OpenTelemetry trace spans."+---++# Inspecting traces with `cubepi trace`++The `JsonlSpanExporter` writes one file per trace under+`./cubepi-traces/<date>/<trace_id>.jsonl`. The `cubepi trace` CLI (provided by+the `trace-cli` extra) reads those files so you can see exactly what a run did+— which LLM and tool calls fired, in what order, what each returned, where it+errored, and the token counts — without re-running it.++```bash+pip install 'cubepi[trace-cli]' # or: uv sync --extra trace-cli+cubepi trace --help+```++`--dir` defaults to `./cubepi-traces`; pass `--dir <path>` if your traces live+elsewhere. Each file is one **trace**: the run plus any nested subagent runs,+which inherit the parent's `trace_id` and so land in the same file.++## `ls` — list recent traces++```bash+cubepi trace ls # newest first; -n N to limit+```++| column | meaning |+|---|---|+| `started` | trace start time (UTC) |+| `trace_id` | the id you pass to `view` / `follow` / `stats` |+| `spans` | span count for the whole trace (incl. subagents) |+| `status` | `ok` or `error` |+| `duration` | wall-clock span of the trace |+| `input` | the user's prompt, to identify the run |++### Filter by run metadata (`--meta`)++If the host stamped run-scoped metadata onto the trace (via+`tracing_context(metadata=…)` — e.g. cubebox records `conversation_id`,+`user_id`, `org_id`, `workspace_id` on the root `invoke_agent` span), filter to+just those traces:++```bash+cubepi trace ls --meta conversation_id=conv_123+cubepi trace ls --meta user_id=usr_9 --meta org_id=org_1 # repeatable = AND, exact match+```++Traces produced by [`Tracer.oneshot()`](../../api/cubepi-tracing#tracer-oneshot)+(background LLM calls without a full agent loop) are also indexed here. Filter+them by the `operation` name passed to `oneshot()`:++```bash+cubepi trace ls --meta oneshot_operation=consolidate_memory+cubepi trace stats --by model --meta oneshot_operation=consolidate_memory+```++Each `--meta KEY=VALUE` is matched exactly against the trace's root metadata;+repeating the flag ANDs the conditions.++To **display** metadata values as columns (rather than only filter by them),+add `--show-meta KEY[,KEY…]`:++```bash+cubepi trace ls --show-meta conversation_id,user_id+cubepi trace ls --meta org_id=org_1 --show-meta conversation_id # filter + show+```++(Or see all of a single trace's metadata with `cubepi trace view <id> -v`.)++## `view` — render a trace as a span tree++A trace-id **prefix** is enough (the table truncates ids); an ambiguous prefix+lists candidates.++```bash+cubepi trace view 1cd97cdb+```++```+trace+└── invoke_agent 14425.8ms [0x1cd97cdb]+ ├── cubepi.turn 1283.1ms [0x5cfda93e]+ │ ├── chat deepseek-v4-flash 1208.7ms tok 6845/68 [0x0d130229]+ │ └── execute_tool subagent 9610.2ms subagent [0x38bdd10a]+ │ └── invoke_agent 9601.0ms [0x8094f99b] ← subagent run, nested+ │ └── cubepi.turn 9598.4ms [0x57c5cfc7]+ │ ├── chat deepseek-v4-flash 1190.3ms [0x8205ca6b]+ │ └── execute_tool web_search 6500.2ms web_search [0xca4e59fc]+ └── cubepi.turn 491.9ms ERROR [0xce25f242]+ └── chat deepseek-v4-flash 427.2ms ERROR [0x0bff68ec]+ └── error: Error code: 400 - ... `tool_use` ids were found without+ `tool_result` blocks immediately after: call_01_...+```++Read it top-down: `invoke_agent` (a run) → `cubepi.turn` (one agent-loop turn)+→ `chat <model>` (an LLM call, with `tok <input>/<output>`) and+`execute_tool <name>` (a tool call). A **subagent** shows up as+`execute_tool subagent` with its own `invoke_agent → cubepi.turn → …` nested+directly beneath it. The `[0x…]` suffix on each node is the span's `span_id` —+grep it in the raw JSONL to inspect that exact span. Errors print inline under+the failing span.++Flags:++```bash+cubepi trace view <id> --content # expand gen_ai prompts / tool args / results+cubepi trace view <id> -v # expand ALL span attributes (verbose, large)+```++`--content` requires the run to have been recorded with+`record_content=True` (see [Content & Redaction](./content-recording)).++## `follow` — watch a trace live++```bash+cubepi trace follow <id> # polls as spans complete; good for a run in progress+```++## `stats` — aggregate across traces++```bash+cubepi trace stats --by model # latency p50/p95, error rate, tokens+cubepi trace stats --by tool --since 2026-05-20+```++`stats` also accepts `--meta KEY=VALUE` (same semantics as `ls`) to aggregate+only the traces that match — e.g. latency / error-rate / tokens for one user or+conversation:++```bash+cubepi trace stats --by model --meta user_id=usr_9+cubepi trace stats --by tool --meta conversation_id=conv_123+```++## `convert` — reconstruct an API request body++When you need to replay a specific LLM call — reproduce a failure, test a prompt+change, or run a raw `curl` against the same context — `convert` reads a recorded+`chat` span and outputs the complete request body.++Requires `record_content=True`.++```bash+# Default: last chat span in the trace, OpenAI JSON format+cubepi trace convert <trace_id>++# Select which LLM call to reconstruct+cubepi trace convert <trace_id> --turn 2 # 2nd chat span (1-indexed)+cubepi trace convert <trace_id> --span 0xbb7eb1 # by span_id prefix (from `view`)++# Output formats+cubepi trace convert <trace_id> --format openai # default — JSON request body+cubepi trace convert <trace_id> --format anthropic # Anthropic Messages API body+cubepi trace convert <trace_id> --format curl # shell-executable curl command+```++The `[0x…]` span id from `view` output goes directly into `--span`:++```+├── chat kimi-k2.6 31704.5ms [0xbb7eb192] ← paste as: --span 0xbb7eb1+├── chat kimi-k2.6 32420.2ms [0x7c76f48d] ← or: --span 0x7c76f4+```++The reconstructed body includes the full conversation history, the system prompt,+all tool definitions, and request parameters (`model`, `max_tokens`,+`temperature`). Pipe to `python -m json.tool`, `jq`, or directly to a replay script.++## Beyond the CLI++The files are plain JSONL — one span per line — so you can parse them directly+(`jq`, `python -c`) to pull a specific attribute (`gen_ai.usage.*`,+`gen_ai.tool.call.result`, `gen_ai.input.messages`, …). Error detail lives in a+span **event** named `gen_ai.client.operation.exception`.++:::tip For AI agents+A bundled `cubepi-trace` skill drives this CLI for debugging ("why did the run+end with no reply?", "the tool result is wrong"). It encodes the fast path+(`ls` → `view <prefix>`) and the token/cache-rate conventions.++```bash+npx skills add cubeplexai/cubepi@cubepi-trace -a claude-code+```+:::diff --git a/website/versioned_docs/version-0.7/guides/tracing/content-recording.md b/website/versioned_docs/version-0.7/guides/tracing/content-recording.mdnew file mode 100644index 0000000..a469c95--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/content-recording.md@@ -0,0 +1,164 @@+---+title: Content & Redaction+description: "Configure prompt content recording and redaction in CubePi's OpenTelemetry tracing."+sidebar_position: 4+---++# Recording Prompts, Responses, and Tool Payloads++By default CubePi's tracing emits structural attributes only — operation+names, models, token counts, finish reasons, durations. **No prompt content,+no model output, no tool arguments or results leave the process.** This is+deliberate: many agent setups handle PII, customer data, or trade-secret+prompts that don't belong in a third-party observability backend.++When you _do_ want content captured — for offline evaluation, debugging a+flaky tool call, or building a labelled dataset — opt in explicitly with+`record_content=True`, and combine it with a `redact` callback to strip the+sensitive parts before they leave the process.++## Turning content recording on++```python+tracer = Tracer(+ service_name="my-bot",+ agent_name="assistant",+ record_content=True, # ← opt-in+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+)+```++With `record_content=True`, each span layer carries the relevant content+attributes per the OTel GenAI semconv:++| Span | Content attributes added |+|---|---|+| `invoke_agent` | `gen_ai.system_instructions`, `gen_ai.input.messages`, `gen_ai.output.messages` |+| `cubepi.turn` | `gen_ai.input.messages` (per-turn slice), `gen_ai.output.messages` (per-turn slice) |+| `chat <model>` | `gen_ai.system_instructions`, `gen_ai.input.messages`, `gen_ai.tool.definitions`, `cubepi.llm.raw_request`, `cubepi.llm.raw_response` |+| `execute_tool <tool_name>` | `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result` |++The `chat` span's `gen_ai.input.messages` contains the **full chronological+context** the provider request actually carried — including prior assistant+turns and tool results — not just the new user prompt. This matters for+multi-turn tool-using runs: trace consumers can reconstruct exactly what+the model saw at each call.++## Redacting before export++`redact` is a `(key, value) -> value | None` callback invoked at every+content-attribute set site. Return:++- The original value unchanged → keep as-is+- A modified value of the same shape → substitute+- `None` → drop the attribute entirely++```python+def redact(key: str, value):+ # Strip secrets from prompts before they leave the process.+ if key in ("gen_ai.input.messages", "gen_ai.output.messages"):+ return _scrub_messages(value)+ # Don't ship raw bodies at all in prod — keep only the normalised shape.+ if key in ("cubepi.llm.raw_request", "cubepi.llm.raw_response"):+ return None+ return value+++tracer = Tracer(+ service_name="my-bot",+ record_content=True,+ redact=redact,+ exporters=[…],+)+```++`redact` is the single chokepoint for content — the recorder calls it once+per attribute before serializing into the OTel attribute, so anything the+function returns is what hits the wire. Exceptions inside `redact` are+swallowed (the attribute is dropped in that case), so a buggy redactor+fails closed rather than leaking.++### Common patterns++Drop everything but per-message length so dashboards still work without+shipping content:++```python+def redact(key, value):+ if key in ("gen_ai.input.messages", "gen_ai.output.messages"):+ return [{"role": m["role"], "parts": [{"type": "text", "content": "<redacted>",+ "length": sum(len(p.get("content", "")) for p in m["parts"])}]}+ for m in value]+ return value+```++Tag-based selective recording — strip everything unless a thread is opted in:++```python+import contextvars+RECORD = contextvars.ContextVar("trace.record_content", default=False)++def redact(key, value):+ return value if RECORD.get() else None+```++then `RECORD.set(True)` for the runs you want captured.++## Size budgets++OTel attribute values are JSON-serialized inside the recorder. Most backends+truncate or reject attributes over a few hundred KB. If you're recording+the raw provider response on every chat span, multi-turn agentic runs can+get large fast. Drop or summarise via `redact` for any field over your+budget.++## Stream-level recording++`record_content` captures the final assembled request and response on each `chat`+span, but not the individual streaming chunks. For post-mortem debugging of+streaming failures — empty tool call arguments, duplicate events, truncated+output — enable `record_stream` to write a chunk-by-chunk event log alongside the+main trace:++```python+tracer = Tracer(+ record_content=True, # needed for trace convert+ record_stream=True, # ← per-chunk event log+ stream_dir="./cubepi-traces", # where to write <run_id>.stream.jsonl+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+)+```++`record_stream` writes `<stream_dir>/<run_id>.stream.jsonl` (one JSON line per+`StreamEvent`). Every line carries `t` (elapsed seconds from run start) and+`type`. Tool-call lines also include `ci` (content index), `id`, `name`, delta+sizes, and argument previews:++```json+{"t": 5.873, "type": "toolcall_start", "ci": 1, "id": "toolu_...", "name": "show_widget"}+{"t": 5.875, "type": "toolcall_delta", "ci": 1, "chars": 11, "accumulated": 11, "preview": "{\"title\": \""}+{"t": 33.177, "type": "toolcall_end", "ci": 1, "id": "toolu_...", "args_chars": 7465, "args_preview": "{\"title\": \"CubePi..."}+```++This makes it straightforward to confirm whether argument chunks ever arrived, or+whether the same event fired twice (e.g. a provider that sends `finish_reason`+twice produces two `toolcall_end` lines for the same `ci`).++`record_stream` is independent of `record_content` — turn it on only in debugging+sessions. Files can grow large for long-running, tool-heavy agents.++## Auditing what's recorded++The recorder always sets `service.name`, `gen_ai.agent.name`, and+`cubepi.run_id` on every span — regardless of `record_content`. Use these+to filter the trace backend to a single run and visually confirm what+landed.++For deeper audits, `JsonlSpanExporter` writes one line per span, so you can+grep / `jq` the local files before pointing the same exporter at a remote+backend:++```bash+jq -r 'select(.attributes["gen_ai.input.messages"]) | .attributes["gen_ai.input.messages"]' \+ cubepi-traces/2026-05-19/*.jsonl+```diff --git a/website/versioned_docs/version-0.7/guides/tracing/getting-started.md b/website/versioned_docs/version-0.7/guides/tracing/getting-started.mdnew file mode 100644index 0000000..04413ab--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/getting-started.md@@ -0,0 +1,270 @@+---+title: Getting Started+description: "Get started with OpenTelemetry tracing in CubePi — install, configure, and export spans."+sidebar_position: 2+---++# Getting Started with Tracing++## Install the extra++CubePi keeps OpenTelemetry an optional dependency:++```bash+pip install "cubepi[tracing]"+```++This pulls in `opentelemetry-sdk` and friends. Without the extra, the+`cubepi.tracing` import raises a clear error so you find out at import time+rather than mid-run.++## Attach a Tracer++The minimal end-to-end setup — local JSONL export, idiomatic RAII:++```python+import asyncio+from cubepi import Agent, Model+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.tracing import Tracer+from cubepi.tracing.exporters import JsonlSpanExporter+++async def main() -> None:+ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ system_prompt="Be helpful.",+ )++ async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ tracer.attached(agent),+ ):+ await agent.prompt("Say hello.")+ await agent.wait_for_idle()+ # On exit: auto-detach (closes any cancelled-run spans, awaits the+ # flush) + tracer shutdown (flushes + closes exporters). No+ # try/finally needed.+++asyncio.run(main())+```++If you can't restructure into an `async with` (e.g. long-lived web+handler that hands the agent around), the explicit pattern still+works and is fully equivalent:++```python+detach = tracer.attach(agent)+try:+ await agent.prompt("…")+finally:+ # Either is enough on its own:+ # await detach() # awaits the scheduled flush+ # await tracer.shutdown() # flushes + closes exporters+ detach()+ await tracer.shutdown()+```++Even if you forget the cleanup entirely, `Tracer` registers an+`atexit` hook by default that sync-flushes buffered spans at process+exit — pass `atexit_flush=False` to opt out, or rely on it as a+safety net while you're still building. (Doesn't run on `SIGKILL` or+`os._exit`; for guaranteed delivery there, use the synchronous+`SimpleSpanProcessor` from OTel.)++The run produces one JSONL file per trace (sharded by `trace_id`):++```text+./cubepi-traces/+ 2026-05-19/+ 8e1c9a3f4b2d…d976a.jsonl ← one trace, one file, one span per line+```++A trace is the whole run, including any nested subagent runs (they inherit the+parent's `trace_id`, so they land in the same file). Each span still carries+`cubepi.run_id` as an attribute if you want to filter by individual run.++Open it with any tool that reads OTLP/JSON or with `jq` directly:++```bash+jq -r '"\(.name) \(.attributes."gen_ai.operation.name" // "")"' \+ cubepi-traces/2026-05-19/*.jsonl+# invoke_agent invoke_agent+# cubepi.turn+# chat claude-sonnet-4-5-20250929 chat+```++## Span hierarchy++For a single prompt with one LLM round-trip, the recorder produces three spans:++```text+invoke_agent assistant [INTERNAL] gen_ai.operation.name=invoke_agent+└── cubepi.turn [INTERNAL] cubepi.turn.index=0+ └── chat <model> [CLIENT] gen_ai.operation.name=chat+```++When the model calls a tool, you get an extra layer per tool:++```text+invoke_agent assistant+└── cubepi.turn ← turn index 0+ ├── chat <model> ← first round trip+ └── execute_tool <tool_name> ← gen_ai.tool.name, gen_ai.tool.call.id+└── cubepi.turn ← turn index 1 (response after tool result)+ └── chat <model>+```++For MCP tools the `execute_tool` span gets a CLIENT child:++```text+execute_tool <tool_name> [INTERNAL] cubepi-side wrapper+└── tools/call <tool_name> [CLIENT] gen_ai.operation.name=execute_tool+ mcp.method.name=tools/call+ mcp.session.id=…+ server.address / server.port+```++The CLIENT span injects W3C `traceparent` into outgoing HTTP headers, so an+instrumented MCP server can continue the trace.++## Cancellation, errors, aborts++The recorder treats cancellation as a control signal, not a failure:++- `agent.abort()` mid-stream → spans close with `cubepi.aborted=true` and+ `error.type=cubepi.aborted`, **status UNSET** (per OTel guidance — cancellation+ isn't an error).+- A provider raising → chat/turn/root close with **status ERROR**, an+ `exception` event on the chat span, and `error.type` derived from the+ exception class (`timeout`, `connection_error`, fully-qualified class name, …).+- An MCP `tools/call` returning `isError=true` → CLIENT span closes+ ERROR + `error.type=mcp.is_error`.++Either way, `detach()` and `tracer.shutdown()` always close any span the run+left open, so cancelled runs are still visible in your backend rather than+silently disappearing.++## What's on each span++Defaults (no opt-in needed):++- `invoke_agent` (root) — `gen_ai.operation.name`, `gen_ai.provider.name`,+ `gen_ai.agent.name`, `cubepi.run_id`, `cubepi.agent.system_prompt.sha256`,+ `cubepi.agent.tools` (names list), `cubepi.input_messages.count`,+ `cubepi.output_messages.count`+- `cubepi.turn` — `cubepi.turn.index`, `cubepi.turn.stop_reason`,+ `cubepi.turn.tool_calls.count`, `cubepi.turn.terminated_by_tool`,+ `cubepi.run_id`+- `chat <model>` — `gen_ai.operation.name`, `gen_ai.provider.name`,+ `gen_ai.request.model`, `gen_ai.request.max_tokens` / `temperature` /+ `top_p`, `gen_ai.request.stream`, `gen_ai.usage.input_tokens` /+ `output_tokens` / `cache_read_input_tokens` / `cache_creation_input_tokens` /+ `reasoning_output_tokens`, `gen_ai.response.model` /+ `finish_reasons` / `id`, `gen_ai.response.time_to_first_chunk`, plus+ OpenAI-specific extras (`openai.api.type`, service tier, system fingerprint)+- `execute_tool <tool_name>` — `gen_ai.operation.name=execute_tool`,+ `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.tool.description`,+ `gen_ai.tool.type`, `cubepi.tool.is_error`, `cubepi.tool.execution_mode`+- `tools/call <tool_name>` (MCP only) — `mcp.method.name`, `mcp.session.id`,+ `mcp.protocol.version`, `server.address`, `server.port`, `gen_ai.tool.name`++Optional, opt-in via `Tracer(record_content=True)`:+`gen_ai.input.messages`, `gen_ai.output.messages`, `gen_ai.system_instructions`,+`gen_ai.tool.definitions`, `gen_ai.tool.call.arguments`,+`gen_ai.tool.call.result`, `cubepi.llm.raw_request`,+`cubepi.llm.raw_response`. See [Content & Redaction](./content-recording).++## Multiple agents, one process++Both `Tracer` and `Meter` are fine to share across agents — call+`attach(agent)` multiple times. Each attach gets its own recorder /+metric state so concurrent agents don't share span or histogram state,+and MCP CLIENT spans route through the right Tracer based on which+agent's `execute_tool` span is the parent.++With the RAII helper, stacking them is one `async with`:++```python+async with (+ Tracer(...) as tracer,+ tracer.attached(agent_a),+ tracer.attached(agent_b),+):+ await asyncio.gather(agent_a.prompt("…"), agent_b.prompt("…"))+```++## Tagging individual runs++`cubepi.tracing.tracing_context` scopes per-run tags / metadata onto+the `invoke_agent` span — perfect for `user_id`, `session_id`,+A/B-test arm, anything you'd want to filter by in the backend later:++```python+from cubepi.tracing import tracing_context++async with tracer.attached(agent):+ with tracing_context(tags=["beta-arm"], metadata={"user_id": "u-42"}):+ await agent.prompt("Hello.")+```++Attributes on the span:++- `cubepi.tags = ("beta-arm",)`+- `cubepi.metadata.user_id = "u-42"`++The `cubepi.metadata.*` prefix keeps user keys from clobbering+recorder-owned schema (e.g. `cubepi.run_id`). Tags and metadata+contextvars are per-asyncio-task, so concurrent agents see+independent values, and nested `tracing_context` blocks merge+(tags concatenate, metadata keys union with inner winning).++## Tracing background LLM calls (`oneshot`)++`attach()` instruments a cubepi `Agent`. For background tasks that call an LLM+directly — without a full agent loop (no tool use, no multi-turn) — use+`Tracer.oneshot()` instead. It produces the same `invoke_agent` root span and+`chat` child span so the `cubepi trace` CLI indexes it alongside normal agent+runs.++```python+async with tracer.oneshot(+ model=model,+ operation="consolidate_memory", # labelled in the trace+ metadata={"conversation_id": conv_id}, # queryable via --meta+) as session:+ text = await session.generate(+ system=SYSTEM_PROMPT,+ messages=[UserMessage(content=[TextContent(text=prompt)])],+ max_output_tokens=1500,+ )+```++The span tree is flat (no `cubepi.turn` wrapper — there is no loop):++```+invoke_agent 820ms+ └── chat deepseek-v3 815ms tok 3200/180+```++Filter these traces by operation name in the CLI:++```bash+cubepi trace ls --meta oneshot_operation=consolidate_memory+cubepi trace ls --meta conversation_id=conv-123 # alongside the conversation's agent runs+```++The `operation` string is recorded as both `cubepi.oneshot.operation` (for+dashboards) and `cubepi.metadata.oneshot_operation` (so `--meta` can reach it,+since the CLI filter only reads `cubepi.metadata.*` attributes).++## Next++- [OTLP & Backends](./otlp) — Jaeger, Tempo, Honeycomb, Datadog, …+- [Content Recording & Redaction](./content-recording)+- [Metrics](./metrics)diff --git a/website/versioned_docs/version-0.7/guides/tracing/metrics.md b/website/versioned_docs/version-0.7/guides/tracing/metrics.mdnew file mode 100644index 0000000..fc5d315--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/metrics.md@@ -0,0 +1,153 @@+---+title: Metrics+description: "Collect token usage, latency, and time-to-first-content metrics in CubePi tracing."+sidebar_position: 5+---++# Metrics with `Meter`++Spans tell you the shape of one run; histograms tell you the shape of the+fleet. `cubepi.tracing.Meter` mirrors `Tracer` and emits the OTel GenAI+metric set so dashboards work out of the box.++## What it emits++| Instrument | Description |+|---|---|+| `gen_ai.client.operation.duration` | Histogram (seconds) — recorded for `chat`, `execute_tool`, and `invoke_agent` on close |+| `gen_ai.client.operation.time_to_first_chunk` | Histogram (seconds) — recorded for `chat` when at least one content chunk arrived |+| `gen_ai.client.token.usage` | Histogram (`{token}`) — one observation per `gen_ai.token.type` (`input`, `output`) per chat response |++Each point carries the operation, provider, and request model attributes+so failed / cancelled requests (where no response body / response model+landed) are still groupable by what was asked for:++- `gen_ai.operation.name` — `chat` / `execute_tool` / `invoke_agent`+- `gen_ai.provider.name` — `anthropic`, `openai`, `openai_responses`, …+- `gen_ai.request.model` — e.g. `claude-sonnet-4-5-20250929`+- `gen_ai.response.model` — e.g. `claude-sonnet-4-5-20250929` (success only)+- `gen_ai.token.type` — `input` or `output` (token usage only)++## Attaching a Meter++The idiomatic RAII form — `async with` everything, no manual cleanup:++```python+from opentelemetry.exporter.otlp.proto.http.metric_exporter import (+ OTLPMetricExporter,+)+from cubepi.tracing import Tracer, Meter+from cubepi.tracing.exporters import JsonlSpanExporter++async with (+ Tracer(+ service_name="my-bot",+ agent_name="assistant",+ exporters=[JsonlSpanExporter(directory="./cubepi-traces")],+ ) as tracer,+ Meter(+ resource=tracer.resource, # share Resource so service.* matches spans+ exporters=[+ OTLPMetricExporter(endpoint="http://otel-collector:4318/v1/metrics"),+ ],+ ) as meter,+ tracer.attached(agent),+ meter.attached(agent),+):+ await agent.prompt("...")+# Exit order auto: detach both (closes any cancelled-run spans, flushes+# the trace pipeline) → shutdown both (flush + close exporters).+```++`Meter.attach()` is independent from `Tracer.attach()`. You can run+either on its own; the recommended setup is both, sharing one+`Resource` so the backend treats them as the same service.++If you need the explicit, non-RAII form (e.g. attaching agents+dynamically over the lifetime of a long-running server):++```python+tracer_detach = tracer.attach(agent)+meter_detach = meter.attach(agent)+try:+ ...+finally:+ tracer_detach() # closes any cancelled-run spans+ meter_detach() # unsubscribes the meter's listeners+ await tracer.shutdown()+ await meter.shutdown()+```++## Bucket boundaries++The duration histograms ship with the OTel GenAI semconv's recommended+boundaries (in seconds):++```text+0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300+```++OTel exposes these as the `_advisory` boundaries; backends are free to use+them as-is or override.++## Concurrent agents on one Meter++Like `Tracer`, one `Meter` instance is safe to attach to multiple agents+in the same process. Each `attach()` call gets its own internal+`_MeterState` holding the open-ns timestamps and attribute dicts, so+overlapping runs from two agents never share or overwrite each other's+metric state.++```python+meter = Meter(resource=tracer.resource, exporters=[exporter])+meter.attach(agent_a)+meter.attach(agent_b)+```++Both agents emit independent duration / token / TTFC observations,+filterable by `gen_ai.agent.name` (set at the `Resource` level when+`Tracer(agent_name=…)` is used) or by `gen_ai.request.model`.++## Shutting down++The RAII form (`async with … as tracer, … as meter, tracer.attached(agent),+meter.attached(agent):` from above) handles the shutdown ordering for you:+detach inner first → outer `Tracer/Meter` `__aexit__` runs `shutdown()`.++For the manual pattern, order matters — `tracer_detach()` must run before+`tracer.shutdown()` so any spans an in-flight cancellation left open get+closed and exported in the same flush:++```python+finally:+ tracer_detach()+ meter_detach()+ await tracer.shutdown()+ await meter.shutdown()+```++`Meter.shutdown()` awaits a flush of the metric reader, then closes it.+`PeriodicExportingMetricReader` exports on a fixed interval (60 s by+default) — `shutdown` is the only way to guarantee the final window lands+before the process exits.++## Querying example (Honeycomb)++p95 chat latency by provider over the last hour:++```text+VISUALIZE P95(duration_s)+GROUP BY gen_ai.provider.name+WHERE gen_ai.operation.name = "chat"+TIME last 1 hour+```++Token usage by model:++```text+VISUALIZE SUM(token_count)+GROUP BY gen_ai.request.model, gen_ai.token.type+WHERE gen_ai.operation.name = "chat"+```++Substitute your backend's query DSL — same attributes, same aggregations.diff --git a/website/versioned_docs/version-0.7/guides/tracing/otlp.md b/website/versioned_docs/version-0.7/guides/tracing/otlp.mdnew file mode 100644index 0000000..0b3d352--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/otlp.md@@ -0,0 +1,175 @@+---+title: OTLP & Backends+description: "Export CubePi OpenTelemetry traces to OTLP-compatible backends like Jaeger, Tempo, or Honeycomb."+sidebar_position: 3+---++# Exporting to OTLP Backends++`cubepi.tracing.Tracer` accepts any `opentelemetry.sdk.trace.export.SpanExporter`,+so anything in the OpenTelemetry ecosystem works. Pick the wire format+(HTTP or gRPC), point it at your collector, hand the exporter to the Tracer.++## HTTP (OTLP/HTTP)++```bash+pip install "cubepi[tracing]" opentelemetry-exporter-otlp-proto-http+```++```python+from opentelemetry.exporter.otlp.proto.http.trace_exporter import (+ OTLPSpanExporter,+)+from cubepi.tracing import Tracer++tracer = Tracer(+ service_name="my-bot",+ service_version="1.4.2",+ deployment_environment="prod",+ agent_name="assistant",+ exporters=[+ OTLPSpanExporter(+ endpoint="http://otel-collector:4318/v1/traces",+ headers={"x-api-key": "…"}, # backend-specific+ ),+ ],+)+```++`service_name`, `service_version`, `deployment_environment`, and `agent_name`+flow through as OTel Resource attributes (`service.*`, `gen_ai.agent.name`,+`deployment.environment.name`) so backends can group runs without further+config.++## gRPC (OTLP/gRPC)++```bash+pip install "cubepi[tracing]" opentelemetry-exporter-otlp-proto-grpc+```++```python+from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (+ OTLPSpanExporter,+)++exporter = OTLPSpanExporter(endpoint="otel-collector:4317", insecure=True)+tracer = Tracer(service_name="my-bot", exporters=[exporter])+```++## Backend recipes++These all consume OTLP — the only thing that differs is the endpoint and+auth header.++### Jaeger (>=1.50)++Jaeger natively accepts OTLP/HTTP on port 4318:++```python+OTLPSpanExporter(endpoint="http://jaeger:4318/v1/traces")+```++### Grafana Tempo++Send to your collector, or directly to Tempo's OTLP endpoint:++```python+OTLPSpanExporter(endpoint="http://tempo:4318/v1/traces")+```++### Honeycomb++```python+OTLPSpanExporter(+ endpoint="https://api.honeycomb.io/v1/traces",+ headers={"x-honeycomb-team": HONEYCOMB_API_KEY},+)+```++### Datadog (via the OTel collector)++Configure the collector with the Datadog exporter, then ship to it:++```python+OTLPSpanExporter(endpoint="http://otel-collector:4318/v1/traces")+```++Datadog also accepts native OTLP HTTP directly — same shape, different URL.++### AWS X-Ray (via collector)++The OTel collector includes the AWS X-Ray exporter; treat it like any other+OTLP target.++## Continuing an upstream trace++`Tracer.attach(agent)` currently roots every agent run in its own trace —+there is no public API yet for passing an inbound `traceparent` so that+spans nest under a caller's HTTP handler trace. The internal hook+(`Tracer._make_parent_context`) exists for a future `run_scope` feature;+until that ships, agent runs and the surrounding service trace are linked+only by their resource attributes (`service.name`, `gen_ai.agent.name`,+`cubepi.run_id`).++If you need the upstream trace to continue into CubePi today, the+workaround is to set the OTel current span yourself before calling+`agent.prompt(...)` and let the agent's spans inherit it via OTel's+ambient context — CubePi never overrides an active parent.++On the way out, MCP `tools/call` does inject W3C `traceparent` as an HTTP+header automatically, so an instrumented MCP server downstream of the+agent can continue the trace through to its own backend.++## Combining exporters++You can pass multiple exporters and they'll receive every span. Common+pattern — JSONL for local debugging plus OTLP for the production backend:++```python+tracer = Tracer(+ service_name="my-bot",+ exporters=[+ JsonlSpanExporter(directory="./cubepi-traces"),+ OTLPSpanExporter(endpoint="https://api.honeycomb.io/v1/traces", headers={…}),+ ],+)+```++## Flushing++`Tracer` uses `BatchSpanProcessor` under the hood, so spans are exported in+the background. The recommended pattern — and the one that exercises all+the cleanup paths automatically — is the `async with` form:++```python+async with Tracer(...) as tracer, tracer.attached(agent):+ await agent.prompt("...")+# On exit:+# - detach() runs synchronously: closes any spans a cancelled run left+# open, then schedules the flush as an asyncio.Task and awaits it.+# - tracer.shutdown() flushes again (idempotent) and closes exporters.+```++If you've stored `detach` from a manual `tracer.attach(agent)` call, it+returns the scheduled flush `Task`. Two valid manual patterns:++```python+# (a) Belt-and-braces — safest, both run.+finally:+ detach() # closes cancelled-run spans, schedules flush+ await tracer.shutdown() # awaits force_flush, then shuts the SDK down++# (b) Awaited detach — single call when you're not also shutting down.+finally:+ await detach() # awaits the scheduled flush+```++Outside an async context (no running loop) `detach()` returns `None` — the+sync part has run, but the flush is the caller's responsibility via+`await tracer.shutdown()`.++If even that gets missed — say a script raises before reaching `finally` —+`Tracer(atexit_flush=True)` (the default) registers a process-exit handler+that sync-flushes buffered spans through `BatchSpanProcessor`. Doesn't run+on `SIGKILL` / `os._exit`; for guaranteed delivery there, use+`SimpleSpanProcessor` (sync export per span).diff --git a/website/versioned_docs/version-0.7/guides/tracing/overview.md b/website/versioned_docs/version-0.7/guides/tracing/overview.mdnew file mode 100644index 0000000..d6fa591--- /dev/null+++ b/website/versioned_docs/version-0.7/guides/tracing/overview.md@@ -0,0 +1,100 @@+---+title: Tracing Overview+description: "Overview of CubePi's OpenTelemetry-native tracing system — spans, attributes, exporters, and semantic conventions."+sidebar_position: 1+---++# Tracing Overview++CubePi emits [OpenTelemetry](https://opentelemetry.io/) spans that follow the+[GenAI Semantic Conventions v1.41](https://opentelemetry.io/docs/specs/semconv/gen-ai/)+so any OTel-compatible backend (Jaeger, Tempo, Honeycomb, Datadog, AWS X-Ray,+Azure Monitor, …) can ingest agent runs without custom instrumentation.++Attach a `Tracer` to an `Agent` and every prompt produces a tree of spans you+can pivot, query, and join with the rest of your service traces:++```+trace+└── invoke_agent 14425.8ms [0x1cd97cdb] ← one per agent.prompt()+ ├── cubepi.turn 1283.1ms [0x5cfda93e] ← one per LLM round-trip+ │ ├── chat deepseek-v4-flash 1208.7ms tok 6845/68 [0x0d130229]+ │ └── execute_tool subagent 9610.2ms subagent [0x38bdd10a]+ │ └── invoke_agent 9601.0ms [0x8094f99b] ← subagent run, nested+ │ └── cubepi.turn 9598.4ms [0x57c5cfc7]+ │ ├── chat deepseek-v4-flash 1190.3ms [0x8205ca6b]+ │ └── execute_tool web_search 6500.2ms web_search [0xca4e59fc]+ └── cubepi.turn 491.9ms ERROR [0xce25f242]+ └── chat deepseek-v4-flash 427.2ms ERROR [0x0bff68ec]+ └── error: Error code: 400 - ... `tool_use` ids were found without+ `tool_result` blocks immediately after: call_01_...+```++Each layer carries standard `gen_ai.*` attributes — `gen_ai.operation.name`,+`gen_ai.request.model`, `gen_ai.provider.name`, `gen_ai.usage.input_tokens`,+`gen_ai.usage.output_tokens`, `gen_ai.response.finish_reasons`, …++## What ships out of the box++- **Tracer** — builds an SDK `TracerProvider`, attaches one+ `BatchSpanProcessor` per exporter, wires the CubePi event stream into spans.+- **Meter** — sibling for OTel histograms:+ `gen_ai.client.operation.duration`, `gen_ai.client.operation.time_to_first_chunk`,+ `gen_ai.client.token.usage`.+- **JsonlSpanExporter** — write one JSON line per span to+ `./cubepi-traces/<date>/<trace_id>.jsonl`. Files are sharded by `trace_id`,+ so one file holds a whole trace — the run plus any nested subagent runs+ (which inherit the trace). Useful for local dev and offline debugging; works+ with any OTel viewer that reads JSONL, and with the [`cubepi trace`+ CLI](./cli).+- **OTLP** — bring your own exporter via `opentelemetry-exporter-otlp-proto-http`+ (HTTP) or `…-grpc` and hand it to `Tracer(exporters=[…])`.+- **W3C trace context propagation** — outgoing MCP calls inject the active+ `traceparent` as an HTTP header so an instrumented MCP server can continue+ the trace.+- **`tracer.attached(agent)` / `meter.attached(agent)`** — async context+ managers that RAII-wrap attach/detach, so cleanup is one `async with`+ block instead of an explicit `try/finally`.+- **`atexit` flush hook** — `Tracer(atexit_flush=True)` (default) registers+ a process-exit handler that sync-flushes any buffered spans, so callers+ who forget `await tracer.shutdown()` still get their spans exported on+ normal exit / Ctrl-C / unhandled exception.+- **`tracing_context()`** — set per-run tags and metadata+ (`cubepi.tags = ("beta-arm",)`, `cubepi.metadata.user_id = "u-42"`)+ via a contextvar-scoped block. Concurrent agents each see their own+ values.++## What it costs++- One pure-Python recorder per agent run subscribing to the agent's event+ stream and the provider's listener registry — no monkey-patching, no extra+ threads.+- One OTel SDK span per layer above. `BatchSpanProcessor` batches export off+ the hot path.+- **No payloads are recorded by default.** `gen_ai.input.messages`,+ `gen_ai.output.messages`, raw request/response, and tool args/results all+ require explicit opt-in via `record_content=True` so you don't accidentally+ ship PII to your backend. See [Content & Redaction](./content-recording).++## When to use each piece++| You want | Use |+|---|---|+| Trace one local agent run and inspect a JSONL file | `Tracer` + `JsonlSpanExporter` |+| Ship to Jaeger / Tempo / Honeycomb / Datadog | `Tracer` + OTLP exporter |+| Latency + token histograms next to the spans | `Meter` alongside `Tracer` |+| Record prompts / model outputs for evaluation | `Tracer(record_content=True)` |+| Redact PII before it leaves the process | `Tracer(redact=…)` |+| Tag runs with `user_id` / `session_id` / A-B arm | `tracing_context(tags=…, metadata=…)` |+| One-liner cleanup, no try/finally | `async with tracer.attached(agent): …` |+| Forget to call `shutdown()` and not lose spans | `Tracer(atexit_flush=True)` (default) |+| Continue a trace from an upstream service | `Tracer(resource=…)` + W3C `traceparent` (auto for MCP, manual for HTTP) |++## Where to go next++- [Getting Started](./getting-started) — install the extra and emit your+ first spans+- [OTLP & Backends](./otlp) — point CubePi at Jaeger, Tempo, Honeycomb, …+- [Content Recording & Redaction](./content-recording) — record prompts and+ responses safely+- [Metrics](./metrics) — histograms via `Meter`diff --git a/website/versioned_docs/version-0.7/intro.mdx b/website/versioned_docs/version-0.7/intro.mdxnew file mode 100644index 0000000..fe6f93a--- /dev/null+++ b/website/versioned_docs/version-0.7/intro.mdx@@ -0,0 +1,77 @@+---+id: intro+title: "CubePi — Pythonic async-native agent framework"+description: "A Pythonic, async-native agent framework. Plain async functions, append-only checkpointing, three dependencies."+slug: /+sidebar_position: 0+---++# CubePi++**CubePi** is a Pythonic, async-native agent framework designed for high+performance, readability, and production-grade persistence. It provides a+leaner alternative to graph-based agent runtimes by modeling agent logic as+a linear `while` loop that developers can easily trace and debug.++```bash+pip install cubepi+```++Then jump to the [Quick Start](./getting-started/quick-start) to ship a+working tool-using agent in under five minutes.++## What you get++- **Plain async functions, not graph nodes.** The agent loop is a+ `while` loop over message turns. You build tools as `async def`+ functions; the framework routes calls and executes them in parallel.+- **One streaming pattern.** Every provider yields `StreamEvent`s+ through a `MessageStream`. You iterate it with `async for`. No+ callback registries, no separate handler types for text vs. tools.+- **Append-only checkpointing.** Persistence writes the new messages on+ each turn, not the full transcript. O(1) DB I/O regardless of+ conversation length — SQLite for laptops, Postgres for production.+- **Native multi-provider.** Anthropic and OpenAI ship in the box,+ through a `Provider` Protocol. Add a new provider in one class.+- **Eight-hook middleware.** `transform_context`, `convert_to_llm`,+ `transform_system_prompt`, `before_tool_call`, `after_tool_call`,+ `should_stop_after_turn`, `after_model_response`, `on_run_end` — each+ with explicit composition rules. No mystery node ordering.+- **MCP loaders.** Point at any+ [Model Context Protocol](https://modelcontextprotocol.io) server+ (HTTP or stdio) and get back a list of `AgentTool`s.+- **OpenTelemetry built in.** Attach a `Tracer` and every prompt+ produces a tree of OTel spans aligned with the GenAI Semantic+ Conventions — works with Jaeger, Tempo, Honeycomb, Datadog, or any+ OTLP-compatible backend. No payloads recorded by default; opt in+ with `record_content=True` and a `redact` callback.+- **Human-in-the-Loop.** Suspend an agent mid-run, route a decision to+ a human (approve/reject/answer), then resume — with full checkpoint+ persistence so the agent survives a process restart while waiting.++## Where to go next++| If you want to… | Start here |+|---|---|+| Install and run your first agent | [Getting Started → Installation](./getting-started/installation) |+| Understand the building blocks | [Getting Started → Core Concepts](./getting-started/core-concepts) |+| Wire a real tool-using agent | [Guides → Building Your First Agent](./guides/agents/first-agent) |+| Persist a conversation across restarts | [Guides → SQLite Checkpointing](./guides/checkpointing/sqlite) |+| Ship traces to Jaeger / Tempo / Honeycomb | [Guides → Tracing](./guides/tracing/overview) |+| Look up a specific symbol | [API Reference](./api/) |+| See full working examples | [Recipes](./recipes/weather-agent) |+| Port an existing langgraph agent | [Migration → From langgraph](./migration/from-langgraph) |++## Status++You are reading the **CubePi 0.7** release docs. This is the latest stable+documentation set for installing CubePi and building against the published+0.7 API.++CubePi 0.7 adds provider-level `generate()`, compaction and subagent+middleware, `on_run_end`, typed provider errors, `Tracer.oneshot()`, and+checkpointed HITL run IDs. For unreleased changes on `main`, switch to the+**Next** docs from the version picker.++Source, issues, and discussion live on+[GitHub](https://github.com/cubeplexai/cubepi).diff --git a/website/versioned_docs/version-0.7/migration/from-langgraph.md b/website/versioned_docs/version-0.7/migration/from-langgraph.mdnew file mode 100644index 0000000..a39538e--- /dev/null+++ b/website/versioned_docs/version-0.7/migration/from-langgraph.md@@ -0,0 +1,243 @@+---+title: From langgraph+description: "Learn how to migrate your agents from LangGraph to CubePi — concept mapping, side-by-side code comparisons, and a step-by-step porting guide for the Pythonic async-native alternative."+---++# Migrating from langgraph++CubePi and [langgraph](https://github.com/langchain-ai/langgraph) both+build tool-using LLM agents, but they have different mental models.+This page maps langgraph concepts onto CubePi so you can port code+without having to re-learn from scratch.++## Mental-model shift++| langgraph | CubePi | Why |+|---|---|---|+| **State graph** with nodes, edges, channels | **Agent loop** that's a plain `while` loop you can read | A linear loop is easier to reason about than a graph; CubePi never branches at runtime — control flow lives in middleware |+| **Channels** (typed state slots) | **`AgentContext.extra`** + `AgentState.messages` | A single dict + a single message list cover every state shape we've seen |+| **`StateGraph.add_node(name, fn)`** | A middleware hook or a tool | Functions in langgraph nodes split into two roles in CubePi: tool execution (when the model decides) vs. middleware (always-on transforms) |+| **`add_edge(a, b)`** / `add_conditional_edges` | Built-in: tools → next turn → tools → … | The conditional shape (tool calls → re-prompt) is the loop; you don't reify it |+| **`MemorySaver` / `SqliteSaver` / `PostgresSaver`** | `MemoryCheckpointer` / `SQLiteCheckpointer` / `PostgresCheckpointer` | Same idea, append-only schema instead of full snapshots |+| **`config: {"configurable": {"thread_id": …}}`** | `Agent(thread_id=…)` | First-class agent parameter |+| **`stream_mode="messages"` / `"values"` / `"updates"`** | `agent.subscribe(listener)` — one event stream | One pattern, eleven event types |+| **Tools as `@tool` decorated functions** | `AgentTool` with Pydantic params + async execute | Closer to OpenAI/Anthropic native shape |+| **`HumanMessage`, `AIMessage`** | `UserMessage`, `AssistantMessage` | Same role-tagged messages, just renamed |+| **Interrupts via `interrupt_before` / `interrupt_after`** | `agent.steer(...)`, `agent.follow_up(...)`, `agent.abort()` | Imperative control instead of declarative interrupt points |+| **`config_schema`** | Constructor parameters on `Agent` | No separate schema layer |++## Side-by-side: a tool-using agent++### langgraph++```python+from typing import TypedDict+from langchain_anthropic import ChatAnthropic+from langgraph.graph import StateGraph, END+from langgraph.prebuilt import ToolNode+from langchain_core.tools import tool+++@tool+def get_weather(city: str) -> str:+ """Get current weather for a city."""+ return f"72°F and sunny in {city}"+++llm = ChatAnthropic(model="claude-sonnet-4-5-20250929").bind_tools([get_weather])++class State(TypedDict):+ messages: list++def call_model(state: State):+ return {"messages": [llm.invoke(state["messages"])]}++def should_continue(state: State):+ last = state["messages"][-1]+ return "tools" if last.tool_calls else END++graph = StateGraph(State)+graph.add_node("llm", call_model)+graph.add_node("tools", ToolNode([get_weather]))+graph.add_edge("__start__", "llm")+graph.add_conditional_edges("llm", should_continue)+graph.add_edge("tools", "llm")+app = graph.compile()++for chunk in app.stream({"messages": [("user", "Weather in Tokyo?")]}):+ print(chunk)+```++### CubePi++```python+import asyncio+from pydantic import BaseModel+from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++class GetWeatherParams(BaseModel):+ city: str+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ return AgentToolResult(content=[TextContent(text=f"72°F and sunny in {params.city}")])+++agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key="…").model("claude-sonnet-4-5-20250929"),+ tools=[AgentTool(+ name="get_weather",+ description="Get current weather for a city.",+ parameters=GetWeatherParams,+ execute=get_weather,+ )],+)+agent.subscribe(lambda e, s=None: print(e.type))+asyncio.run(agent.prompt("Weather in Tokyo?"))+```++CubePi version removes:++- The `StateGraph`, edges, nodes, `END` sentinel, conditional edges.+- The `ToolNode` registry — tools go directly to the `Agent`.+- The `should_continue` function — the loop knows when there are tool+ calls.+- The `State` TypedDict — state lives on the agent.++## Mapping common patterns++### Checkpointing++```python+# langgraph+from langgraph.checkpoint.sqlite import SqliteSaver+graph.compile(checkpointer=SqliteSaver.from_conn_string(":memory:"))++# CubePi+from cubepi.checkpointer import SQLiteCheckpointer+async with SQLiteCheckpointer("agent.db") as cp:+ agent = Agent(..., checkpointer=cp, thread_id="conv-1")+```++CubePi's append-only model is O(1) per message, regardless of+conversation length. langgraph saves full snapshots, which scales+linearly with history.++### Streaming++```python+# langgraph+for chunk in app.stream(state, stream_mode="messages"):+ if chunk["event"] == "on_chat_model_stream":+ print(chunk["data"]["chunk"].content, end="")++# CubePi+def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="")++agent.subscribe(on_event)+await agent.prompt("…")+```++One subscriber, one stream — no mode flag.++### Interrupting / human-in-the-loop++```python+# langgraph+graph.compile(interrupt_before=["tools"])++# CubePi+class HumanApproval(Middleware):+ async def before_tool_call(self, ctx, *, signal=None):+ approved = await ask_human(f"Run {ctx.tool_call.name}({ctx.args})?")+ if not approved:+ return BeforeToolCallResult(block=True, reason="rejected")+ return None+```++Imperative interrupts via middleware. You decide per call instead of+configuring graph-level interrupt points.++### Branching++```python+# langgraph+graph.add_conditional_edges("llm", lambda s: "tools" if s["messages"][-1].tool_calls else "summary")+graph.add_node("summary", summarize)+graph.add_edge("summary", END)+```++```python+# CubePi+class SummariseAtEnd(Middleware):+ async def should_stop_after_turn(self, ctx) -> bool:+ msg = ctx.message+ if not any(isinstance(c, ToolCall) for c in msg.content):+ # No more tool calls; we're done. Inject a summary turn first.+ ...+ return True+ return False+```++There's no built-in branching primitive; flow control happens through+`should_stop_after_turn` and `after_model_response`.++## What langgraph does that CubePi doesn't (yet)++- **Multi-agent supervisor patterns.** No first-class "agents+ spawning agents" abstraction. You can build it by running multiple+ `Agent` instances with shared tools.+- **Visual graph rendering.** No `app.get_graph().draw_mermaid()`+ equivalent. CubePi's flow is linear so the picture would be a single+ line anyway.+- **Time travel / fork** at arbitrary checkpoints. The Postgres schema+ has fork columns but no API surface in v0.4.+- **First-party UI for traces.** CubePi doesn't render its own trace+ visualizer the way LangSmith / Langfuse do; instead it emits+ vendor-neutral OpenTelemetry — point any OTLP backend+ (LangSmith's OTel endpoint, Langfuse v3, Jaeger, Tempo,+ Honeycomb, Datadog, …) at it via+ `Tracer(exporters=[OTLPSpanExporter(...)])`. See+ [Tracing → OTLP & Backends](../guides/tracing/otlp).++## What CubePi does that langgraph doesn't++- **Native OpenTelemetry tracing** — `Tracer` + `Meter` emit OTel+ spans + GenAI-semconv attributes out of the box, ingestible by+ any OTLP backend. See [Tracing → Overview](../guides/tracing/overview).+- **Native async-first** — every entry point is async. No+ `app.invoke` vs. `app.ainvoke` split.+- **Append-only persistence** — O(1) DB writes, JSONB-queryable+ messages.+- **3 core deps** vs. langchain-core + langgraph-sdk + transitives.+- **Streaming-realistic test provider** (`FauxProvider`) ships in the+ box.+- **MCP loaders** for HTTP + stdio transports.++## Porting checklist++1. Replace `StateGraph` construction with a single `Agent(...)` call.+2. Move `@tool`-decorated functions to `AgentTool` instances (Pydantic+ models for params, async execute).+3. Replace `MemorySaver` / `SqliteSaver` / `PostgresSaver` with+ `MemoryCheckpointer` / `SQLiteCheckpointer` / `PostgresCheckpointer`.+4. Replace `stream_mode` callbacks with `agent.subscribe(...)`.+5. Convert custom nodes that did message transforms → `Middleware`+ hooks.+6. Convert `interrupt_before/after` → `before_tool_call` /+ `after_model_response` middleware.+7. If you had a `summary` or `route` node — fold it into+ `after_model_response` with `decision="stop"` or `"loop_to_model"`.++## See also++- [Core Concepts](../getting-started/core-concepts) — the building+ blocks you're mapping to.+- [Middleware → Composition](../guides/middleware/composition) — where+ flow-control logic lives.+- [Checkpointing](../guides/checkpointing/sqlite) — the new+ persistence story.diff --git a/website/versioned_docs/version-0.7/recipes/ask-user-form.md b/website/versioned_docs/version-0.7/recipes/ask-user-form.mdnew file mode 100644index 0000000..2da7bbf--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/ask-user-form.md@@ -0,0 +1,184 @@+---+title: Multi-question Form via ask_user+description: "Build a multi-question form with CubePi's ask_user HITL tool for structured user input."+---++# Recipe: Multi-question Form via `ask_user`++Use case: the agent needs a structured answer from the user before it can+proceed — a configuration wizard, a preference selector, a feature toggle.++## Step 1: Register the tool++```python+from cubepi.agent.agent import Agent+from cubepi.hitl import InMemoryChannel, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=...,+ system_prompt=(+ "When you need the user to choose among options, use the ask_user tool. "+ "For free-form clarification questions, just end your turn with text — "+ "the user's next message will be your answer."+ ),+ tools=[ask_user_tool(channel)],+ channel=channel,+)+```++The `ask_user` tool is registered like any other tool. Its+`execution_mode="sequential"` makes the tool batch run one-by-one —+the HITL pause can't overlap with parallel tool execution.++## Step 2: Host renders the form++The model invokes `ask_user` with a list of question objects. The host+receives an `AskRequest` payload on the channel:++```python+async def host():+ async for req in channel.subscribe():+ if req.payload.kind == "ask":+ answers = {}+ for q in req.payload.questions:+ if q.options is None:+ # Free-text question+ answers[q.key] = await my_ui.text_input(q.prompt)+ elif q.multi_select:+ answers[q.key] = await my_ui.checkbox_group(+ q.prompt, [(o.label, o.value) for o in q.options],+ )+ else:+ answers[q.key] = await my_ui.radio_group(+ q.prompt,+ [(o.label, o.value) for o in q.options],+ allow_input_indexes=[+ i for i, o in enumerate(q.options) if o.allow_input+ ],+ )+ await channel.answer(req.question_id, answers)+```++## What the model sees as tool parameters++The model can pass questions that mix free-text, single-select, and+multi-select fields in a single call:++```json+{+ "questions": [+ {+ "key": "project_type",+ "prompt": "What kind of project?",+ "options": [+ {"label": "Web app", "value": "web"},+ {"label": "CLI tool", "value": "cli"},+ {"label": "Library", "value": "lib"}+ ]+ },+ {+ "key": "framework",+ "prompt": "Which framework?",+ "options": [+ {"label": "React", "value": "react"},+ {"label": "Vue", "value": "vue"},+ {"label": "Other", "value": "other", "allow_input": true}+ ]+ },+ {+ "key": "features",+ "prompt": "Which features do you need?",+ "multi_select": true,+ "options": [+ {"label": "Authentication", "value": "auth"},+ {"label": "Payments", "value": "payments"},+ {"label": "File uploads", "value": "uploads"}+ ]+ },+ {+ "key": "project_name",+ "prompt": "What should we call this project?"+ }+ ]+}+```++## Answer shape++The host answers with a dict mapping `key → value`:++```python+# Example answer for the above form:+{+ "project_type": "web",+ "framework": "svelte", # user chose "Other" and typed "svelte"+ "features": ["auth", "uploads"],+ "project_name": "my-saas"+}+```++The answer is stuffed into the tool result as+`details["hitl"]["answers"]`. The model receives a human-readable summary+in the text content and can reference the dict for structured consumption.++## Cancel and timeout++If the host cancels via `channel.cancel(qid, reason)`:++```python+await channel.cancel(req.question_id, reason="user closed the form")+```++The tool surfaces an error result to the model:++```+tool_result.is_error = True+tool_result.details["hitl"]["outcome"] = "cancelled"+tool_result.details["hitl"]["reason"] = "user closed the form"+```++If the timeout expires:++```+tool_result.details["hitl"]["outcome"] = "timed_out"+tool_result.details["hitl"]["seconds"] = 30.0+```++In both cases the model sees a clean error result and can react+accordingly — ask again, fall back to a default, or report to the user.++## In-process example (full runnable snippet)++```python+import asyncio+from cubepi.agent.agent import Agent+from cubepi.hitl import InMemoryChannel, ask_user_tool++channel = InMemoryChannel()++agent = Agent(+ model=...,+ tools=[ask_user_tool(channel)],+ channel=channel,+)++async def host():+ async for req in channel.subscribe():+ if req.payload.kind == "ask":+ answers = {+ q.key: q.options[0].value if q.options else ""+ for q in req.payload.questions+ }+ await channel.answer(req.question_id, answers)++async def main():+ host_task = asyncio.create_task(host())+ try:+ await agent.prompt("Scaffold a new project.")+ finally:+ host_task.cancel()++asyncio.run(main())+```diff --git a/website/versioned_docs/version-0.7/recipes/multi-provider-failover.md b/website/versioned_docs/version-0.7/recipes/multi-provider-failover.mdnew file mode 100644index 0000000..bc01a58--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/multi-provider-failover.md@@ -0,0 +1,264 @@+---+title: Multi-Provider Failover+description: "Configure multi-provider failover in CubePi for resilience — automatic fallback between providers."+---++# Recipe: Multi-Provider Failover++When Anthropic is rate-limited or down, fail over to OpenAI without+crashing the agent. We'll wrap both providers behind a single+`Provider` adapter that does its own retry/failover logic.++**Time to run:** 10 minutes.+**Deps:** `cubepi`, `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`.++## The wrapper provider++```python title="failover.py"+import asyncio+import logging+import time+from typing import Sequence++from cubepi.providers.base import (+ AssistantMessage,+ BaseProvider,+ BoundModel,+ Message,+ MessageStream,+ Model,+ StreamEvent,+ StreamOptions,+ ToolDefinition,+ Usage,+)+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider++log = logging.getLogger(__name__)+++class FailoverProvider(BaseProvider):+ """Try providers in order; fall over on construction or first-event errors.++ Built-in providers swallow API/network errors and surface them as+ `StreamEvent(type="error")` on the returned stream — never as exceptions+ out of `provider.stream()`. So we peek at the first event from each+ inner stream and only commit to it once we see a non-error event.++ Limitation: errors that arrive *after* the first event (e.g. mid-stream+ rate limit, server disconnect) are forwarded to the agent as-is.+ Fully replaying a half-streamed turn against a fallback provider would+ require buffering the whole turn — out of scope here.+ """++ def __init__(self, primary: BoundModel, *fallbacks: BoundModel) -> None:+ super().__init__(provider_id=primary.spec.provider_id)+ self._chain: list[BoundModel] = [primary, *fallbacks]++ async def stream(+ self,+ model: Model,+ messages: list[Message],+ *,+ system_prompt: str = "",+ tools: list[ToolDefinition] | None = None,+ options: StreamOptions | None = None,+ ) -> MessageStream:+ last_error: str | None = None++ for bound_model in self._chain:+ provider = bound_model.provider+ mapped_model = bound_model.spec+ # Construction-time failures (rare — most stay inside the producer task).+ try:+ inner = await provider.stream(+ mapped_model,+ messages,+ system_prompt=system_prompt,+ tools=tools,+ options=options,+ )+ except Exception as e:+ log.warning("provider %s failed at construction: %s", mapped_model.provider_id, e)+ last_error = repr(e)+ continue++ # Peek at the first event to learn whether the stream is healthy.+ iterator = inner.__aiter__()+ try:+ first = await iterator.__anext__()+ except StopAsyncIteration:+ last_error = "stream ended before producing any events"+ continue++ if first.type == "error":+ log.warning("provider %s errored on first event: %s",+ mapped_model.provider_id, first.error_message)+ last_error = first.error_message or "stream error"+ continue++ # Healthy — commit to this provider. Forward `first` plus the rest+ # through a fresh outer MessageStream so the caller sees a complete+ # stream starting at the start event.+ outer = MessageStream()++ async def _forward(first_event=first, src=iterator, src_stream=inner):+ try:+ outer.push(first_event)+ async for ev in src:+ outer.push(ev)+ final = await src_stream.result()+ outer.set_result(final)+ except Exception as exc:+ fallback_msg = AssistantMessage(+ content=[],+ stop_reason="error",+ error_message=str(exc),+ usage=Usage(),+ timestamp=time.time(),+ )+ outer.push(StreamEvent(type="error", error_message=str(exc)))+ outer.set_result(fallback_msg)++ outer.attach_task(asyncio.create_task(_forward()))+ return outer++ raise RuntimeError(f"all providers exhausted; last error: {last_error!r}")+```++## Use it++```python title="main.py"+import asyncio+import os++from cubepi import Agent+from cubepi.providers.anthropic import AnthropicProvider+from cubepi.providers.openai import OpenAIProvider+from failover import FailoverProvider+++async def main():+ anthropic = AnthropicProvider(+ provider_id="anthropic",+ api_key=os.environ["ANTHROPIC_API_KEY"],+ )+ openai = OpenAIProvider(+ provider_id="openai",+ api_key=os.environ["OPENAI_API_KEY"],+ )+ failover = FailoverProvider(+ anthropic.model("claude-sonnet-4-5-20250929"),+ openai.model("gpt-5"),+ )++ # The model passed here is overridden inside FailoverProvider; pass any+ # placeholder. We use the primary's so usage tracking labels match the+ # happy path.+ agent = Agent(+ model=failover.model("claude-sonnet-4-5-20250929"),+ system_prompt="You answer concisely.",+ )+ agent.subscribe(lambda e, s=None: None)+ await agent.prompt("Capital of Mongolia?")+ last = agent.state.messages[-1]+ print(last.content[0].text)+++asyncio.run(main())+```++## What about smarter failover policies?++The example above falls back on **any** error event. That's fine for+`RateLimitError`, `APIConnectionError`, or 5xx — but arguably wrong for+`BadRequestError` (your code is wrong; the next provider will fail the+same way).++The first-event `error_message` comes from `str(exc)` on the+underlying SDK exception. Filter on substrings, or — better — wrap each+provider's `_produce` to tag the error category:++```python+NON_RETRYABLE_HINTS = ("bad request", "invalid_request_error", "401", "403")++if first.type == "error":+ msg = (first.error_message or "").lower()+ if any(h in msg for h in NON_RETRYABLE_HINTS):+ raise RuntimeError(f"non-retryable error from {mapped_model.provider_id}: {msg}")+ last_error = first.error_message+ continue+```++A more robust approach is to fork the built-in providers and re-raise+specific SDK exceptions from `_produce` so they reach `provider.stream()`+as real Python exceptions — but that's a larger change against+CubePi itself.++## Adding circuit breaking++Don't keep retrying a provider that's clearly down. A simple counter:++```python+import time++class CircuitBreaker:+ def __init__(self, failure_threshold: int = 3, recovery_seconds: float = 60) -> None:+ self._failures = 0+ self._opened_at: float | None = None+ self._threshold = failure_threshold+ self._recovery = recovery_seconds++ def can_attempt(self) -> bool:+ if self._opened_at and time.monotonic() - self._opened_at < self._recovery:+ return False+ if self._opened_at:+ self._opened_at = None # half-open+ return True++ def record_failure(self) -> None:+ self._failures += 1+ if self._failures >= self._threshold:+ self._opened_at = time.monotonic()+ self._failures = 0++ def record_success(self) -> None:+ self._failures = 0+```++Hold one `CircuitBreaker` per provider in the `FailoverProvider`,+skip if `can_attempt()` is False.++## Per-tool failover doesn't apply++This recipe handles **provider** failures. Tool failures are different+— see [Middleware → Retries](../guides/middleware/examples#retries-with-backoff)+for that pattern.++## Common pitfalls++- **Different tool schemas across providers** — Both built-in+ providers accept the same `ToolDefinition`, but extra-body+ customisations (e.g. OpenAI `parallel_tool_calls=False`) won't carry+ to Anthropic. Keep cross-provider behaviour in+ [`transform_context`](../guides/middleware/hooks#transform_context),+ not in `extra_body`.+- **Different cost** — Failover from Anthropic to OpenAI changes+ per-token cost. Track which provider answered (via `on_response` or+ `AssistantMessage.provider_id`) and bill accordingly.+- **Streaming consistency** — The wrapper forwards events through a+ fresh `MessageStream`, so consumers see the same `StreamEvent` shape+ regardless of which provider answered. The original `start` event+ comes from the inner provider unchanged.+- **Mid-stream errors aren't recovered** — Once we've seen a healthy+ first event, the wrapper commits to that provider. If it errors+ halfway through a long response, the agent sees the error. Full+ mid-stream replay would require buffering — out of scope here.++## See also++- [Providers / Anthropic](../guides/providers/anthropic) and+ [OpenAI](../guides/providers/openai) — provider-specific details.+- [Writing a Custom Provider](../guides/providers/custom) — the same+ Protocol used by this wrapper.diff --git a/website/versioned_docs/version-0.7/recipes/persistent-chat.md b/website/versioned_docs/version-0.7/recipes/persistent-chat.mdnew file mode 100644index 0000000..b788bf3--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/persistent-chat.md@@ -0,0 +1,175 @@+---+title: Persistent Chat+description: "Build a persistent chat application with CubePi and SQLiteCheckpointer."+---++# Recipe: Persistent Chat (SQLite)++A REPL chat that survives restarts. Conversation history is kept in a+SQLite file; each user gets a `thread_id`.++**Time to run:** 5 minutes.+**Deps:** `cubepi[sqlite]`, an `ANTHROPIC_API_KEY`.++## The script++```python title="chat.py"+import asyncio+import os+import sys++from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++async def main(thread_id: str):+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])++ async with SQLiteCheckpointer("chat.db") as cp:+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a concise, friendly assistant.",+ checkpointer=cp,+ thread_id=thread_id,+ )++ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "agent_end":+ print()++ agent.subscribe(on_event)++ print(f"chatting on thread {thread_id!r}. Ctrl-D to quit.\n")+ loop = asyncio.get_event_loop()+ while True:+ try:+ user_input = await loop.run_in_executor(None, input, "you> ")+ except EOFError:+ print()+ return+ if not user_input.strip():+ continue+ print("ai > ", end="", flush=True)+ await agent.prompt(user_input)+++if __name__ == "__main__":+ asyncio.run(main(thread_id=sys.argv[1] if len(sys.argv) > 1 else "default"))+```++Run:++```bash+pip install "cubepi[sqlite]"+export ANTHROPIC_API_KEY=sk-…+python chat.py alice+# Have a chat, then Ctrl-D.++python chat.py alice+# History is restored. Ask "what did I just tell you?" — the model+# remembers the previous session.++python chat.py bob+# Different thread, clean slate.+```++## What's going on++- **First `prompt()` per process loads history.** CubePi checks the+ checkpointer once at the start of the first prompt, restores+ `agent.state.messages`, and proceeds.+- **Each `message_end` appends to the database.** No batching, no+ lossy buffering. If the process crashes mid-stream, the next run+ picks up at the last committed message.+- **The system prompt isn't persisted** — it's part of agent+ construction, not state. Keep it in your code (or in env config).++## Multi-user routing++In a web service, derive `thread_id` from the authenticated user:++```python+agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id=f"user-{user_id}",+)+```++You can serve thousands of users from one `SQLiteCheckpointer` —+threads are isolated by `thread_id`.++## Pruning a thread++There's no built-in "delete thread" API by design — checkpointers are+append-only. To prune, drop rows by `thread_id` directly:++```bash+sqlite3 chat.db "DELETE FROM messages WHERE thread_id='alice'; DELETE FROM thread_extra WHERE thread_id='alice'"+```++Or implement a small admin tool that does the SQL on demand.++## Sliding the context window++After a long conversation, the model's context gets expensive. Add a+[`SlidingWindow`](../guides/middleware/examples#sliding-window-truncation)+middleware:++```python+from cubepi import Middleware++class SlidingWindow(Middleware):+ def __init__(self, n: int) -> None:+ self.n = n++ async def transform_context(self, messages, *, ctx, signal=None):+ return messages[-self.n:] if len(messages) > self.n else messages+++agent = Agent(+ model=model,+ checkpointer=cp,+ thread_id=thread_id,+ middleware=[SlidingWindow(40)],+)+```++The DB keeps every message; the model only sees the last 40. The+user-visible history (e.g. for a chat UI rendering past turns) stays+complete.++## Switching to Postgres++Same code, different checkpointer:++```python+from cubepi.checkpointer import PostgresCheckpointer++async with PostgresCheckpointer("postgresql://…") as cp:+ agent = Agent(model=…, checkpointer=cp, thread_id=…)+```++Postgres is the right choice for multi-instance services or many+concurrent users — see [Postgres + FastAPI](./postgres-fastapi).++## Common pitfalls++- **`async with` forgotten** — Without it, the SQLite connection is+ never opened. You get `AssertionError`. Wrap.+- **Multiple processes writing the same `thread_id`** — Interleaved+ history. One agent per thread, or move to Postgres.+- **`chat.db` in `/tmp`** — Some OSes wipe `/tmp` on reboot. Use+ `~/.local/share/myapp/chat.db` or similar for user data.++## See also++- [Multi-turn Conversations](../guides/agents/multi-turn) — `steer`,+ `follow_up`, `resume`.+- [SQLite Checkpointing](../guides/checkpointing/sqlite) — the backend+ in detail.+- [Resumable Long Tasks](./resumable-tasks) — when crashes happen+ mid-tool, not just between turns.diff --git a/website/versioned_docs/version-0.7/recipes/postgres-fastapi.md b/website/versioned_docs/version-0.7/recipes/postgres-fastapi.mdnew file mode 100644index 0000000..0eee2b8--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/postgres-fastapi.md@@ -0,0 +1,255 @@+---+title: Postgres + FastAPI Service+description: "Deploy a FastAPI-backed CubePi agent with PostgresCheckpointer for production."+---++# Recipe: Postgres + FastAPI Service++A production-shaped HTTP service that fronts a CubePi agent: FastAPI+for routing, server-sent events for streaming, a shared+`PostgresCheckpointer` for persistence, and `thread_id` derived from+the authenticated user.++**Time to run:** 30 minutes.+**Deps:** `cubepi[postgres]`, `fastapi`, `uvicorn[standard]`,+`sse-starlette`, a running Postgres with the CubePi schema applied.++## Schema first++Before the service starts, run the CubePi schema migration. The+quickest way for this recipe:++```bash+psql "$DATABASE_URL" <<'SQL'+CREATE TABLE cubepi_threads (+ thread_id TEXT PRIMARY KEY,+ parent_thread_id TEXT REFERENCES cubepi_threads(thread_id),+ forked_at_seq BIGINT,+ extra JSONB NOT NULL DEFAULT '{}'::jsonb,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now()+);++CREATE TABLE cubepi_messages (+ thread_id TEXT NOT NULL REFERENCES cubepi_threads(thread_id) ON DELETE CASCADE,+ seq BIGINT NOT NULL,+ role TEXT NOT NULL,+ metadata JSONB NOT NULL DEFAULT '{}'::jsonb,+ payload BYTEA NOT NULL,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (thread_id, seq)+) PARTITION BY HASH (thread_id);++-- 64 hash partitions+DO $$+BEGIN+ FOR i IN 0..63 LOOP+ EXECUTE format(+ 'CREATE TABLE cubepi_messages_p%s PARTITION OF cubepi_messages FOR VALUES WITH (MODULUS 64, REMAINDER %s)',+ i, i+ );+ END LOOP;+END$$;++CREATE INDEX ix_cubepi_messages_metadata_gin+ON cubepi_messages USING gin (metadata jsonb_path_ops);++CREATE TABLE cubepi_schema_version (version INT PRIMARY KEY);+INSERT INTO cubepi_schema_version VALUES (1);+SQL+```++For a real deployment, generate this via Alembic — see+[Postgres Checkpointing → Bootstrapping via Alembic](../guides/checkpointing/postgres#bootstrapping-via-alembic).++## The service++```python title="service.py"+import asyncio+import os+from contextlib import asynccontextmanager+from typing import AsyncIterator++from fastapi import FastAPI, Depends, HTTPException+from pydantic import BaseModel+from sse_starlette.sse import EventSourceResponse++from cubepi import Agent, Model+from cubepi.checkpointer import PostgresCheckpointer+from cubepi.providers.anthropic import AnthropicProvider+++# --- App lifecycle ------------------------------------------------------++_provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+_checkpointer: PostgresCheckpointer | None = None+++@asynccontextmanager+async def lifespan(app: FastAPI):+ global _checkpointer+ _checkpointer = await PostgresCheckpointer(+ os.environ["DATABASE_URL"],+ min_pool_size=2,+ max_pool_size=20,+ ).__aenter__()+ yield+ await _checkpointer.__aexit__(None, None, None)+++app = FastAPI(lifespan=lifespan)+++# --- Auth (stub — replace with your real auth) -------------------------++async def current_user_id() -> str:+ # In production: decode JWT, look up session, etc.+ return "demo-user"+++# --- Routes -------------------------------------------------------------++class PromptBody(BaseModel):+ text: str++[email protected]("/chat/{conversation_id}/messages")+async def post_message(+ conversation_id: str,+ body: PromptBody,+ user_id: str = Depends(current_user_id),+):+ thread_id = f"{user_id}:{conversation_id}"++ async def event_generator() -> AsyncIterator[dict]:+ agent = Agent(+ model=_provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a helpful assistant.",+ checkpointer=_checkpointer,+ thread_id=thread_id,+ )++ queue: asyncio.Queue = asyncio.Queue()+ agent.subscribe(lambda e, s=None: queue.put_nowait(e))++ async def run():+ try:+ await agent.prompt(body.text)+ finally:+ queue.put_nowait(None) # sentinel++ task = asyncio.create_task(run())++ try:+ while True:+ event = await queue.get()+ if event is None:+ break+ # Emit a small subset to the client.+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ yield {"event": "delta", "data": event.stream_event.delta}+ elif event.type == "tool_execution_start":+ yield {"event": "tool_start", "data": event.tool_name}+ elif event.type == "agent_end":+ yield {"event": "done", "data": ""}+ finally:+ await task++ return EventSourceResponse(event_generator())++[email protected]("/chat/{conversation_id}/history")+async def get_history(+ conversation_id: str,+ user_id: str = Depends(current_user_id),+):+ thread_id = f"{user_id}:{conversation_id}"+ data = await _checkpointer.load(thread_id)+ if data is None:+ return {"messages": []}+ return {+ "messages": [m.model_dump(mode="json") for m in data.messages],+ }+```++Run:++```bash+pip install "cubepi[postgres]" fastapi "uvicorn[standard]" sse-starlette+export DATABASE_URL=postgresql://user:pass@localhost/cubepi+export ANTHROPIC_API_KEY=sk-…+uvicorn service:app --reload --port 8000+```++Test:++```bash+curl -N -X POST http://localhost:8000/chat/conv1/messages \+ -H "content-type: application/json" \+ -d '{"text":"hi"}'+# event: delta+# data: Hello+# event: delta+# data: !+# event: done+```++## Design notes++- **One `PostgresCheckpointer` per process, shared across requests.**+ It holds a connection pool; opening one per request would defeat+ the pool.+- **One `Agent` per request.** Agents own per-conversation state+ (steering queues, listeners). Don't reuse them.+- **`thread_id = f"{user_id}:{conversation_id}"`** — user isolation by+ prefix. The agent reads/writes only its own thread.+- **SSE for streaming.** Each text delta goes to the client as a+ separate event. Tool starts get their own event type — clients can+ render a "thinking" indicator without rebuilding event handling.+- **No load balancer affinity needed.** Because state is in Postgres,+ any service instance can pick up any conversation.++## Concurrency on the same thread++If a user double-clicks send, two `POST` requests arrive simultaneously.+Both create an `Agent` bound to the same `thread_id`. The Postgres+advisory lock serialises their appends, but the **in-memory** states+diverge — the second request's agent might not see the first's+in-progress message in its `agent.state.messages`.++For most chat UIs this is fine (the client controls send timing).+If you need strict ordering, add an application-layer mutex+(`asyncio.Lock` keyed by `thread_id`) or queue.++## Production hardening checklist++- **Auth:** Replace `current_user_id()` with real JWT / session+ validation.+- **Rate limiting:** Add a [`RateLimitMiddleware`](../guides/middleware/examples#rate-limiting)+ to the agent constructor, keyed by `user_id`.+- **Cost tracking:** Subscribe to `agent_end`, sum `usage` on each+ `AssistantMessage`, write to a billing table.+- **Observability:** Use `on_response` to capture `anthropic-*` rate+ headers; export to Prometheus.+- **Backups:** Postgres native — `pg_dump`, point-in-time recovery.+- **Graceful shutdown:** uvicorn's lifespan handler closes the pool;+ add `signal.signal(SIGTERM, ...)` if you have other resources.++## Common pitfalls++- **CubepiSchemaUninitialized at startup** — Your migrations didn't+ run. Apply the schema first.+- **Connection pool exhaustion** — Default `max_pool_size=10`. Bump+ it if your service has more concurrent agents than that.+- **SSE behind a load balancer** — Some LBs buffer SSE. Disable+ buffering (`X-Accel-Buffering: no` for nginx).+- **Long requests timing out** — Tool-heavy agents can run minutes.+ Set generous proxy timeouts and uvicorn `--timeout-keep-alive 600`.++## See also++- [Postgres Checkpointing](../guides/checkpointing/postgres) — the+ backend in depth.+- [Persistent Chat](./persistent-chat) — the same flow with SQLite.+- [Multi-Provider Failover](./multi-provider-failover) — combine with+ this service for resilience.diff --git a/website/versioned_docs/version-0.7/recipes/resumable-tasks.md b/website/versioned_docs/version-0.7/recipes/resumable-tasks.mdnew file mode 100644index 0000000..7887b8c--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/resumable-tasks.md@@ -0,0 +1,229 @@+---+title: Resumable Long Tasks+description: "Build crash-resilient long-running tasks with CubePi checkpointing and recovery."+---++# Recipe: Resumable Long Tasks++When an agent is mid-flight through a long-running operation (a series+of tool calls, a multi-turn reasoning session) and the process dies,+you want to come back and pick up where it left off — not start over.+CubePi's append-only checkpointing plus `agent.resume()` makes this+trivial *between turns*; for resumption *mid-tool*, you need a little+more care.++**Time to run:** 15 minutes.+**Deps:** `cubepi[sqlite]`, an `ANTHROPIC_API_KEY`.++## The pattern++There are three crash points to think about:++1. **Between turns** — The model has answered, no tools to run, the+ loop is between iterations. `resume()` re-invokes the model. *Free+ with the checkpointer.*+2. **After tool results, before model call** — Tool results are+ persisted. `resume()` sees the last message is a `ToolResultMessage`+ and re-invokes the model. *Free with the checkpointer.*+3. **Mid-tool** — The tool started but didn't finish. Nothing is+ persisted yet (CubePi only persists messages). You need+ tool-internal idempotency. *Requires care.*++This recipe focuses on case 3.++## Idempotent tools with external state++The pattern: each tool action has a deterministic, idempotent key.+Before doing the work, check whether it's been done.++```python title="tools.py"+import os+import json+from pathlib import Path++from pydantic import BaseModel+from cubepi import AgentTool, AgentToolResult, TextContent+++# Simple file-backed job store; replace with Redis / Postgres in prod.+JOB_DIR = Path(os.environ.get("JOB_DIR", "/tmp/cubepi-jobs"))+JOB_DIR.mkdir(parents=True, exist_ok=True)+++class TranscodeParams(BaseModel):+ source_path: str+ output_path: str+++async def transcode_video(tool_call_id, params: TranscodeParams, *, signal=None, on_update=None):+ job_key = f"transcode:{params.source_path}->{params.output_path}"+ job_file = JOB_DIR / f"{job_key.replace('/', '_')}.json"++ if job_file.exists():+ # Already done in a previous run.+ state = json.loads(job_file.read_text())+ return AgentToolResult(+ content=[TextContent(text=f"Already transcoded to {state['output_path']}.")],+ details=state,+ )++ # Do the actual work (long-running, expensive).+ # Use signal to abort cleanly if cancelled.+ output = await run_ffmpeg(params.source_path, params.output_path, signal=signal)++ # Commit the job-done marker AFTER the work succeeds.+ job_file.write_text(json.dumps({"output_path": output}))++ return AgentToolResult(+ content=[TextContent(text=f"Transcoded to {output}.")],+ details={"output_path": output},+ )+++transcode_tool = AgentTool(+ name="transcode_video",+ description="Transcode a video file. Idempotent — safe to retry.",+ parameters=TranscodeParams,+ execute=transcode_video,+ execution_mode="sequential", # one transcode at a time+)+```++Now if the process dies during `run_ffmpeg`, the next agent run sees+`job_file.exists() == False`, redoes the work, and only writes the+marker on success. If the process dies *after* the marker was+written, the next run sees the marker, returns the cached result+immediately, and the agent continues as if it had just finished.++## Resuming the agent++```python title="resume.py"+import asyncio+import os+import sys++from cubepi import Agent, Model+from cubepi.checkpointer import SQLiteCheckpointer+from cubepi.providers.anthropic import AnthropicProvider++from tools import transcode_tool # the wrapped AgentTool from above+++async def main(thread_id: str, initial_prompt: str | None):+ async with SQLiteCheckpointer("jobs.db") as cp:+ agent = Agent(+ model=AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"]).model("claude-sonnet-4-5-20250929"),+ system_prompt="You orchestrate video transcoding jobs.",+ tools=[transcode_tool],+ checkpointer=cp,+ thread_id=thread_id,+ )+ agent.subscribe(lambda e, s=None: None)++ if initial_prompt:+ # Fresh run. prompt() auto-loads history on first call before+ # appending the new user message.+ await agent.prompt(initial_prompt)+ else:+ # Resume. agent.resume() does NOT auto-load — only prompt() does.+ # Hydrate the agent state manually first.+ data = await cp.load(thread_id)+ if data is None:+ raise RuntimeError(f"No saved state for thread {thread_id!r}")+ agent.state.messages = list(data.messages)+ # `extra` is restored too; it's private on Agent, so use the+ # checkpointer's view if your middleware reads it.++ # Resume picks up from the last persisted message:+ # ToolResultMessage / UserMessage → re-invokes the model+ # AssistantMessage with no queued steer/follow_up → raises+ await agent.resume()+++if __name__ == "__main__":+ thread_id = sys.argv[1]+ initial = sys.argv[2] if len(sys.argv) > 2 else None+ asyncio.run(main(thread_id, initial))+```++Workflow:++```bash+# Start a job:+python resume.py job-1 "Transcode /videos/a.mov to /out/a.mp4 and /videos/b.mov to /out/b.mp4"++# Kill it mid-flight: Ctrl-C.++# Resume — agent picks up from the last persisted message:+python resume.py job-1+```++## The three resume scenarios in code++```python+async def smart_resume(agent, cp, thread_id):+ # resume() doesn't auto-load — hydrate the agent first if its state is empty.+ if not agent.state.messages:+ data = await cp.load(thread_id)+ if data is None or not data.messages:+ return False # nothing to resume from+ agent.state.messages = list(data.messages)++ last = agent.state.messages[-1]+ last_role = type(last).__name__++ if last_role == "AssistantMessage":+ # Either the run finished naturally, or it died right after a+ # turn ended. resume() raises unless there's queued steering.+ # Easiest path: ask the user what's next.+ return False++ # Last is UserMessage or ToolResultMessage — safe to resume.+ await agent.resume()+ return True+```++## Persistence + abort++`agent.abort()` triggers a clean teardown that emits `agent_end`. The+last *fully persisted* message is whatever made it through+`message_end`. Aborts during a tool's execution don't persist the+tool result (the tool didn't return), so `resume()` will re-dispatch+the model with the last `AssistantMessage` containing the unfinished+`ToolCall`. The model will usually re-issue the call — your+idempotency guards handle the rest.++## What about persisting partial tool state?++CubePi doesn't expose a "persist a partial tool result" API. The+intended pattern is: keep partial state in the tool's own+external store (filesystem, Redis, S3), keyed deterministically by the+tool args. That's what `transcode_video` above does with `JOB_DIR`.++## Common pitfalls++- **Non-idempotent tools** — Without deterministic keys, retries can+ charge a credit card twice or send a duplicate email. Always wrap+ external side-effects in an idempotency key.+- **Job markers in `/tmp`** — Cleared on reboot. Use a real+ persistence layer for production jobs.+- **`resume()` after an assistant message with no queue** — Raises.+ Either prompt the user for the next message or call `prompt()`+ fresh.+- **`resume()` on a fresh agent** — Raises `No messages to continue+ from`. `resume()` does not auto-load from the checkpointer; only+ `prompt()` does. Hydrate manually with `agent.state.messages =+ (await cp.load(thread_id)).messages` first.+- **Forgetting the signal check inside the tool** — A long+ `await asyncio.sleep(...)` or a `for ... in stream` that ignores+ `signal.is_set()` won't honour `abort`. Drop a check inside any+ hot loop.++## See also++- [Multi-turn → `resume()`](../guides/agents/multi-turn#resume--continue-from-the-last-message)+ — full semantics.+- [Persistent Chat](./persistent-chat) — the simpler restartable+ scenario.+- [SQLite Checkpointing](../guides/checkpointing/sqlite) — what's+ persisted, when.diff --git a/website/versioned_docs/version-0.7/recipes/sandbox-confirm.md b/website/versioned_docs/version-0.7/recipes/sandbox-confirm.mdnew file mode 100644index 0000000..01cbb3e--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/sandbox-confirm.md@@ -0,0 +1,140 @@+---+title: Sandbox Confirm with ApprovalPolicyMiddleware+description: "Implement sandbox tool confirmation with CubePi's ApprovalPolicyMiddleware — auto-allow, deny, or human-confirm."+---++# Recipe: Sandbox Confirm with `ApprovalPolicyMiddleware`++Use case: a web service where every tool call goes through a rule engine that+classifies it as auto-allow, hard-deny, or human-confirm.++## Step 1: Define your policy function++The policy receives a `BeforeToolCallContext` and returns an+`ApprovalDecision` — `Approve()`, `Deny(reason)`, or `AskUser(...)`.++```python+from cubepi.hitl import Approve, AskUser, Deny++# Mock rule engine — replace with your actual policy catalog.+def classify_command(cmd: str) -> tuple[str, str | None]:+ """(tier, reason) — "allow", "block", or "confirm"."""+ if cmd.startswith(("ls", "cat", "head", "grep", "find")):+ return ("allow", None)+ if "rm -rf /" in cmd or cmd.startswith("dd"):+ return ("block", "destructive I/O")+ return ("confirm", "needs human review")+++def sandbox_policy(ctx):+ cmd = ctx.args.cmd # ctx.args is the validated pydantic model+ tier, reason = classify_command(cmd)++ if tier == "allow":+ return Approve()+ if tier == "block":+ return Deny(reason=reason or "blocked by policy")+ return AskUser(+ timeout_seconds=180,+ details={"matched_rule": tier, "impact": reason or "unknown"},+ )+```++`ctx.args` is the **validated pydantic model**. Access fields as+attributes (`ctx.args.cmd`). The middleware internally converts it to a+dict via `_args_to_dict` for the channel's approve payload, but your+policy receives the typed model.++## Step 2: Wire into the agent++```python+from cubepi.agent.agent import Agent+from cubepi.checkpointer.postgres import PostgresCheckpointer+from cubepi.hitl import ApprovalPolicyMiddleware, CheckpointedChannel++async def main():+ async with PostgresCheckpointer("postgresql://...") as cp:+ channel = CheckpointedChannel(checkpointer=cp, thread_id="session-1")++ agent = Agent(+ model=anthropic.model("claude-sonnet-4-6"),+ system_prompt="You are a helpful assistant with access to a bash shell.",+ tools=[bash_tool],+ middleware=[+ ApprovalPolicyMiddleware(channel, policy=sandbox_policy),+ ],+ channel=channel,+ checkpointer=cp,+ thread_id="session-1",+ )++ await agent.prompt("list files then delete temp logs")+ # Agent runs; when bash is invoked, sandbox_policy decides:+ # ls → Approve() → runs immediately+ # rm /tmp/logs → AskUser() → channel suspends, HitlRequestEvent fires+```++## Step 3: Host handles the pending request++```python+async def host_loop(channel: CheckpointedChannel):+ async for req in channel.subscribe():+ if req.payload.kind == "approve":+ tool_name = req.payload.tool_name+ command = req.payload.args.get("cmd", "")+ details = req.payload.details or {}+ timeout = req.timeout_seconds # seconds for the frontend countdown++ # Render to the frontend: tool_name, command, details["matched_rule"],+ # details["impact"], and a countdown based on timeout.+ human_answer = await my_frontend.show_confirm(+ tool_name=tool_name,+ command=command,+ details=details,+ timeout=timeout,+ )+ # Build an ApproveAnswer from the human's decision.+ from cubepi.hitl import ApproveAnswer+ human_answer = ApproveAnswer(+ decision=ui_response["decision"], # "approve" | "deny" | "edit"+ reason=ui_response.get("reason"), # only for deny+ edited_args=ui_response.get("edited_args"), # only for edit+ )+ await channel.answer(req.question_id, human_answer)+ elif req.payload.kind == "ask":+ await channel.answer(req.question_id, await my_frontend.show_form(req))+ else: # confirm+ await channel.answer(req.question_id, await my_frontend.show_confirm(req))+```++## Decision semantics++| Human chose | Tool outcome | `hitl_trace["decision"]` | Model sees |+|---|---|---|---|+| Approve | Runs with original args | unset (passthrough, no HITL details) | Normal `tool_result` |+| Deny | Blocked | `"human_deny"` | `tool_result.is_error=True` with user's reason |+| Edit | Runs with edited args | `"edit"` + `original_args` / `edited_args` | Normal `tool_result` (from the edited execution) |++Policy decisions (hard-deny without asking the human) carry+`hitl_trace["decision"]="policy_deny"`.++## Timeout behaviour++If the human doesn't respond within `timeout_seconds`, the middleware+translates to `BeforeToolCallResult(block=True, deny_reason="approval_timeout")`.+The model sees `tool_result.is_error=True` with+`details.hitl.decision="timed_out"` and naturally produces a follow-up turn+explaining the timeout.++## Aborting++If the user closes the tab or an admin kills the conversation:++```python+await agent.abort_pending(reason="user closed tab")+```++This closes the conversation cleanly: synthetic deny tool_results are+appended for any unresolved tool calls, a terminal+`AssistantMessage(stop_reason="aborted")` is persisted, and+`AgentAbortedEvent` is emitted. The next `agent.prompt(...)` starts fresh.diff --git a/website/versioned_docs/version-0.7/recipes/weather-agent.md b/website/versioned_docs/version-0.7/recipes/weather-agent.mdnew file mode 100644index 0000000..b13ad5f--- /dev/null+++ b/website/versioned_docs/version-0.7/recipes/weather-agent.md@@ -0,0 +1,160 @@+---+title: Weather Agent+description: "Build a weather agent that fetches live API data with CubePi tools."+---++# Recipe: Weather Agent++A complete, runnable agent that calls a real weather API as a tool.+Demonstrates HTTP-calling tools, error handling, streaming UI, and+cancellation.++**Time to run:** 5 minutes.+**Deps:** `cubepi`, `httpx`, an `ANTHROPIC_API_KEY`.++## The script++```python title="weather_agent.py"+import asyncio+import os++import httpx+from pydantic import BaseModel, Field++from cubepi import Agent, AgentTool, AgentToolResult, Model, TextContent+from cubepi.providers.anthropic import AnthropicProvider+++# --- The tool -----------------------------------------------------------++class GetWeatherParams(BaseModel):+ city: str = Field(..., description="The city to look up weather for")+ units: str = Field("metric", pattern="^(metric|imperial)$")+++async def get_weather(tool_call_id, params: GetWeatherParams, *, signal=None, on_update=None):+ # Free Open-Meteo geocoding + forecast. No API key required.+ async with httpx.AsyncClient(timeout=10) as client:+ try:+ geo = await client.get(+ "https://geocoding-api.open-meteo.com/v1/search",+ params={"name": params.city, "count": 1, "language": "en"},+ )+ geo.raise_for_status()+ results = geo.json().get("results")+ if not results:+ return AgentToolResult(+ content=[TextContent(text=f"Couldn't find a city named {params.city!r}.")],+ is_error=True,+ )+ lat, lon = results[0]["latitude"], results[0]["longitude"]++ wx = await client.get(+ "https://api.open-meteo.com/v1/forecast",+ params={+ "latitude": lat,+ "longitude": lon,+ "current_weather": True,+ "temperature_unit": "celsius" if params.units == "metric" else "fahrenheit",+ },+ )+ wx.raise_for_status()+ cw = wx.json()["current_weather"]+ unit = "°C" if params.units == "metric" else "°F"+ return AgentToolResult(+ content=[TextContent(text=f"{cw['temperature']}{unit}, wind {cw['windspeed']} km/h in {params.city}.")],+ )+ except httpx.HTTPError as e:+ return AgentToolResult(+ content=[TextContent(text=f"Weather API error: {e}")],+ is_error=True,+ )+++weather_tool = AgentTool(+ name="get_weather",+ description="Get current weather for a city. Returns a short text summary.",+ parameters=GetWeatherParams,+ execute=get_weather,+)+++# --- The agent ----------------------------------------------------------++async def main():+ provider = AnthropicProvider(provider_id="anthropic", api_key=os.environ["ANTHROPIC_API_KEY"])+ agent = Agent(+ model=provider.model("claude-sonnet-4-5-20250929"),+ system_prompt="You are a concise weather assistant. Always use the tool; don't guess.",+ tools=[weather_tool],+ )++ def on_event(event, signal=None):+ if event.type == "message_update" and event.stream_event.type == "text_delta":+ print(event.stream_event.delta, end="", flush=True)+ elif event.type == "tool_execution_start":+ print(f"\n[calling {event.tool_name}({event.args})]")+ elif event.type == "agent_end":+ print()++ agent.subscribe(on_event)++ # Wrap prompt() so Ctrl-C cleanly cancels the run.+ task = asyncio.create_task(agent.prompt("Weather in Tokyo and São Paulo, please."))+ try:+ await task+ except KeyboardInterrupt:+ agent.abort()+ await agent.wait_for_idle()+++if __name__ == "__main__":+ asyncio.run(main())+```++Run:++```bash+pip install cubepi httpx+export ANTHROPIC_API_KEY=sk-…+python weather_agent.py+```++Sample output:++```+[calling get_weather({'city': 'Tokyo', 'units': 'metric'})]+[calling get_weather({'city': 'São Paulo', 'units': 'metric'})]+Tokyo is currently 18°C with a wind speed of 12 km/h. São Paulo is 25°C with winds of 9 km/h.+```++## What's going on++- **Two tools in parallel.** The model emits two `get_weather` tool+ calls in the same assistant turn. CubePi runs them concurrently —+ the second one doesn't wait for the first.+- **Streaming text + tool events interleave.** The `on_event` filter+ handles both `text_delta` (for the final answer) and+ `tool_execution_start` (for the "thinking" indicator).+- **Errors are tool results, not exceptions.** A bad city or network+ hiccup returns `is_error=True`; the model gets the error message+ and usually retries with a different spelling.+- **Cancellation is clean.** `agent.abort()` propagates through+ `signal` into in-flight tools and the provider stream.++## Extending this recipe++- **Add caching:** memoize the geocoding lookup by `city` —+ Open-Meteo's coordinates are stable.+- **Add retries:** wrap with [`RetryMiddleware`](../guides/middleware/examples#retries-with-backoff)+ to handle transient API errors.+- **Persist conversations:** add a+ [`SQLiteCheckpointer`](../guides/checkpointing/sqlite) so follow-up+ questions ("and in Osaka?") have history.++## See also++- [Building Your First Agent](../guides/agents/first-agent) — the same+ pattern with a hard-coded tool.+- [Tool Use & Parallel Execution](../guides/agents/tool-use) — more on+ parallel tool calls.diff --git a/website/versioned_sidebars/version-0.7-sidebars.json b/website/versioned_sidebars/version-0.7-sidebars.jsonnew file mode 100644index 0000000..27b1210--- /dev/null+++ b/website/versioned_sidebars/version-0.7-sidebars.json@@ -0,0 +1,130 @@+{+ "docs": [+ "intro",+ {+ "type": "category",+ "label": "Getting Started",+ "collapsed": false,+ "items": [+ "getting-started/installation",+ "getting-started/quick-start",+ "getting-started/core-concepts"+ ]+ },+ {+ "type": "category",+ "label": "Guides",+ "items": [+ {+ "type": "category",+ "label": "Agents",+ "items": [+ "guides/agents/first-agent",+ "guides/agents/tool-use",+ "guides/agents/multi-turn",+ "guides/agents/streaming"+ ]+ },+ {+ "type": "category",+ "label": "Providers",+ "items": [+ "guides/providers/overview",+ "guides/providers/anthropic",+ "guides/providers/openai",+ "guides/providers/custom",+ "guides/providers/image-generation"+ ]+ },+ {+ "type": "category",+ "label": "Checkpointing",+ "items": [+ "guides/checkpointing/sqlite",+ "guides/checkpointing/postgres",+ "guides/checkpointing/mysql",+ "guides/checkpointing/custom"+ ]+ },+ {+ "type": "category",+ "label": "Middleware",+ "items": [+ "guides/middleware/hooks",+ "guides/middleware/composition",+ "guides/middleware/examples",+ "guides/middleware/compaction",+ "guides/middleware/subagents"+ ]+ },+ {+ "type": "category",+ "label": "Human-in-the-Loop",+ "items": [+ "guides/hitl/overview",+ "guides/hitl/middlewares",+ "guides/hitl/durable",+ "guides/hitl/reference"+ ]+ },+ {+ "type": "category",+ "label": "MCP",+ "items": [+ "guides/mcp/loading",+ "guides/mcp/auth"+ ]+ },+ {+ "type": "category",+ "label": "Tracing",+ "items": [+ "guides/tracing/overview",+ "guides/tracing/getting-started",+ "guides/tracing/cli",+ "guides/tracing/otlp",+ "guides/tracing/content-recording",+ "guides/tracing/metrics"+ ]+ }+ ]+ },+ {+ "type": "category",+ "label": "API Reference",+ "link": {+ "type": "doc",+ "id": "api/index"+ },+ "items": [+ "api/cubepi-agent",+ "api/cubepi-providers",+ "api/cubepi-checkpointer",+ "api/cubepi-middleware",+ "api/cubepi-mcp",+ "api/cubepi-tracing",+ "api/cubepi-utils"+ ]+ },+ {+ "type": "category",+ "label": "Recipes",+ "items": [+ "recipes/weather-agent",+ "recipes/multi-provider-failover",+ "recipes/persistent-chat",+ "recipes/resumable-tasks",+ "recipes/postgres-fastapi",+ "recipes/ask-user-form",+ "recipes/sandbox-confirm"+ ]+ },+ {+ "type": "category",+ "label": "Migration",+ "items": [+ "migration/from-langgraph"+ ]+ }+ ]+}diff --git a/website/versions.json b/website/versions.jsonindex f9b93e3..718f23e 100644--- a/website/versions.json+++ b/website/versions.json@@ -1,4 +1,5 @@[+ "0.7","0.6","0.5","0.4",