@botonic/plugin-ai-agents 0.37.1 → 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/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
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { AIAgent, Tool } from './types';
|
|
1
|
+
import { AIAgent, ContactInfo, GuardrailRule, Tool } from './types';
|
|
2
2
|
export declare class AIAgentBuilder {
|
|
3
3
|
private name;
|
|
4
4
|
private instructions;
|
|
5
5
|
private tools;
|
|
6
|
-
|
|
6
|
+
private inputGuardrails;
|
|
7
|
+
constructor(name: string, instructions: string, tools: Tool[], contactInfo: ContactInfo, inputGuardrailRules: GuardrailRule[]);
|
|
7
8
|
build(): AIAgent;
|
|
8
9
|
private addExtraInstructions;
|
|
10
|
+
private getContactInfoInstructions;
|
|
11
|
+
private getMetadataInstructions;
|
|
12
|
+
private getOutputInstructions;
|
|
9
13
|
private addHubtypeTools;
|
|
10
14
|
}
|
package/lib/cjs/agent-builder.js
CHANGED
|
@@ -2,13 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AIAgentBuilder = void 0;
|
|
4
4
|
const agents_1 = require("@openai/agents");
|
|
5
|
+
const guardrails_1 = require("./guardrails");
|
|
5
6
|
const structured_output_1 = require("./structured-output");
|
|
6
7
|
const tools_1 = require("./tools");
|
|
7
8
|
class AIAgentBuilder {
|
|
8
|
-
constructor(name, instructions, tools) {
|
|
9
|
+
constructor(name, instructions, tools, contactInfo, inputGuardrailRules) {
|
|
9
10
|
this.name = name;
|
|
10
|
-
this.instructions = this.addExtraInstructions(instructions);
|
|
11
|
+
this.instructions = this.addExtraInstructions(instructions, contactInfo);
|
|
11
12
|
this.tools = this.addHubtypeTools(tools);
|
|
13
|
+
this.inputGuardrails = [];
|
|
14
|
+
if (inputGuardrailRules.length > 0) {
|
|
15
|
+
const inputGuardrail = (0, guardrails_1.createInputGuardrail)(inputGuardrailRules);
|
|
16
|
+
this.inputGuardrails.push(inputGuardrail);
|
|
17
|
+
}
|
|
12
18
|
}
|
|
13
19
|
build() {
|
|
14
20
|
return new agents_1.Agent({
|
|
@@ -16,10 +22,28 @@ class AIAgentBuilder {
|
|
|
16
22
|
instructions: this.instructions,
|
|
17
23
|
tools: this.tools,
|
|
18
24
|
outputType: structured_output_1.OutputSchema,
|
|
25
|
+
inputGuardrails: this.inputGuardrails,
|
|
26
|
+
outputGuardrails: [],
|
|
19
27
|
});
|
|
20
28
|
}
|
|
21
|
-
addExtraInstructions(
|
|
29
|
+
addExtraInstructions(initialInstructions, contactInfo) {
|
|
30
|
+
const instructions = `<instructions>\n${initialInstructions}\n</instructions>`;
|
|
31
|
+
const metadataInstructions = this.getMetadataInstructions();
|
|
32
|
+
const contactInfoInstructions = this.getContactInfoInstructions(contactInfo);
|
|
33
|
+
const outputInstructions = this.getOutputInstructions();
|
|
34
|
+
return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${outputInstructions}`;
|
|
35
|
+
}
|
|
36
|
+
getContactInfoInstructions(contactInfo) {
|
|
37
|
+
const structuredContactInfo = Object.entries(contactInfo)
|
|
38
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
39
|
+
.join('\n');
|
|
40
|
+
return `<contact_info>\n${structuredContactInfo}\n</contact_info>`;
|
|
41
|
+
}
|
|
42
|
+
getMetadataInstructions() {
|
|
22
43
|
const metadata = `Current Date: ${new Date().toISOString()}`;
|
|
44
|
+
return `<metadata>\n${metadata}\n</metadata>`;
|
|
45
|
+
}
|
|
46
|
+
getOutputInstructions() {
|
|
23
47
|
const example = {
|
|
24
48
|
messages: [
|
|
25
49
|
{
|
|
@@ -29,10 +53,9 @@ class AIAgentBuilder {
|
|
|
29
53
|
},
|
|
30
54
|
},
|
|
31
55
|
],
|
|
32
|
-
numMessages: 1,
|
|
33
56
|
};
|
|
34
57
|
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>`;
|
|
35
|
-
return `<
|
|
58
|
+
return `<output>\n${output}\n</output>`;
|
|
36
59
|
}
|
|
37
60
|
addHubtypeTools(tools) {
|
|
38
61
|
const hubtypeTools = [...tools_1.mandatoryTools];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-builder.js","sourceRoot":"","sources":["../../src/agent-builder.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"agent-builder.js","sourceRoot":"","sources":["../../src/agent-builder.ts"],"names":[],"mappings":";;;AAAA,2CAAsD;AAEtD,6CAAmD;AACnD,2DAAkD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qBAAqB,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC;aACtD,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,KAAK,KAAK,EAAE,CAAC;aACzC,IAAI,CAAC,IAAI,CAAC,CAAA;QACb,OAAO,mBAAmB,qBAAqB,mBAAmB,CAAA;IACpE,CAAC;IAEO,uBAAuB;QAC7B,MAAM,QAAQ,GAAG,iBAAiB,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAA;QAC5D,OAAO,eAAe,QAAQ,eAAe,CAAA;IAC/C,CAAC;IAEO,qBAAqB;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,sBAAc,CAAC,CAAA;QAChD,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,KAAK,CAAC,CAAA;IACpC,CAAC;CACF;AA5ED,wCA4EC"}
|
|
@@ -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,kDAAuB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInputGuardrail = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const agents_1 = require("@openai/agents");
|
|
6
|
+
const zod_1 = require("zod");
|
|
7
|
+
function createInputGuardrail(rules) {
|
|
8
|
+
const outputType = zod_1.z.object(Object.fromEntries(rules.map(rule => [rule.name, zod_1.z.boolean().describe(rule.description)])));
|
|
9
|
+
const agent = new agents_1.Agent({
|
|
10
|
+
name: 'InputGuardrail',
|
|
11
|
+
instructions: 'Check if the user triggers some of the following guardrails.',
|
|
12
|
+
outputType,
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
name: 'InputGuardrail',
|
|
16
|
+
execute: ({ input, context }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const lastMessage = input[input.length - 1];
|
|
18
|
+
const result = yield (0, agents_1.run)(agent, [lastMessage], { context });
|
|
19
|
+
const finalOutput = result.finalOutput;
|
|
20
|
+
if (finalOutput === undefined) {
|
|
21
|
+
throw new Error('Guardrail agent failed to produce output');
|
|
22
|
+
}
|
|
23
|
+
const triggered = Object.values(finalOutput).some(value => value === true);
|
|
24
|
+
const triggeredGuardrails = Object.keys(finalOutput).filter(key => finalOutput[key] === true);
|
|
25
|
+
return {
|
|
26
|
+
outputInfo: triggeredGuardrails,
|
|
27
|
+
tripwireTriggered: triggered,
|
|
28
|
+
};
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.createInputGuardrail = createInputGuardrail;
|
|
33
|
+
//# sourceMappingURL=input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../../src/guardrails/input.ts"],"names":[],"mappings":";;;;AAAA,2CAA4E;AAC5E,6BAAuB;AAIvB,SAAgB,oBAAoB,CAAC,KAAsB;IACzD,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CACzB,MAAM,CAAC,WAAW,CAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,OAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CACvE,CACF,CAAA;IAED,MAAM,KAAK,GAAG,IAAI,cAAK,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,IAAA,YAAG,EAAC,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;AAjCD,oDAiCC"}
|
package/lib/cjs/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/cjs/index.js
CHANGED
|
@@ -26,7 +26,7 @@ class BotonicPluginAiAgents {
|
|
|
26
26
|
throw new Error('Auth token is required');
|
|
27
27
|
}
|
|
28
28
|
const tools = this.buildTools(((_a = aiAgentArgs.activeTools) === null || _a === void 0 ? void 0 : _a.map(tool => tool.name)) || []);
|
|
29
|
-
const agent = new agent_builder_1.AIAgentBuilder(aiAgentArgs.name, aiAgentArgs.instructions, tools).build();
|
|
29
|
+
const agent = new agent_builder_1.AIAgentBuilder(aiAgentArgs.name, aiAgentArgs.instructions, tools, request.session.user.contact_info || {}, aiAgentArgs.inputGuardrailRules || []).build();
|
|
30
30
|
const messages = yield this.getMessages(request, authToken, 25);
|
|
31
31
|
const context = {
|
|
32
32
|
authToken,
|
|
@@ -36,6 +36,8 @@ class BotonicPluginAiAgents {
|
|
|
36
36
|
}
|
|
37
37
|
catch (error) {
|
|
38
38
|
console.error('error plugin returns undefined', error);
|
|
39
|
+
// Here we can return a InferenceResponse as a exit
|
|
40
|
+
// but indicate that the inference failed
|
|
39
41
|
return undefined;
|
|
40
42
|
}
|
|
41
43
|
});
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AACA,2CAAqC;AAErC,mDAAgD;AAChD,2CAAoC;AACpC,6DAAuD;AACvD,qCAAsC;AACtC,qCAAwC;AAWxC,MAAqB,qBAAqB;IAIxC,YAAY,OAA8B;QAFn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;;YAEpB,IAAI,kBAAM,EAAE;gBACV,MAAM,aAAa,GAAG,IAAI,qCAAgB,CAAC,SAAS,CAAC,CAAA;gBACrD,OAAO,MAAM,aAAa,CAAC,WAAW,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;aAC9D;YACD,MAAM,aAAa,GAAG,IAAI,qCAAgB,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,IAAA,aAAI,EAAoB;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;AA5ED,wCA4EC;AAED,kDAAuB"}
|
package/lib/cjs/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/cjs/runner.js
CHANGED
|
@@ -20,9 +20,30 @@ class AIAgentRunner {
|
|
|
20
20
|
modelSettings: { temperature: 0 },
|
|
21
21
|
});
|
|
22
22
|
const result = yield runner.run(this.agent, messages, { context });
|
|
23
|
-
|
|
23
|
+
const outputMessages = ((_a = result.finalOutput) === null || _a === void 0 ? void 0 : _a.messages) || [];
|
|
24
|
+
const hasExit = outputMessages.length === 0 ||
|
|
25
|
+
outputMessages.some(message => message.type === 'exit');
|
|
26
|
+
const toolsExecuted = this.getExecutedNameTools(result);
|
|
27
|
+
return {
|
|
28
|
+
messages: hasExit
|
|
29
|
+
? []
|
|
30
|
+
: outputMessages.filter(message => message.type !== 'exit'),
|
|
31
|
+
toolsExecuted,
|
|
32
|
+
exit: hasExit,
|
|
33
|
+
inputGuardrailTriggered: [],
|
|
34
|
+
outputGuardrailTriggered: [],
|
|
35
|
+
};
|
|
24
36
|
}
|
|
25
37
|
catch (error) {
|
|
38
|
+
if (error instanceof agents_1.InputGuardrailTripwireTriggered) {
|
|
39
|
+
return {
|
|
40
|
+
messages: [],
|
|
41
|
+
toolsExecuted: [],
|
|
42
|
+
exit: true,
|
|
43
|
+
inputGuardrailTriggered: error.result.output.outputInfo,
|
|
44
|
+
outputGuardrailTriggered: [],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
26
47
|
if (attempt > maxRetries) {
|
|
27
48
|
throw error;
|
|
28
49
|
}
|
|
@@ -30,6 +51,15 @@ class AIAgentRunner {
|
|
|
30
51
|
}
|
|
31
52
|
});
|
|
32
53
|
}
|
|
54
|
+
getExecutedNameTools(result) {
|
|
55
|
+
var _a;
|
|
56
|
+
return (((_a = result.newItems) === null || _a === void 0 ? void 0 : _a.filter(item => item instanceof agents_1.RunToolCallItem).map((item) => {
|
|
57
|
+
if (item.rawItem.type === 'function_call') {
|
|
58
|
+
return item.rawItem.name;
|
|
59
|
+
}
|
|
60
|
+
return '';
|
|
61
|
+
}).filter((toolName) => toolName !== '')) || []);
|
|
62
|
+
}
|
|
33
63
|
}
|
|
34
64
|
exports.AIAgentRunner = AIAgentRunner;
|
|
35
65
|
//# sourceMappingURL=runner.js.map
|
package/lib/cjs/runner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";;;;AAAA,2CAIuB;AAUvB,MAAa,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,eAAM,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,EA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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,6BAAuB;AAIV,QAAA,cAAc,GAAG,OAAC;KAC5B,MAAM,CAAC;IACN,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC;IAC1B,OAAO,EAAE,OAAC,CAAC,MAAM,CAAC;QAChB,QAAQ,EAAE,OAAC,CAAC,KAAK,CACf,OAAC,CAAC,MAAM,CAAC;YACP,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE;YACjB,QAAQ,EAAE,OAAC,CAAC,MAAM,EAAE;YACpB,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,OAAC,CAAC,MAAM,CAAC;gBACf,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;gBAChB,GAAG,EAAE,OAAC,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,sDAAmB;AAIN,QAAA,UAAU,GAAG,aAAC;KACxB,MAAM,CAAC;IACN,IAAI,EAAE,aAAC,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
|
}
|
|
@@ -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,6BAAuB;AAEvB,yCAA2C;AAC3C,iCAAmC;AACnC,iCAAmC;AACnC,2DAA2D;AAM9C,QAAA,YAAY,GAAG,OAAC;KAC1B,MAAM,CAAC;IACN,QAAQ,EAAE,OAAC,CAAC,KAAK,CACf,OAAC,CAAC,KAAK,CAAC,CAAC,iBAAU,EAAE,yCAAqB,EAAE,yBAAc,EAAE,iBAAU,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,sDAAmB;AAIN,QAAA,qBAAqB,GAAG,aAAC;KACnC,MAAM,CAAC;IACN,IAAI,EAAE,aAAC,CAAC,IAAI,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACjC,OAAO,EAAE,aAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,aAAC,CAAC,MAAM,EAAE;QAChB,OAAO,EAAE,aAAC,CAAC,KAAK,CAAC,aAAC,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,6BAAuB;AAIV,QAAA,UAAU,GAAG,OAAC;KACxB,MAAM,CAAC;IACN,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,EAAE,OAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;KACjB,CAAC;CACH,CAAC;KACD,QAAQ,CAAC,gBAAgB,CAAC,CAAA"}
|
package/lib/cjs/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 };
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { AIAgent, Tool } from './types';
|
|
1
|
+
import { AIAgent, ContactInfo, GuardrailRule, Tool } from './types';
|
|
2
2
|
export declare class AIAgentBuilder {
|
|
3
3
|
private name;
|
|
4
4
|
private instructions;
|
|
5
5
|
private tools;
|
|
6
|
-
|
|
6
|
+
private inputGuardrails;
|
|
7
|
+
constructor(name: string, instructions: string, tools: Tool[], contactInfo: ContactInfo, inputGuardrailRules: GuardrailRule[]);
|
|
7
8
|
build(): AIAgent;
|
|
8
9
|
private addExtraInstructions;
|
|
10
|
+
private getContactInfoInstructions;
|
|
11
|
+
private getMetadataInstructions;
|
|
12
|
+
private getOutputInstructions;
|
|
9
13
|
private addHubtypeTools;
|
|
10
14
|
}
|
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,G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,CAAA;QAC5D,OAAO,eAAe,QAAQ,eAAe,CAAA;IAC/C,CAAC;IAEO,qBAAqB;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;IA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,aAAa,GAAG,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAA;gBACrD,OAAO,MAAM,aAAa,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
|
}
|