Skip to content

Commit 0965538

Browse files
authored
feat(agent): IMicroAgenticaConfig.throw (#517)
1 parent 65df5ce commit 0965538

5 files changed

Lines changed: 89 additions & 5 deletions

File tree

packages/core/src/MicroAgentica.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { createUserMessageEvent } from "./factory/events";
1919
import { call, describe } from "./orchestrate";
2020
import { transformHistory } from "./transformers/transformHistory";
2121
import { __map_take } from "./utils/__map_take";
22+
import { assertExecuteFailure } from "./utils/assertExecuteFailure";
2223
import { getChatCompletionWithStreamingFunction } from "./utils/request";
2324

2425
/**
@@ -166,10 +167,18 @@ export class MicroAgentica {
166167
ctx,
167168
this.operations_.array,
168169
);
169-
if (executes.length
170-
&& this.props.config?.executor?.describe !== null
171-
&& this.props.config?.executor?.describe !== false) {
172-
await describe(ctx, executes);
170+
if (this.props.config?.throw === true) {
171+
for (const execute of executes) {
172+
assertExecuteFailure(execute);
173+
}
174+
}
175+
176+
// eslint-disable-next-line
177+
if (executes.length && !!this.props.config?.executor?.describe) {
178+
const func = typeof this.props.config.executor.describe === "function"
179+
? this.props.config.executor.describe
180+
: describe;
181+
await func(ctx, executes);
173182
}
174183

175184
const completed: MicroAgenticaHistory[] = await Promise.all(

packages/core/src/structures/IMicroAgenticaConfig.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ export interface IMicroAgenticaConfig {
7878
*/
7979
retry?: number;
8080

81+
/**
82+
* Whether to throw an exception when execution fails.
83+
*
84+
* If you set this property to `true`, the A.I. chatbot will throw
85+
* an exception when {@link AgenticaExecuteHistory.success} is `false`.
86+
*
87+
* Otherwise, the execution failure will be handled silently without
88+
* throwing an exception.
89+
*
90+
* @default false
91+
*/
92+
throw?: boolean;
93+
8194
/**
8295
* Backoff strategy.
8396
*

packages/core/src/structures/IMicroAgenticaExecutor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface IMicroAgenticaExecutor {
5555
* @param ctx Context of the agent
5656
* @param executes List of function calling results
5757
* @returns List of prompts generated by the describer
58+
* @default false
5859
*/
5960
describe:
6061
| boolean
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
2+
3+
export function assertExecuteFailure(failure: AgenticaExecuteHistory): void {
4+
if (failure.success === true) {
5+
return;
6+
}
7+
else if (failure.value instanceof Error) {
8+
throw failure.value;
9+
}
10+
else if (typeof failure.value === "object" && failure.value !== null) {
11+
const error: Error = new Error("Error from execute failure");
12+
Object.assign(error, failure.value);
13+
throw error;
14+
}
15+
throw failure.value;
16+
}

website/content/docs/core/micro.mdx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,49 @@ When you call `MicroAgentica.conversate()` function, the agent will start the [#
376376

377377
If you want to archive the conversation state of current agent, store the returned prompts to your database with serialization to `IMicroAgenticaHistoryJson` format by calling `MicroAgenticaHistory.toJSON()` function. And then restore the conversation state by assigning the prompt histories to the `IMicroAgenticaProps.histories` property creating a new `Agentica` instance.
378378

379-
By the way, waiting for the completion of internal agents' orchestration and giving prompt results to the user at once is not a good choice in the UX (User Experience) perspective. In that case, it would better to deliver the events to the user step by step with streaming. Enroll event listeners by calling `MicroAgentica.on()` function, and deliver it to the user.
379+
By the way, waiting for the completion of internal agents' orchestration and giving prompt results to the user at once is not a good choice in the UX (User Experience) perspective. In that case, it would better to deliver the events to the user step by step with streaming. Enroll event listeners by calling `MicroAgentica.on()` function, and deliver it to the user.
380+
381+
382+
383+
384+
## Error Handling
385+
<Tabs items={[
386+
<code>src/main.ts</code>,
387+
<code>IMicroAgenticaConfig</code>,
388+
]}>
389+
<Tabs.Tab>
390+
```typescript filename="src/main.ts" showLineNumbers {12}
391+
import { MicroAgentica } from "@agentica/core";
392+
import OpenAI from "openai";
393+
394+
const agent = new MicroAgentica({
395+
vendor: {
396+
api: new OpenAI({ apiKey: "********" }),
397+
model: "gpt-4o-mini",
398+
},
399+
controllers: [...],
400+
config: {
401+
throw: true,
402+
},
403+
});
404+
405+
try {
406+
await agent.conversate("I wanna buy MacBook Pro");
407+
} catch (error) {
408+
console.error("Function execution failed:", error);
409+
}
410+
```
411+
</Tabs.Tab>
412+
<Tabs.Tab>
413+
<RemoteSource
414+
url="https://raw.githubusercontent.com/wrtnlabs/agentica/refs/heads/main/packages/core/src/structures/IMicroAgenticaConfig.ts"
415+
filename="@agentica/core/IMicroAgenticaConfig"
416+
showLineNumbers />
417+
</Tabs.Tab>
418+
</Tabs>
419+
420+
You can configure the `throw` property to control error handling behavior.
421+
422+
When you set `IMicroAgenticaConfig.throw` to `true`, the agent will throw an exception when function execution fails (`AgenticaExecuteHistory.success === false`). This is useful when you want to handle execution failures explicitly in your application logic.
423+
424+
By default, execution failures are handled silently without throwing exceptions. Set this property to `true` if you need to catch and handle execution errors in a try-catch block.

0 commit comments

Comments
 (0)