@botonic/plugin-ai-agents 0.37.0 → 0.37.2-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/README.md +148 -6
- 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 +12 -4
- 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 +12 -4
- 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 +20 -5
- 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/agent-builder.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { Agent } from '@openai/agents';
|
|
2
|
+
import { createInputGuardrail } from './guardrails';
|
|
2
3
|
import { OutputSchema } from './structured-output';
|
|
3
4
|
import { mandatoryTools } from './tools';
|
|
4
5
|
export class AIAgentBuilder {
|
|
5
|
-
constructor(name, instructions, tools) {
|
|
6
|
+
constructor(name, instructions, tools, contactInfo, inputGuardrailRules) {
|
|
6
7
|
this.name = name;
|
|
7
|
-
this.instructions = this.addExtraInstructions(instructions);
|
|
8
|
+
this.instructions = this.addExtraInstructions(instructions, contactInfo);
|
|
8
9
|
this.tools = this.addHubtypeTools(tools);
|
|
10
|
+
this.inputGuardrails = [];
|
|
11
|
+
if (inputGuardrailRules.length > 0) {
|
|
12
|
+
const inputGuardrail = createInputGuardrail(inputGuardrailRules);
|
|
13
|
+
this.inputGuardrails.push(inputGuardrail);
|
|
14
|
+
}
|
|
9
15
|
}
|
|
10
16
|
build() {
|
|
11
17
|
return new Agent({
|
|
@@ -13,10 +19,28 @@ export class AIAgentBuilder {
|
|
|
13
19
|
instructions: this.instructions,
|
|
14
20
|
tools: this.tools,
|
|
15
21
|
outputType: OutputSchema,
|
|
22
|
+
inputGuardrails: this.inputGuardrails,
|
|
23
|
+
outputGuardrails: [],
|
|
16
24
|
});
|
|
17
25
|
}
|
|
18
|
-
addExtraInstructions(
|
|
26
|
+
addExtraInstructions(initialInstructions, contactInfo) {
|
|
27
|
+
const instructions = `<instructions>\n${initialInstructions}\n</instructions>`;
|
|
28
|
+
const metadataInstructions = this.getMetadataInstructions();
|
|
29
|
+
const contactInfoInstructions = this.getContactInfoInstructions(contactInfo);
|
|
30
|
+
const outputInstructions = this.getOutputInstructions();
|
|
31
|
+
return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${outputInstructions}`;
|
|
32
|
+
}
|
|
33
|
+
getContactInfoInstructions(contactInfo) {
|
|
34
|
+
const structuredContactInfo = Object.entries(contactInfo)
|
|
35
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
36
|
+
.join('\n');
|
|
37
|
+
return `<contact_info>\n${structuredContactInfo}\n</contact_info>`;
|
|
38
|
+
}
|
|
39
|
+
getMetadataInstructions() {
|
|
19
40
|
const metadata = `Current Date: ${new Date().toISOString()}`;
|
|
41
|
+
return `<metadata>\n${metadata}\n</metadata>`;
|
|
42
|
+
}
|
|
43
|
+
getOutputInstructions() {
|
|
20
44
|
const example = {
|
|
21
45
|
messages: [
|
|
22
46
|
{
|
|
@@ -26,10 +50,9 @@ export class AIAgentBuilder {
|
|
|
26
50
|
},
|
|
27
51
|
},
|
|
28
52
|
],
|
|
29
|
-
numMessages: 1,
|
|
30
53
|
};
|
|
31
54
|
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>`;
|
|
32
|
-
return `<
|
|
55
|
+
return `<output>\n${output}\n</output>`;
|
|
33
56
|
}
|
|
34
57
|
addHubtypeTools(tools) {
|
|
35
58
|
const hubtypeTools = [...mandatoryTools];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-builder.js","sourceRoot":"","sources":["../../src/agent-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"agent-builder.js","sourceRoot":"","sources":["../../src/agent-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAkB,MAAM,gBAAgB,CAAA;AAEtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAGxC,MAAM,OAAO,cAAc;IAMzB,YACE,IAAY,EACZ,YAAoB,EACpB,KAAa,EACb,WAAwB,EACxB,mBAAoC;QAEpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,oBAAoB,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;QACxE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;QACxC,IAAI,CAAC,eAAe,GAAG,EAAE,CAAA;QACzB,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE;YAClC,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QAC3B,MAAM,OAAO,GAAG;YACd,QAAQ,EAAE;gBACR;oBACE,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE;wBACP,IAAI,EAAE,kCAAkC;qBACzC;iBACF;aACF;SACF,CAAA;QACD,MAAM,MAAM,GAAG,yIAAyI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,cAAc,CAAA;QAC7L,OAAO,aAAa,MAAM,aAAa,CAAA;IACzC,CAAC;IAEO,eAAe,CAAC,KAAa;QACnC,MAAM,YAAY,GAAW,CAAC,GAAG,cAAc,CAAC,CAAA;QAChD,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,KAAK,CAAC,CAAA;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './input';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/guardrails/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { Agent, run } from '@openai/agents';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
export function createInputGuardrail(rules) {
|
|
5
|
+
const outputType = z.object(Object.fromEntries(rules.map(rule => [rule.name, z.boolean().describe(rule.description)])));
|
|
6
|
+
const agent = new Agent({
|
|
7
|
+
name: 'InputGuardrail',
|
|
8
|
+
instructions: 'Check if the user triggers some of the following guardrails.',
|
|
9
|
+
outputType,
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
name: 'InputGuardrail',
|
|
13
|
+
execute: ({ input, context }) => __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
const lastMessage = input[input.length - 1];
|
|
15
|
+
const result = yield run(agent, [lastMessage], { context });
|
|
16
|
+
const finalOutput = result.finalOutput;
|
|
17
|
+
if (finalOutput === undefined) {
|
|
18
|
+
throw new Error('Guardrail agent failed to produce output');
|
|
19
|
+
}
|
|
20
|
+
const triggered = Object.values(finalOutput).some(value => value === true);
|
|
21
|
+
const triggeredGuardrails = Object.keys(finalOutput).filter(key => finalOutput[key] === true);
|
|
22
|
+
return {
|
|
23
|
+
outputInfo: triggeredGuardrails,
|
|
24
|
+
tripwireTriggered: triggered,
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../../src/guardrails/input.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAkB,GAAG,EAAmB,MAAM,gBAAgB,CAAA;AAC5E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,MAAM,UAAU,oBAAoB,CAAC,KAAsB;IACzD,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CACzB,MAAM,CAAC,WAAW,CAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CACvE,CACF,CAAA;IAED,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC;QACtB,IAAI,EAAE,gBAAgB;QACtB,YAAY,EACV,8DAA8D;QAChE,UAAU;KACX,CAAC,CAAA;IAEF,OAAO;QACL,IAAI,EAAE,gBAAgB;QACtB,OAAO,EAAE,CAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE;YACpC,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAoB,CAAA;YAC9D,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,KAAK,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;YAC3D,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAA;YACtC,IAAI,WAAW,KAAK,SAAS,EAAE;gBAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAA;aAC5D;YACD,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,CAAA;YAC1E,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CACzD,GAAG,CAAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,IAAI,CACjC,CAAA;YACD,OAAO;gBACL,UAAU,EAAE,mBAAmB;gBAC/B,iBAAiB,EAAE,SAAS;aAC7B,CAAA;QACH,CAAC,CAAA;KACF,CAAA;AACH,CAAC"}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { BotContext, Plugin } from '@botonic/core';
|
|
2
|
-
import {
|
|
2
|
+
import { AiAgentArgs, CustomTool, InferenceResponse, PluginAiAgentOptions } from './types';
|
|
3
3
|
export default class BotonicPluginAiAgents implements Plugin {
|
|
4
4
|
private readonly authToken?;
|
|
5
5
|
toolDefinitions: CustomTool[];
|
|
6
6
|
constructor(options?: PluginAiAgentOptions);
|
|
7
7
|
pre(): void;
|
|
8
|
-
getInference(request: BotContext, aiAgentArgs: AiAgentArgs): Promise<
|
|
8
|
+
getInference(request: BotContext, aiAgentArgs: AiAgentArgs): Promise<InferenceResponse>;
|
|
9
9
|
private getMessages;
|
|
10
10
|
private buildTools;
|
|
11
11
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -24,7 +24,7 @@ export default class BotonicPluginAiAgents {
|
|
|
24
24
|
throw new Error('Auth token is required');
|
|
25
25
|
}
|
|
26
26
|
const tools = this.buildTools(((_a = aiAgentArgs.activeTools) === null || _a === void 0 ? void 0 : _a.map(tool => tool.name)) || []);
|
|
27
|
-
const agent = new AIAgentBuilder(aiAgentArgs.name, aiAgentArgs.instructions, tools).build();
|
|
27
|
+
const agent = new AIAgentBuilder(aiAgentArgs.name, aiAgentArgs.instructions, tools, request.session.user.contact_info || {}, aiAgentArgs.inputGuardrailRules || []).build();
|
|
28
28
|
const messages = yield this.getMessages(request, authToken, 25);
|
|
29
29
|
const context = {
|
|
30
30
|
authToken,
|
|
@@ -34,6 +34,8 @@ export default class BotonicPluginAiAgents {
|
|
|
34
34
|
}
|
|
35
35
|
catch (error) {
|
|
36
36
|
console.error('error plugin returns undefined', error);
|
|
37
|
+
// Here we can return a InferenceResponse as a exit
|
|
38
|
+
// but indicate that the inference failed
|
|
37
39
|
return undefined;
|
|
38
40
|
}
|
|
39
41
|
});
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AAErC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAWxC,MAAM,CAAC,OAAO,OAAO,qBAAqB;IAIxC,YAAY,OAA8B;QAFnC,oBAAe,GAAiB,EAAE,CAAA;QAGvC,WAAW,EAAE,CAAA;QACb,IAAI,CAAC,SAAS,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,CAAA;QACnC,IAAI,CAAC,eAAe,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,EAAE,CAAA;IACnD,CAAC;IAED,GAAG;QACD,OAAM;IACR,CAAC;IAEK,YAAY,CAChB,OAAmB,EACnB,WAAwB;;;YA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Aa,CAAC,WAAW,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;aAC9D;YACD,MAAM,aAAa,GAAG,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAA;YACrD,OAAO,MAAM,aAAa,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAA;QAC3D,CAAC;KAAA;IAEO,UAAU,CAAC,eAAyB;QAC1C,MAAM,cAAc,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CACxD,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CACpC,CAAA;QACD,OAAO,cAAc,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE;YACzC,OAAO,IAAI,CAAoB;gBAC7B,IAAI,EAAE,cAAc,CAAC,IAAI;gBACzB,WAAW,EAAE,cAAc,CAAC,WAAW;gBACvC,UAAU,EAAE,cAAc,CAAC,MAAM;gBACjC,OAAO,EAAE,cAAc,CAAC,IAAI;aAC7B,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAED,cAAc,SAAS,CAAA"}
|
package/lib/esm/runner.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { AgenticInputMessage,
|
|
1
|
+
import { AgenticInputMessage, AIAgent, Context, RunResult } from './types';
|
|
2
2
|
export declare class AIAgentRunner {
|
|
3
3
|
private agent;
|
|
4
4
|
constructor(agent: AIAgent);
|
|
5
|
-
run(messages: AgenticInputMessage[], context: Context, maxRetries?: number): Promise<
|
|
5
|
+
run(messages: AgenticInputMessage[], context: Context, maxRetries?: number): Promise<RunResult>;
|
|
6
6
|
private runWithRetry;
|
|
7
|
+
private getExecutedNameTools;
|
|
7
8
|
}
|
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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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,15 +1,22 @@
|
|
|
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
|
}
|
|
8
|
+
export type RunContext = OpenAIRunContext<Context>;
|
|
7
9
|
export interface CustomTool {
|
|
8
10
|
name: string;
|
|
9
11
|
description: string;
|
|
10
12
|
schema: ZodSchema;
|
|
11
|
-
func: (input?: any, runContext?: RunContext
|
|
13
|
+
func: (input?: any, runContext?: RunContext) => Promise<any>;
|
|
12
14
|
}
|
|
15
|
+
export interface GuardrailRule {
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
18
|
+
}
|
|
19
|
+
export type ContactInfo = Record<string, string>;
|
|
13
20
|
export type Tool = OpenAITool<Context>;
|
|
14
21
|
export type AIAgent = Agent<Context, typeof OutputSchema>;
|
|
15
22
|
export interface PluginAiAgentOptions {
|
|
@@ -22,6 +29,7 @@ export interface AiAgentArgs {
|
|
|
22
29
|
activeTools?: {
|
|
23
30
|
name: string;
|
|
24
31
|
}[];
|
|
32
|
+
inputGuardrailRules?: GuardrailRule[];
|
|
25
33
|
}
|
|
26
34
|
export type AgenticInputMessage = AgentInputItem;
|
|
27
|
-
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.37.0",
|
|
3
|
+
"version": "0.37.2-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.37.1-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
|
}
|