feat(agent): add provider options passthrough, tool call records, and completion validation retries

This commit is contained in:
2026-05-07 10:26:45 +00:00
parent 0dde716109
commit b08cb3689e
10 changed files with 901 additions and 603 deletions
+68 -2
View File
@@ -91,11 +91,15 @@ The single entry point. Options:
| `prompt` | `string` | *required* | The user's task/question |
| `system` | `string` | `undefined` | System prompt |
| `tools` | `ToolSet` | `{}` | Tools the agent can call |
| `providerOptions` | `ProviderOptions` | `undefined` | Provider-specific AI SDK request options passed through to `streamText()` |
| `maxSteps` | `number` | `20` | Max agentic steps before stopping |
| `messages` | `ModelMessage[]` | `[]` | Conversation history (for multi-turn) |
| `maxRetries` | `number` | `5` | Max retries on rate-limit/server errors |
| `onToken` | `(delta: string) => void` | — | Streaming token callback |
| `onToolCall` | `(name: string) => void` | — | Called when a tool is invoked |
| `onToolResult` | `(name: string, result: unknown) => void` | — | Called when a tool finishes |
| `validateCompletion` | `(result) => string \| void` | — | Return a string to reject and reprompt an incomplete run |
| `maxValidationRetries` | `number` | `0` | Number of validation-triggered reprompts allowed |
| `onContextOverflow` | `(messages) => messages` | — | Handle context overflow (e.g., compact messages) |
### `IAgentRunResult`
@@ -107,13 +111,75 @@ interface IAgentRunResult {
steps: number; // Number of agentic steps taken
messages: ModelMessage[]; // Full conversation for multi-turn
usage: {
promptTokens: number;
completionTokens: number;
inputTokens: number;
outputTokens: number;
totalTokens: number;
};
toolCalls: Array<{
toolName: string;
input: unknown;
output?: unknown;
error?: string;
}>;
}
```
### OpenAI Provider Options
Use `providerOptions` for provider-specific request settings such as GPT reasoning effort. SmartAgent forwards the object unchanged to AI SDK `streamText()`.
```typescript
import { getModelSetup } from '@push.rocks/smartai';
import { runAgent } from '@push.rocks/smartagent';
const setup = getModelSetup({
provider: 'openai',
model: 'gpt-5.5',
apiKey: process.env.OPENAI_API_KEY,
providerOptions: {
openai: {
reasoningEffort: 'xhigh',
},
},
});
const result = await runAgent({
model: setup.model,
system: 'You handle financial documents carefully.',
prompt: 'Process this inbox document.',
tools,
maxSteps: 20,
providerOptions: setup.providerOptions,
});
const saved = result.toolCalls.some((call) =>
call.toolName === 'saveVoucher' || call.toolName === 'saveBankStatement',
);
```
### Completion Validation
Use `validateCompletion` when a workflow must not finish unless a required side-effect happened. Return `void` to accept the run, or return a string to append that string as a new user message and continue. If retries are exhausted, `runAgent()` throws.
```typescript
const result = await runAgent({
model,
prompt: 'Process this inbox document.',
tools,
maxSteps: 20,
maxValidationRetries: 1,
validateCompletion: (result) => {
const saved = result.toolCalls.some((call) =>
call.toolName === 'saveVoucher' || call.toolName === 'saveBankStatement',
);
if (!saved) {
return 'You must call saveVoucher or saveBankStatement before finalizing.';
}
},
});
```
## Defining Tools 🛠️
Tools use Vercel AI SDK's `tool()` helper with Zod schemas: