@botonic/plugin-ai-agents 0.37.1 → 0.38.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/agent-builder.d.ts +6 -2
- package/lib/cjs/agent-builder.js +28 -5
- package/lib/cjs/agent-builder.js.map +1 -1
- package/lib/cjs/guardrails/index.d.ts +1 -0
- package/lib/cjs/guardrails/index.js +5 -0
- package/lib/cjs/guardrails/index.js.map +1 -0
- package/lib/cjs/guardrails/input.d.ts +3 -0
- package/lib/cjs/guardrails/input.js +33 -0
- package/lib/cjs/guardrails/input.js.map +1 -0
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +3 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/runner.d.ts +3 -2
- package/lib/cjs/runner.js +31 -1
- package/lib/cjs/runner.js.map +1 -1
- package/lib/cjs/structured-output/carousel.d.ts +2 -17
- package/lib/cjs/structured-output/carousel.js.map +1 -1
- package/lib/cjs/structured-output/exit.d.ts +2 -4
- package/lib/cjs/structured-output/exit.js.map +1 -1
- package/lib/cjs/structured-output/index.d.ts +1 -5
- package/lib/cjs/structured-output/index.js.map +1 -1
- package/lib/cjs/structured-output/text-with-buttons.d.ts +2 -8
- package/lib/cjs/structured-output/text-with-buttons.js.map +1 -1
- package/lib/cjs/structured-output/text.d.ts +2 -7
- package/lib/cjs/structured-output/text.js.map +1 -1
- package/lib/cjs/types.d.ts +10 -3
- package/lib/esm/agent-builder.d.ts +6 -2
- package/lib/esm/agent-builder.js +28 -5
- package/lib/esm/agent-builder.js.map +1 -1
- package/lib/esm/guardrails/index.d.ts +1 -0
- package/lib/esm/guardrails/index.js +2 -0
- package/lib/esm/guardrails/index.js.map +1 -0
- package/lib/esm/guardrails/input.d.ts +3 -0
- package/lib/esm/guardrails/input.js +29 -0
- package/lib/esm/guardrails/input.js.map +1 -0
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +3 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/runner.d.ts +3 -2
- package/lib/esm/runner.js +32 -2
- package/lib/esm/runner.js.map +1 -1
- package/lib/esm/structured-output/carousel.d.ts +2 -17
- package/lib/esm/structured-output/carousel.js.map +1 -1
- package/lib/esm/structured-output/exit.d.ts +2 -4
- package/lib/esm/structured-output/exit.js.map +1 -1
- package/lib/esm/structured-output/index.d.ts +1 -5
- package/lib/esm/structured-output/index.js +1 -1
- package/lib/esm/structured-output/index.js.map +1 -1
- package/lib/esm/structured-output/text-with-buttons.d.ts +2 -8
- package/lib/esm/structured-output/text-with-buttons.js.map +1 -1
- package/lib/esm/structured-output/text.d.ts +2 -7
- package/lib/esm/structured-output/text.js.map +1 -1
- package/lib/esm/types.d.ts +10 -3
- package/package.json +3 -3
- package/src/agent-builder.ts +43 -7
- package/src/guardrails/index.ts +1 -0
- package/src/guardrails/input.ts +39 -0
- package/src/index.ts +7 -3
- package/src/runner.ts +49 -4
- package/src/structured-output/carousel.ts +2 -15
- package/src/structured-output/exit.ts +2 -5
- package/src/structured-output/index.ts +5 -13
- package/src/structured-output/text-with-buttons.ts +2 -9
- package/src/structured-output/text.ts +2 -8
- package/src/types.ts +17 -4
- package/lib/cjs/structured-output/shared.d.ts +0 -3
- package/lib/cjs/structured-output/shared.js +0 -3
- package/lib/cjs/structured-output/shared.js.map +0 -1
- package/lib/esm/structured-output/shared.d.ts +0 -3
- package/lib/esm/structured-output/shared.js +0 -2
- package/lib/esm/structured-output/shared.js.map +0 -1
- package/src/structured-output/shared.ts +0 -3
package/lib/esm/runner.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __awaiter } from "tslib";
|
|
2
|
-
import { Runner } from '@openai/agents';
|
|
2
|
+
import { InputGuardrailTripwireTriggered, Runner, RunToolCallItem, } from '@openai/agents';
|
|
3
3
|
export class AIAgentRunner {
|
|
4
4
|
constructor(agent) {
|
|
5
5
|
this.agent = agent;
|
|
@@ -17,9 +17,30 @@ export class AIAgentRunner {
|
|
|
17
17
|
modelSettings: { temperature: 0 },
|
|
18
18
|
});
|
|
19
19
|
const result = yield runner.run(this.agent, messages, { context });
|
|
20
|
-
|
|
20
|
+
const outputMessages = ((_a = result.finalOutput) === null || _a === void 0 ? void 0 : _a.messages) || [];
|
|
21
|
+
const hasExit = outputMessages.length === 0 ||
|
|
22
|
+
outputMessages.some(message => message.type === 'exit');
|
|
23
|
+
const toolsExecuted = this.getExecutedNameTools(result);
|
|
24
|
+
return {
|
|
25
|
+
messages: hasExit
|
|
26
|
+
? []
|
|
27
|
+
: outputMessages.filter(message => message.type !== 'exit'),
|
|
28
|
+
toolsExecuted,
|
|
29
|
+
exit: hasExit,
|
|
30
|
+
inputGuardrailTriggered: [],
|
|
31
|
+
outputGuardrailTriggered: [],
|
|
32
|
+
};
|
|
21
33
|
}
|
|
22
34
|
catch (error) {
|
|
35
|
+
if (error instanceof InputGuardrailTripwireTriggered) {
|
|
36
|
+
return {
|
|
37
|
+
messages: [],
|
|
38
|
+
toolsExecuted: [],
|
|
39
|
+
exit: true,
|
|
40
|
+
inputGuardrailTriggered: error.result.output.outputInfo,
|
|
41
|
+
outputGuardrailTriggered: [],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
23
44
|
if (attempt > maxRetries) {
|
|
24
45
|
throw error;
|
|
25
46
|
}
|
|
@@ -27,5 +48,14 @@ export class AIAgentRunner {
|
|
|
27
48
|
}
|
|
28
49
|
});
|
|
29
50
|
}
|
|
51
|
+
getExecutedNameTools(result) {
|
|
52
|
+
var _a;
|
|
53
|
+
return (((_a = result.newItems) === null || _a === void 0 ? void 0 : _a.filter(item => item instanceof RunToolCallItem).map((item) => {
|
|
54
|
+
if (item.rawItem.type === 'function_call') {
|
|
55
|
+
return item.rawItem.name;
|
|
56
|
+
}
|
|
57
|
+
return '';
|
|
58
|
+
}).filter((toolName) => toolName !== '')) || []);
|
|
59
|
+
}
|
|
30
60
|
}
|
|
31
61
|
//# sourceMappingURL=runner.js.map
|
package/lib/esm/runner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,+BAA+B,EAC/B,MAAM,EACN,eAAe,GAChB,MAAM,gBAAgB,CAAA;AAUvB,MAAM,OAAO,aAAa;IAGxB,YAAY,KAAc;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAEK,GAAG,CACP,QAA+B,EAC/B,OAAgB,EAChB,aAAqB,CAAC;;YAEtB,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC,CAAA;QAC5D,CAAC;KAAA;IAEa,YAAY,CACxB,QAA+B,EAC/B,OAAgB,EAChB,UAAkB,EAClB,OAAe;;;YAEf,IAAI;gBACF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC;oBACxB,aAAa,EAAE,EAAE,WAAW,EAAE,CAAC,EAAE;iBAClC,CAAC,CAAA;gBACF,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;gBAElE,MAAM,cAAc,GAAG,CAAA,MAAA,MAAM,CAAC,WAAW,0CAAE,QAAQ,KAAI,EAAE,CAAA;gBACzD,MAAM,OAAO,GACX,cAAc,CAAC,MAAM,KAAK,CAAC;oBAC3B,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAA;gBACzD,MAAM,aAAa,GAAG,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAA;gBAEvD,OAAO;oBACL,QAAQ,EAAE,OAAO;wBACf,CAAC,CAAC,EAAE;wBACJ,CAAC,CAAE,cAAc,CAAC,MAAM,CACpB,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CACR;oBAChC,aAAa;oBACb,IAAI,EAAE,OAAO;oBACb,uBAAuB,EAAE,EAAE;oBAC3B,wBAAwB,EAAE,EAAE;iBAC7B,CAAA;aACF;YAAC,OAAO,KAAK,EAAE;gBACd,IAAI,KAAK,YAAY,+BAA+B,EAAE;oBACpD,OAAO;wBACL,QAAQ,EAAE,EAAE;wBACZ,aAAa,EAAE,EAAE;wBACjB,IAAI,EAAE,IAAI;wBACV,uBAAuB,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU;wBACvD,wBAAwB,EAAE,EAAE;qBAC7B,CAAA;iBACF;gBACD,IAAI,OAAO,GAAG,UAAU,EAAE;oBACxB,MAAM,KAAK,CAAA;iBACZ;gBACD,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,GAAG,CAAC,CAAC,CAAA;aACrE;;KACF;IAEO,oBAAoB,CAAC,MAAM;;QACjC,OAAO,CACL,CAAA,MAAA,MAAM,CAAC,QAAQ,0CACX,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,YAAY,eAAe,EAC/C,GAAG,CAAC,CAAC,IAAqB,EAAE,EAAE;YAC7B,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,eAAe,EAAE;gBACzC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;aACzB;YACD,OAAO,EAAE,CAAA;QACX,CAAC,EACA,MAAM,CAAC,CAAC,QAAgB,EAAE,EAAE,CAAC,QAAQ,KAAK,EAAE,CAAC,KAAI,EAAE,CACvD,CAAA;IACH,CAAC;CACF"}
|
|
@@ -1,20 +1,6 @@
|
|
|
1
|
+
import { CarouselMessage } from '@botonic/core';
|
|
1
2
|
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
interface CarouselElement {
|
|
4
|
-
title: string;
|
|
5
|
-
subtitle: string;
|
|
6
|
-
image: string;
|
|
7
|
-
button: {
|
|
8
|
-
text: string;
|
|
9
|
-
url: string;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
export interface CarouselMessage extends BaseMessage {
|
|
13
|
-
type: 'carousel';
|
|
14
|
-
content: {
|
|
15
|
-
elements: CarouselElement[];
|
|
16
|
-
};
|
|
17
|
-
}
|
|
3
|
+
export type { CarouselMessage };
|
|
18
4
|
export declare const CarouselSchema: z.ZodObject<{
|
|
19
5
|
type: z.ZodEnum<["carousel"]>;
|
|
20
6
|
content: z.ZodObject<{
|
|
@@ -97,4 +83,3 @@ export declare const CarouselSchema: z.ZodObject<{
|
|
|
97
83
|
}[];
|
|
98
84
|
};
|
|
99
85
|
}>;
|
|
100
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"carousel.js","sourceRoot":"","sources":["../../../src/structured-output/carousel.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"carousel.js","sourceRoot":"","sources":["../../../src/structured-output/carousel.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC;KAC5B,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC;IAC1B,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QAChB,QAAQ,EAAE,CAAC,CAAC,KAAK,CACf,CAAC,CAAC,MAAM,CAAC;YACP,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;YACjB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;YACpB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC;gBACf,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;gBAChB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;aAChB,CAAC;SACH,CAAC,CACH;KACF,CAAC;CACH,CAAC;KACD,QAAQ,CAAC,kDAAkD,CAAC,CAAA"}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
+
import { ExitMessage } from '@botonic/core';
|
|
1
2
|
import z from 'zod';
|
|
2
|
-
|
|
3
|
-
export interface ExitMessage extends BaseMessage {
|
|
4
|
-
type: 'exit';
|
|
5
|
-
}
|
|
3
|
+
export type { ExitMessage };
|
|
6
4
|
export declare const ExitSchema: z.ZodObject<{
|
|
7
5
|
type: z.ZodEnum<["exit"]>;
|
|
8
6
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exit.js","sourceRoot":"","sources":["../../../src/structured-output/exit.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"exit.js","sourceRoot":"","sources":["../../../src/structured-output/exit.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,KAAK,CAAA;AAInB,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC;KACxB,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;CACvB,CAAC;KACD,QAAQ,CACP,uIAAuI,CACxI,CAAA"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
+
import { OutputMessage } from '@botonic/core';
|
|
1
2
|
import { z } from 'zod';
|
|
2
|
-
import { CarouselMessage } from './carousel';
|
|
3
|
-
import { ExitMessage } from './exit';
|
|
4
|
-
import { TextMessage } from './text';
|
|
5
|
-
import { TextWithButtonsMessage } from './text-with-buttons';
|
|
6
|
-
export type OutputMessage = TextMessage | TextWithButtonsMessage | CarouselMessage | ExitMessage;
|
|
7
3
|
export interface Output {
|
|
8
4
|
messages: OutputMessage[];
|
|
9
5
|
}
|
|
@@ -2,7 +2,7 @@ import { z } from 'zod';
|
|
|
2
2
|
import { CarouselSchema } from './carousel';
|
|
3
3
|
import { ExitSchema } from './exit';
|
|
4
4
|
import { TextSchema } from './text';
|
|
5
|
-
import { TextWithButtonsSchema
|
|
5
|
+
import { TextWithButtonsSchema } from './text-with-buttons';
|
|
6
6
|
export const OutputSchema = z
|
|
7
7
|
.object({
|
|
8
8
|
messages: z.array(z.union([TextSchema, TextWithButtonsSchema, CarouselSchema, ExitSchema])),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/structured-output/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/structured-output/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAA;AAM3D,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC;KAC1B,MAAM,CAAC;IACN,QAAQ,EAAE,CAAC,CAAC,KAAK,CACf,CAAC,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,qBAAqB,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC,CACzE;CACF,CAAC;KACD,QAAQ,CAAC,qCAAqC,CAAC,CAAA"}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
+
import { TextWithButtonsMessage } from '@botonic/core';
|
|
1
2
|
import z from 'zod';
|
|
2
|
-
|
|
3
|
-
export interface TextWithButtonsMessage extends BaseMessage {
|
|
4
|
-
type: 'textWithButtons';
|
|
5
|
-
content: {
|
|
6
|
-
text: string;
|
|
7
|
-
buttons: string[];
|
|
8
|
-
};
|
|
9
|
-
}
|
|
3
|
+
export type { TextWithButtonsMessage };
|
|
10
4
|
export declare const TextWithButtonsSchema: z.ZodObject<{
|
|
11
5
|
type: z.ZodEnum<["textWithButtons"]>;
|
|
12
6
|
content: z.ZodObject<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-with-buttons.js","sourceRoot":"","sources":["../../../src/structured-output/text-with-buttons.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"text-with-buttons.js","sourceRoot":"","sources":["../../../src/structured-output/text-with-buttons.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,KAAK,CAAA;AAInB,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC;KACnC,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACjC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;QAChB,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;KAC7B,CAAC;CACH,CAAC;KACD,QAAQ,CACP,oEAAoE,CACrE,CAAA"}
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
+
import { TextMessage } from '@botonic/core';
|
|
1
2
|
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
export interface TextMessage extends BaseMessage {
|
|
4
|
-
type: 'text';
|
|
5
|
-
content: {
|
|
6
|
-
text: string;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
3
|
+
export type { TextMessage };
|
|
9
4
|
export declare const TextSchema: z.ZodObject<{
|
|
10
5
|
type: z.ZodEnum<["text"]>;
|
|
11
6
|
content: z.ZodObject<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/structured-output/text.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/structured-output/text.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC;KACxB,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;KACjB,CAAC;CACH,CAAC;KACD,QAAQ,CAAC,gBAAgB,CAAC,CAAA"}
|
package/lib/esm/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AgenticOutputMessage, InferenceResponse, RunResult } from '@botonic/core';
|
|
2
|
+
import { Agent, AgentInputItem, RunContext as OpenAIRunContext, Tool as OpenAITool } from '@openai/agents';
|
|
2
3
|
import { ZodSchema } from 'zod';
|
|
3
|
-
import {
|
|
4
|
+
import { OutputSchema } from './structured-output';
|
|
4
5
|
export interface Context {
|
|
5
6
|
authToken: string;
|
|
6
7
|
}
|
|
@@ -11,6 +12,11 @@ export interface CustomTool {
|
|
|
11
12
|
schema: ZodSchema;
|
|
12
13
|
func: (input?: any, runContext?: RunContext) => Promise<any>;
|
|
13
14
|
}
|
|
15
|
+
export interface GuardrailRule {
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
18
|
+
}
|
|
19
|
+
export type ContactInfo = Record<string, string>;
|
|
14
20
|
export type Tool = OpenAITool<Context>;
|
|
15
21
|
export type AIAgent = Agent<Context, typeof OutputSchema>;
|
|
16
22
|
export interface PluginAiAgentOptions {
|
|
@@ -23,6 +29,7 @@ export interface AiAgentArgs {
|
|
|
23
29
|
activeTools?: {
|
|
24
30
|
name: string;
|
|
25
31
|
}[];
|
|
32
|
+
inputGuardrailRules?: GuardrailRule[];
|
|
26
33
|
}
|
|
27
34
|
export type AgenticInputMessage = AgentInputItem;
|
|
28
|
-
export type AgenticOutputMessage
|
|
35
|
+
export type { AgenticOutputMessage, InferenceResponse, RunResult };
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botonic/plugin-ai-agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.0-alpha.0",
|
|
4
4
|
"main": "./lib/cjs/index.js",
|
|
5
5
|
"module": "./lib/esm/index.js",
|
|
6
6
|
"description": "Use AI Agents to generate your contents",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rm -rf lib && ../../node_modules/.bin/tsc -p tsconfig.json && ../../node_modules/.bin/tsc -p tsconfig.esm.json",
|
|
9
9
|
"build:watch": "npm run build -- --watch",
|
|
10
|
-
"test": "
|
|
10
|
+
"test": "../../node_modules/.bin/jest --coverage",
|
|
11
11
|
"prepublishOnly": "rm -rf lib && npm i && npm run build",
|
|
12
12
|
"lint": "npm run lint_core -- --fix",
|
|
13
13
|
"lint_core": "../../node_modules/.bin/eslint_d --cache --quiet 'src/**/*.ts*'"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@botonic/core": "
|
|
16
|
+
"@botonic/core": "0.38.0-alpha.0",
|
|
17
17
|
"@openai/agents": "^0.0.10",
|
|
18
18
|
"axios": "^1.10.0",
|
|
19
19
|
"openai": "^5.8.3",
|
package/src/agent-builder.ts
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
1
|
-
import { Agent } from '@openai/agents'
|
|
1
|
+
import { Agent, InputGuardrail } from '@openai/agents'
|
|
2
2
|
|
|
3
|
+
import { createInputGuardrail } from './guardrails'
|
|
3
4
|
import { OutputSchema } from './structured-output'
|
|
4
5
|
import { mandatoryTools } from './tools'
|
|
5
|
-
import { AIAgent, Tool } from './types'
|
|
6
|
+
import { AIAgent, ContactInfo, GuardrailRule, Tool } from './types'
|
|
6
7
|
|
|
7
8
|
export class AIAgentBuilder {
|
|
8
9
|
private name: string
|
|
9
10
|
private instructions: string
|
|
10
11
|
private tools: Tool[]
|
|
12
|
+
private inputGuardrails: InputGuardrail[]
|
|
11
13
|
|
|
12
|
-
constructor(
|
|
14
|
+
constructor(
|
|
15
|
+
name: string,
|
|
16
|
+
instructions: string,
|
|
17
|
+
tools: Tool[],
|
|
18
|
+
contactInfo: ContactInfo,
|
|
19
|
+
inputGuardrailRules: GuardrailRule[]
|
|
20
|
+
) {
|
|
13
21
|
this.name = name
|
|
14
|
-
this.instructions = this.addExtraInstructions(instructions)
|
|
22
|
+
this.instructions = this.addExtraInstructions(instructions, contactInfo)
|
|
15
23
|
this.tools = this.addHubtypeTools(tools)
|
|
24
|
+
this.inputGuardrails = []
|
|
25
|
+
if (inputGuardrailRules.length > 0) {
|
|
26
|
+
const inputGuardrail = createInputGuardrail(inputGuardrailRules)
|
|
27
|
+
this.inputGuardrails.push(inputGuardrail)
|
|
28
|
+
}
|
|
16
29
|
}
|
|
17
30
|
|
|
18
31
|
build(): AIAgent {
|
|
@@ -21,11 +34,35 @@ export class AIAgentBuilder {
|
|
|
21
34
|
instructions: this.instructions,
|
|
22
35
|
tools: this.tools,
|
|
23
36
|
outputType: OutputSchema,
|
|
37
|
+
inputGuardrails: this.inputGuardrails,
|
|
38
|
+
outputGuardrails: [],
|
|
24
39
|
})
|
|
25
40
|
}
|
|
26
41
|
|
|
27
|
-
private addExtraInstructions(
|
|
42
|
+
private addExtraInstructions(
|
|
43
|
+
initialInstructions: string,
|
|
44
|
+
contactInfo: ContactInfo
|
|
45
|
+
): string {
|
|
46
|
+
const instructions = `<instructions>\n${initialInstructions}\n</instructions>`
|
|
47
|
+
const metadataInstructions = this.getMetadataInstructions()
|
|
48
|
+
const contactInfoInstructions = this.getContactInfoInstructions(contactInfo)
|
|
49
|
+
const outputInstructions = this.getOutputInstructions()
|
|
50
|
+
return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${outputInstructions}`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private getContactInfoInstructions(contactInfo: ContactInfo): string {
|
|
54
|
+
const structuredContactInfo = Object.entries(contactInfo)
|
|
55
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
56
|
+
.join('\n')
|
|
57
|
+
return `<contact_info>\n${structuredContactInfo}\n</contact_info>`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private getMetadataInstructions(): string {
|
|
28
61
|
const metadata = `Current Date: ${new Date().toISOString()}`
|
|
62
|
+
return `<metadata>\n${metadata}\n</metadata>`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private getOutputInstructions(): string {
|
|
29
66
|
const example = {
|
|
30
67
|
messages: [
|
|
31
68
|
{
|
|
@@ -35,10 +72,9 @@ export class AIAgentBuilder {
|
|
|
35
72
|
},
|
|
36
73
|
},
|
|
37
74
|
],
|
|
38
|
-
numMessages: 1,
|
|
39
75
|
}
|
|
40
76
|
const output = `Return a JSON that follows the output schema provided. Never return multiple output schemas concatenated by a line break.\n<example>\n${JSON.stringify(example)}\n</example>`
|
|
41
|
-
return `<
|
|
77
|
+
return `<output>\n${output}\n</output>`
|
|
42
78
|
}
|
|
43
79
|
|
|
44
80
|
private addHubtypeTools(tools: Tool[]): Tool[] {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './input'
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Agent, InputGuardrail, run, UserMessageItem } from '@openai/agents'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
|
|
4
|
+
import { GuardrailRule } from '../types'
|
|
5
|
+
|
|
6
|
+
export function createInputGuardrail(rules: GuardrailRule[]): InputGuardrail {
|
|
7
|
+
const outputType = z.object(
|
|
8
|
+
Object.fromEntries(
|
|
9
|
+
rules.map(rule => [rule.name, z.boolean().describe(rule.description)])
|
|
10
|
+
)
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
const agent = new Agent({
|
|
14
|
+
name: 'InputGuardrail',
|
|
15
|
+
instructions:
|
|
16
|
+
'Check if the user triggers some of the following guardrails.',
|
|
17
|
+
outputType,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
name: 'InputGuardrail',
|
|
22
|
+
execute: async ({ input, context }) => {
|
|
23
|
+
const lastMessage = input[input.length - 1] as UserMessageItem
|
|
24
|
+
const result = await run(agent, [lastMessage], { context })
|
|
25
|
+
const finalOutput = result.finalOutput
|
|
26
|
+
if (finalOutput === undefined) {
|
|
27
|
+
throw new Error('Guardrail agent failed to produce output')
|
|
28
|
+
}
|
|
29
|
+
const triggered = Object.values(finalOutput).some(value => value === true)
|
|
30
|
+
const triggeredGuardrails = Object.keys(finalOutput).filter(
|
|
31
|
+
key => finalOutput[key] === true
|
|
32
|
+
)
|
|
33
|
+
return {
|
|
34
|
+
outputInfo: triggeredGuardrails,
|
|
35
|
+
tripwireTriggered: triggered,
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,10 +8,10 @@ import { setUpOpenAI } from './openai'
|
|
|
8
8
|
import { AIAgentRunner } from './runner'
|
|
9
9
|
import {
|
|
10
10
|
AgenticInputMessage,
|
|
11
|
-
AgenticOutputMessage,
|
|
12
11
|
AiAgentArgs,
|
|
13
12
|
Context,
|
|
14
13
|
CustomTool,
|
|
14
|
+
InferenceResponse,
|
|
15
15
|
PluginAiAgentOptions,
|
|
16
16
|
Tool,
|
|
17
17
|
} from './types'
|
|
@@ -33,7 +33,7 @@ export default class BotonicPluginAiAgents implements Plugin {
|
|
|
33
33
|
async getInference(
|
|
34
34
|
request: BotContext,
|
|
35
35
|
aiAgentArgs: AiAgentArgs
|
|
36
|
-
): Promise<
|
|
36
|
+
): Promise<InferenceResponse> {
|
|
37
37
|
try {
|
|
38
38
|
const authToken = isProd ? request.session._access_token : this.authToken
|
|
39
39
|
if (!authToken) {
|
|
@@ -46,7 +46,9 @@ export default class BotonicPluginAiAgents implements Plugin {
|
|
|
46
46
|
const agent = new AIAgentBuilder(
|
|
47
47
|
aiAgentArgs.name,
|
|
48
48
|
aiAgentArgs.instructions,
|
|
49
|
-
tools
|
|
49
|
+
tools,
|
|
50
|
+
request.session.user.contact_info || {},
|
|
51
|
+
aiAgentArgs.inputGuardrailRules || []
|
|
50
52
|
).build()
|
|
51
53
|
|
|
52
54
|
const messages = await this.getMessages(request, authToken, 25)
|
|
@@ -58,6 +60,8 @@ export default class BotonicPluginAiAgents implements Plugin {
|
|
|
58
60
|
return await runner.run(messages, context)
|
|
59
61
|
} catch (error) {
|
|
60
62
|
console.error('error plugin returns undefined', error)
|
|
63
|
+
// Here we can return a InferenceResponse as a exit
|
|
64
|
+
// but indicate that the inference failed
|
|
61
65
|
return undefined
|
|
62
66
|
}
|
|
63
67
|
}
|
package/src/runner.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
InputGuardrailTripwireTriggered,
|
|
3
|
+
Runner,
|
|
4
|
+
RunToolCallItem,
|
|
5
|
+
} from '@openai/agents'
|
|
2
6
|
|
|
3
7
|
import {
|
|
4
8
|
AgenticInputMessage,
|
|
5
9
|
AgenticOutputMessage,
|
|
6
10
|
AIAgent,
|
|
7
11
|
Context,
|
|
12
|
+
RunResult,
|
|
8
13
|
} from './types'
|
|
9
14
|
|
|
10
15
|
export class AIAgentRunner {
|
|
@@ -18,7 +23,7 @@ export class AIAgentRunner {
|
|
|
18
23
|
messages: AgenticInputMessage[],
|
|
19
24
|
context: Context,
|
|
20
25
|
maxRetries: number = 1
|
|
21
|
-
): Promise<
|
|
26
|
+
): Promise<RunResult> {
|
|
22
27
|
return this.runWithRetry(messages, context, maxRetries, 1)
|
|
23
28
|
}
|
|
24
29
|
|
|
@@ -27,18 +32,58 @@ export class AIAgentRunner {
|
|
|
27
32
|
context: Context,
|
|
28
33
|
maxRetries: number,
|
|
29
34
|
attempt: number
|
|
30
|
-
): Promise<
|
|
35
|
+
): Promise<RunResult> {
|
|
31
36
|
try {
|
|
32
37
|
const runner = new Runner({
|
|
33
38
|
modelSettings: { temperature: 0 },
|
|
34
39
|
})
|
|
35
40
|
const result = await runner.run(this.agent, messages, { context })
|
|
36
|
-
|
|
41
|
+
|
|
42
|
+
const outputMessages = result.finalOutput?.messages || []
|
|
43
|
+
const hasExit =
|
|
44
|
+
outputMessages.length === 0 ||
|
|
45
|
+
outputMessages.some(message => message.type === 'exit')
|
|
46
|
+
const toolsExecuted = this.getExecutedNameTools(result)
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
messages: hasExit
|
|
50
|
+
? []
|
|
51
|
+
: (outputMessages.filter(
|
|
52
|
+
message => message.type !== 'exit'
|
|
53
|
+
) as AgenticOutputMessage[]),
|
|
54
|
+
toolsExecuted,
|
|
55
|
+
exit: hasExit,
|
|
56
|
+
inputGuardrailTriggered: [],
|
|
57
|
+
outputGuardrailTriggered: [],
|
|
58
|
+
}
|
|
37
59
|
} catch (error) {
|
|
60
|
+
if (error instanceof InputGuardrailTripwireTriggered) {
|
|
61
|
+
return {
|
|
62
|
+
messages: [],
|
|
63
|
+
toolsExecuted: [],
|
|
64
|
+
exit: true,
|
|
65
|
+
inputGuardrailTriggered: error.result.output.outputInfo,
|
|
66
|
+
outputGuardrailTriggered: [],
|
|
67
|
+
}
|
|
68
|
+
}
|
|
38
69
|
if (attempt > maxRetries) {
|
|
39
70
|
throw error
|
|
40
71
|
}
|
|
41
72
|
return this.runWithRetry(messages, context, maxRetries, attempt + 1)
|
|
42
73
|
}
|
|
43
74
|
}
|
|
75
|
+
|
|
76
|
+
private getExecutedNameTools(result) {
|
|
77
|
+
return (
|
|
78
|
+
result.newItems
|
|
79
|
+
?.filter(item => item instanceof RunToolCallItem)
|
|
80
|
+
.map((item: RunToolCallItem) => {
|
|
81
|
+
if (item.rawItem.type === 'function_call') {
|
|
82
|
+
return item.rawItem.name
|
|
83
|
+
}
|
|
84
|
+
return ''
|
|
85
|
+
})
|
|
86
|
+
.filter((toolName: string) => toolName !== '') || []
|
|
87
|
+
)
|
|
88
|
+
}
|
|
44
89
|
}
|
|
@@ -1,20 +1,7 @@
|
|
|
1
|
+
import { CarouselMessage } from '@botonic/core'
|
|
1
2
|
import { z } from 'zod'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface CarouselElement {
|
|
6
|
-
title: string
|
|
7
|
-
subtitle: string
|
|
8
|
-
image: string
|
|
9
|
-
button: { text: string; url: string }
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface CarouselMessage extends BaseMessage {
|
|
13
|
-
type: 'carousel'
|
|
14
|
-
content: {
|
|
15
|
-
elements: CarouselElement[]
|
|
16
|
-
}
|
|
17
|
-
}
|
|
4
|
+
export type { CarouselMessage }
|
|
18
5
|
|
|
19
6
|
export const CarouselSchema = z
|
|
20
7
|
.object({
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
+
import { ExitMessage } from '@botonic/core'
|
|
1
2
|
import z from 'zod'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export interface ExitMessage extends BaseMessage {
|
|
6
|
-
type: 'exit'
|
|
7
|
-
}
|
|
4
|
+
export type { ExitMessage }
|
|
8
5
|
|
|
9
6
|
export const ExitSchema = z
|
|
10
7
|
.object({
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
+
import { OutputMessage } from '@botonic/core'
|
|
1
2
|
import { z } from 'zod'
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
TextWithButtonsMessage,
|
|
8
|
-
TextWithButtonsSchema,
|
|
9
|
-
} from './text-with-buttons'
|
|
10
|
-
|
|
11
|
-
export type OutputMessage =
|
|
12
|
-
| TextMessage
|
|
13
|
-
| TextWithButtonsMessage
|
|
14
|
-
| CarouselMessage
|
|
15
|
-
| ExitMessage
|
|
4
|
+
import { CarouselSchema } from './carousel'
|
|
5
|
+
import { ExitSchema } from './exit'
|
|
6
|
+
import { TextSchema } from './text'
|
|
7
|
+
import { TextWithButtonsSchema } from './text-with-buttons'
|
|
16
8
|
|
|
17
9
|
export interface Output {
|
|
18
10
|
messages: OutputMessage[]
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
+
import { TextWithButtonsMessage } from '@botonic/core'
|
|
1
2
|
import z from 'zod'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export interface TextWithButtonsMessage extends BaseMessage {
|
|
6
|
-
type: 'textWithButtons'
|
|
7
|
-
content: {
|
|
8
|
-
text: string
|
|
9
|
-
buttons: string[]
|
|
10
|
-
}
|
|
11
|
-
}
|
|
4
|
+
export type { TextWithButtonsMessage }
|
|
12
5
|
|
|
13
6
|
export const TextWithButtonsSchema = z
|
|
14
7
|
.object({
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
+
import { TextMessage } from '@botonic/core'
|
|
1
2
|
import { z } from 'zod'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export interface TextMessage extends BaseMessage {
|
|
6
|
-
type: 'text'
|
|
7
|
-
content: {
|
|
8
|
-
text: string
|
|
9
|
-
}
|
|
10
|
-
}
|
|
4
|
+
export type { TextMessage }
|
|
11
5
|
|
|
12
6
|
export const TextSchema = z
|
|
13
7
|
.object({
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgenticOutputMessage,
|
|
3
|
+
InferenceResponse,
|
|
4
|
+
RunResult,
|
|
5
|
+
} from '@botonic/core'
|
|
1
6
|
import {
|
|
2
7
|
Agent,
|
|
3
8
|
AgentInputItem,
|
|
4
|
-
Tool as OpenAITool,
|
|
5
9
|
RunContext as OpenAIRunContext,
|
|
10
|
+
Tool as OpenAITool,
|
|
6
11
|
} from '@openai/agents'
|
|
7
12
|
import { ZodSchema } from 'zod'
|
|
8
13
|
|
|
9
|
-
import {
|
|
14
|
+
import { OutputSchema } from './structured-output'
|
|
10
15
|
|
|
11
16
|
export interface Context {
|
|
12
17
|
authToken: string
|
|
@@ -21,9 +26,15 @@ export interface CustomTool {
|
|
|
21
26
|
func: (input?: any, runContext?: RunContext) => Promise<any>
|
|
22
27
|
}
|
|
23
28
|
|
|
29
|
+
export interface GuardrailRule {
|
|
30
|
+
name: string
|
|
31
|
+
description: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ContactInfo = Record<string, string>
|
|
35
|
+
|
|
24
36
|
export type Tool = OpenAITool<Context>
|
|
25
37
|
export type AIAgent = Agent<Context, typeof OutputSchema>
|
|
26
|
-
|
|
27
38
|
export interface PluginAiAgentOptions {
|
|
28
39
|
authToken?: string
|
|
29
40
|
customTools?: CustomTool[]
|
|
@@ -33,7 +44,9 @@ export interface AiAgentArgs {
|
|
|
33
44
|
name: string
|
|
34
45
|
instructions: string
|
|
35
46
|
activeTools?: { name: string }[]
|
|
47
|
+
inputGuardrailRules?: GuardrailRule[]
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
export type AgenticInputMessage = AgentInputItem
|
|
39
|
-
|
|
51
|
+
|
|
52
|
+
export type { AgenticOutputMessage, InferenceResponse, RunResult }
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/structured-output/shared.ts"],"names":[],"mappings":""}
|