@botonic/plugin-ai-agents 0.46.1 → 2.23.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/CHANGELOG.md +209 -0
- package/package.json +23 -41
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -0
- package/{lib/cjs → src/lib}/agent-builder.d.ts +5 -13
- package/{lib/cjs → src/lib}/agent-builder.js +31 -26
- package/src/lib/agent-builder.js.map +1 -0
- package/{lib/cjs → src/lib}/constants.d.ts +1 -3
- package/src/lib/constants.js +13 -0
- package/src/lib/constants.js.map +1 -0
- package/{lib/esm → src/lib}/debug-logger.d.ts +3 -5
- package/{lib/esm → src/lib}/debug-logger.js +44 -29
- package/src/lib/debug-logger.js.map +1 -0
- package/src/lib/guardrails/index.js +2 -0
- package/src/lib/guardrails/index.js.map +1 -0
- package/src/lib/guardrails/input.d.ts +3 -0
- package/src/lib/guardrails/input.js +29 -0
- package/src/lib/guardrails/input.js.map +1 -0
- package/{lib/cjs/services → src/lib}/hubtype-api-client.d.ts +13 -5
- package/src/lib/hubtype-api-client.js +206 -0
- package/src/lib/hubtype-api-client.js.map +1 -0
- package/{lib/esm → src/lib}/index.d.ts +2 -7
- package/src/lib/index.js +111 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/openai.d.ts +1 -0
- package/src/lib/openai.js +21 -0
- package/src/lib/openai.js.map +1 -0
- package/src/lib/runner.d.ts +12 -0
- package/src/lib/runner.js +96 -0
- package/src/lib/runner.js.map +1 -0
- package/{lib/esm → src/lib}/structured-output/carousel.d.ts +1 -1
- package/src/lib/structured-output/carousel.js +19 -0
- package/src/lib/structured-output/carousel.js.map +1 -0
- package/{lib/cjs → src/lib}/structured-output/exit.d.ts +1 -1
- package/src/lib/structured-output/exit.js +7 -0
- package/src/lib/structured-output/exit.js.map +1 -0
- package/src/lib/structured-output/index.d.ts +228 -0
- package/src/lib/structured-output/index.js +11 -0
- package/src/lib/structured-output/index.js.map +1 -0
- package/{lib/esm/structured-output/bot-executor.d.ts → src/lib/structured-output/text-with-buttons.d.ts} +13 -13
- package/src/lib/structured-output/text-with-buttons.js +14 -0
- package/src/lib/structured-output/text-with-buttons.js.map +1 -0
- package/{lib/esm → src/lib}/structured-output/text.d.ts +1 -1
- package/src/lib/structured-output/text.js +10 -0
- package/src/lib/structured-output/text.js.map +1 -0
- package/{lib/esm → src/lib}/tools/index.d.ts +1 -1
- package/src/lib/tools/index.js +3 -0
- package/src/lib/tools/index.js.map +1 -0
- package/{lib/cjs → src/lib}/tools/retrieve-knowledge.d.ts +2 -2
- package/src/lib/tools/retrieve-knowledge.js +30 -0
- package/src/lib/tools/retrieve-knowledge.js.map +1 -0
- package/{lib/esm → src/lib}/types.d.ts +11 -21
- package/src/lib/types.js +2 -0
- package/src/lib/types.js.map +1 -0
- package/README.md +0 -156
- package/lib/cjs/agent-builder.js.map +0 -1
- package/lib/cjs/bot-config-tools.d.ts +0 -7
- package/lib/cjs/bot-config-tools.js +0 -19
- package/lib/cjs/bot-config-tools.js.map +0 -1
- package/lib/cjs/constants.js +0 -17
- package/lib/cjs/constants.js.map +0 -1
- package/lib/cjs/debug-logger.d.ts +0 -33
- package/lib/cjs/debug-logger.js +0 -97
- package/lib/cjs/debug-logger.js.map +0 -1
- package/lib/cjs/guardrails/index.js +0 -5
- package/lib/cjs/guardrails/index.js.map +0 -1
- package/lib/cjs/guardrails/input.d.ts +0 -10
- package/lib/cjs/guardrails/input.js +0 -77
- package/lib/cjs/guardrails/input.js.map +0 -1
- package/lib/cjs/index.d.ts +0 -18
- package/lib/cjs/index.js +0 -134
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/llm-config.d.ts +0 -15
- package/lib/cjs/llm-config.js +0 -66
- package/lib/cjs/llm-config.js.map +0 -1
- package/lib/cjs/runner.d.ts +0 -18
- package/lib/cjs/runner.js +0 -181
- package/lib/cjs/runner.js.map +0 -1
- package/lib/cjs/services/hubtype-api-client.js +0 -206
- package/lib/cjs/services/hubtype-api-client.js.map +0 -1
- package/lib/cjs/services/types.d.ts +0 -85
- package/lib/cjs/services/types.js +0 -13
- package/lib/cjs/services/types.js.map +0 -1
- package/lib/cjs/structured-output/bot-executor.d.ts +0 -49
- package/lib/cjs/structured-output/bot-executor.js +0 -18
- package/lib/cjs/structured-output/bot-executor.js.map +0 -1
- package/lib/cjs/structured-output/carousel.d.ts +0 -90
- package/lib/cjs/structured-output/carousel.js +0 -22
- package/lib/cjs/structured-output/carousel.js.map +0 -1
- package/lib/cjs/structured-output/exit.js +0 -11
- package/lib/cjs/structured-output/exit.js.map +0 -1
- package/lib/cjs/structured-output/index.d.ts +0 -603
- package/lib/cjs/structured-output/index.js +0 -28
- package/lib/cjs/structured-output/index.js.map +0 -1
- package/lib/cjs/structured-output/text-with-buttons.d.ts +0 -56
- package/lib/cjs/structured-output/text-with-buttons.js +0 -24
- package/lib/cjs/structured-output/text-with-buttons.js.map +0 -1
- package/lib/cjs/structured-output/text.d.ts +0 -23
- package/lib/cjs/structured-output/text.js +0 -13
- package/lib/cjs/structured-output/text.js.map +0 -1
- package/lib/cjs/tools/index.d.ts +0 -3
- package/lib/cjs/tools/index.js +0 -7
- package/lib/cjs/tools/index.js.map +0 -1
- package/lib/cjs/tools/retrieve-knowledge.js +0 -32
- package/lib/cjs/tools/retrieve-knowledge.js.map +0 -1
- package/lib/cjs/types.d.ts +0 -56
- package/lib/cjs/types.js +0 -3
- package/lib/cjs/types.js.map +0 -1
- package/lib/esm/agent-builder.d.ts +0 -37
- package/lib/esm/agent-builder.js +0 -106
- package/lib/esm/agent-builder.js.map +0 -1
- package/lib/esm/bot-config-tools.d.ts +0 -7
- package/lib/esm/bot-config-tools.js +0 -19
- package/lib/esm/bot-config-tools.js.map +0 -1
- package/lib/esm/constants.d.ts +0 -11
- package/lib/esm/constants.js +0 -17
- package/lib/esm/constants.js.map +0 -1
- package/lib/esm/debug-logger.js.map +0 -1
- package/lib/esm/guardrails/index.d.ts +0 -1
- package/lib/esm/guardrails/index.js +0 -5
- package/lib/esm/guardrails/index.js.map +0 -1
- package/lib/esm/guardrails/input.d.ts +0 -10
- package/lib/esm/guardrails/input.js +0 -77
- package/lib/esm/guardrails/input.js.map +0 -1
- package/lib/esm/index.js +0 -134
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/llm-config.d.ts +0 -15
- package/lib/esm/llm-config.js +0 -66
- package/lib/esm/llm-config.js.map +0 -1
- package/lib/esm/runner.d.ts +0 -18
- package/lib/esm/runner.js +0 -181
- package/lib/esm/runner.js.map +0 -1
- package/lib/esm/services/hubtype-api-client.d.ts +0 -14
- package/lib/esm/services/hubtype-api-client.js +0 -206
- package/lib/esm/services/hubtype-api-client.js.map +0 -1
- package/lib/esm/services/types.d.ts +0 -85
- package/lib/esm/services/types.js +0 -13
- package/lib/esm/services/types.js.map +0 -1
- package/lib/esm/structured-output/bot-executor.js +0 -18
- package/lib/esm/structured-output/bot-executor.js.map +0 -1
- package/lib/esm/structured-output/carousel.js +0 -22
- package/lib/esm/structured-output/carousel.js.map +0 -1
- package/lib/esm/structured-output/exit.d.ts +0 -10
- package/lib/esm/structured-output/exit.js +0 -11
- package/lib/esm/structured-output/exit.js.map +0 -1
- package/lib/esm/structured-output/index.d.ts +0 -603
- package/lib/esm/structured-output/index.js +0 -28
- package/lib/esm/structured-output/index.js.map +0 -1
- package/lib/esm/structured-output/text-with-buttons.d.ts +0 -56
- package/lib/esm/structured-output/text-with-buttons.js +0 -24
- package/lib/esm/structured-output/text-with-buttons.js.map +0 -1
- package/lib/esm/structured-output/text.js +0 -13
- package/lib/esm/structured-output/text.js.map +0 -1
- package/lib/esm/tools/index.js +0 -7
- package/lib/esm/tools/index.js.map +0 -1
- package/lib/esm/tools/retrieve-knowledge.d.ts +0 -9
- package/lib/esm/tools/retrieve-knowledge.js +0 -32
- package/lib/esm/tools/retrieve-knowledge.js.map +0 -1
- package/lib/esm/types.js +0 -3
- package/lib/esm/types.js.map +0 -1
- package/src/agent-builder.ts +0 -183
- package/src/bot-config-tools.ts +0 -21
- package/src/constants.ts +0 -21
- package/src/debug-logger.ts +0 -166
- package/src/guardrails/index.ts +0 -1
- package/src/guardrails/input.ts +0 -122
- package/src/index.ts +0 -210
- package/src/llm-config.ts +0 -96
- package/src/runner.ts +0 -287
- package/src/services/hubtype-api-client.ts +0 -240
- package/src/services/types.ts +0 -106
- package/src/structured-output/bot-executor.ts +0 -21
- package/src/structured-output/carousel.ts +0 -24
- package/src/structured-output/exit.ts +0 -12
- package/src/structured-output/index.ts +0 -34
- package/src/structured-output/text-with-buttons.ts +0 -29
- package/src/structured-output/text.ts +0 -13
- package/src/tools/index.ts +0 -5
- package/src/tools/retrieve-knowledge.ts +0 -37
- package/src/types.ts +0 -110
- /package/{lib/cjs → src/lib}/guardrails/index.d.ts +0 -0
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TextWithButtonsSchema = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const zod_1 = tslib_1.__importDefault(require("zod"));
|
|
6
|
-
exports.TextWithButtonsSchema = zod_1.default
|
|
7
|
-
.object({
|
|
8
|
-
type: zod_1.default.enum(['textWithButtons']),
|
|
9
|
-
content: zod_1.default.object({
|
|
10
|
-
text: zod_1.default.string(),
|
|
11
|
-
buttons: zod_1.default.array(zod_1.default.object({
|
|
12
|
-
text: zod_1.default.string(),
|
|
13
|
-
url: zod_1.default.string().nullable().optional(),
|
|
14
|
-
target: zod_1.default
|
|
15
|
-
.enum(['_blank', '_self'])
|
|
16
|
-
.default('_blank')
|
|
17
|
-
.nullable()
|
|
18
|
-
.optional()
|
|
19
|
-
.describe('The target of the button when it has an url. If not provided, it will default to _blank.'),
|
|
20
|
-
})),
|
|
21
|
-
}),
|
|
22
|
-
})
|
|
23
|
-
.describe('A text message with buttons to allow the user to use quick replies');
|
|
24
|
-
//# sourceMappingURL=text-with-buttons.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,CACd,aAAC,CAAC,MAAM,CAAC;YACP,IAAI,EAAE,aAAC,CAAC,MAAM,EAAE;YAChB,GAAG,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;YACrC,MAAM,EAAE,aAAC;iBACN,IAAI,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;iBACzB,OAAO,CAAC,QAAQ,CAAC;iBACjB,QAAQ,EAAE;iBACV,QAAQ,EAAE;iBACV,QAAQ,CACP,0FAA0F,CAC3F;SACJ,CAAC,CACH;KACF,CAAC;CACH,CAAC;KACD,QAAQ,CACP,oEAAoE,CACrE,CAAA"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TextSchema = void 0;
|
|
4
|
-
const zod_1 = require("zod");
|
|
5
|
-
exports.TextSchema = zod_1.z
|
|
6
|
-
.object({
|
|
7
|
-
type: zod_1.z.enum(['text']),
|
|
8
|
-
content: zod_1.z.object({
|
|
9
|
-
text: zod_1.z.string(),
|
|
10
|
-
}),
|
|
11
|
-
})
|
|
12
|
-
.describe('A text message');
|
|
13
|
-
//# sourceMappingURL=text.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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/esm/tools/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mandatoryTools = exports.retrieveKnowledge = void 0;
|
|
4
|
-
var retrieve_knowledge_1 = require("./retrieve-knowledge");
|
|
5
|
-
Object.defineProperty(exports, "retrieveKnowledge", { enumerable: true, get: function () { return retrieve_knowledge_1.retrieveKnowledge; } });
|
|
6
|
-
exports.mandatoryTools = [];
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tools/index.ts"],"names":[],"mappings":";;;AAEA,2DAAwD;AAA/C,uHAAA,iBAAiB,OAAA;AAEb,QAAA,cAAc,GAAW,EAAE,CAAA"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import type { Context } from '../types';
|
|
3
|
-
export declare const retrieveKnowledge: import("@openai/agents").FunctionTool<Context<import("@botonic/core").ResolvedPlugins, any>, z.ZodObject<{
|
|
4
|
-
query: z.ZodString;
|
|
5
|
-
}, "strip", z.ZodTypeAny, {
|
|
6
|
-
query: string;
|
|
7
|
-
}, {
|
|
8
|
-
query: string;
|
|
9
|
-
}>, string>;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.retrieveKnowledge = void 0;
|
|
4
|
-
const agents_1 = require("@openai/agents");
|
|
5
|
-
const zod_1 = require("zod");
|
|
6
|
-
const hubtype_api_client_1 = require("../services/hubtype-api-client");
|
|
7
|
-
exports.retrieveKnowledge = (0, agents_1.tool)({
|
|
8
|
-
name: 'retrieve_knowledge',
|
|
9
|
-
description: 'Consult the knowledge base for information before answering. Use this tool to make sure the information you provide is faithful.',
|
|
10
|
-
parameters: zod_1.z.object({
|
|
11
|
-
query: zod_1.z.string().describe('The query to search the knowledge base for'),
|
|
12
|
-
}),
|
|
13
|
-
execute: async (input, runContext) => {
|
|
14
|
-
const context = runContext?.context;
|
|
15
|
-
const query = input.query;
|
|
16
|
-
if (!context) {
|
|
17
|
-
throw new Error('Context is required');
|
|
18
|
-
}
|
|
19
|
-
const sourceIds = context.sourceIds;
|
|
20
|
-
const client = new hubtype_api_client_1.HubtypeApiClient(context.authToken);
|
|
21
|
-
const chunks = await client.retrieveSimilarChunks(query, sourceIds);
|
|
22
|
-
const chunkTexts = chunks.map(chunk => chunk.text);
|
|
23
|
-
context.knowledgeUsed = {
|
|
24
|
-
query,
|
|
25
|
-
sourceIds,
|
|
26
|
-
chunksIds: chunks.map(chunk => chunk.id),
|
|
27
|
-
chunkTexts,
|
|
28
|
-
};
|
|
29
|
-
return chunkTexts;
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
//# sourceMappingURL=retrieve-knowledge.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retrieve-knowledge.js","sourceRoot":"","sources":["../../../src/tools/retrieve-knowledge.ts"],"names":[],"mappings":";;;AAAA,2CAAsD;AACtD,6BAAuB;AAEvB,uEAAiE;AAGpD,QAAA,iBAAiB,GAAG,IAAA,aAAI,EAAC;IACpC,IAAI,EAAE,oBAAoB;IAC1B,WAAW,EACT,kIAAkI;IACpI,UAAU,EAAE,OAAC,CAAC,MAAM,CAAC;QACnB,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,4CAA4C,CAAC;KACzE,CAAC;IACF,OAAO,EAAE,KAAK,EACZ,KAAwB,EACxB,UAAgC,EACb,EAAE;QACrB,MAAM,OAAO,GAAG,UAAU,EAAE,OAAO,CAAA;QACnC,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAA;QACzB,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QACxC,CAAC;QACD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;QACnC,MAAM,MAAM,GAAG,IAAI,qCAAgB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QACtD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,qBAAqB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;QACnE,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QAElD,OAAO,CAAC,aAAa,GAAG;YACtB,KAAK;YACL,SAAS;YACT,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC;YACxC,UAAU;SACX,CAAA;QAED,OAAO,UAAU,CAAA;IACnB,CAAC;CACF,CAAC,CAAA"}
|
package/lib/esm/types.js
DELETED
package/lib/esm/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/src/agent-builder.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type { CampaignV2, ContactInfo, ResolvedPlugins } from '@botonic/core'
|
|
2
|
-
import {
|
|
3
|
-
Agent,
|
|
4
|
-
type AgentOutputType,
|
|
5
|
-
type InputGuardrail,
|
|
6
|
-
} from '@openai/agents'
|
|
7
|
-
import type { z } from 'zod'
|
|
8
|
-
|
|
9
|
-
import { OPENAI_PROVIDER } from './constants'
|
|
10
|
-
import type { DebugLogger } from './debug-logger'
|
|
11
|
-
import { createInputGuardrail } from './guardrails'
|
|
12
|
-
import type { GuardrailTrackingContext } from './guardrails/input'
|
|
13
|
-
import type { LLMConfig } from './llm-config'
|
|
14
|
-
import { getOutputSchema, type OutputSchema } from './structured-output'
|
|
15
|
-
import { mandatoryTools, retrieveKnowledge } from './tools'
|
|
16
|
-
import type { AIAgent, Context, GuardrailRule, Tool } from './types'
|
|
17
|
-
|
|
18
|
-
interface AIAgentBuilderOptions<
|
|
19
|
-
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
20
|
-
TExtraData = any,
|
|
21
|
-
> {
|
|
22
|
-
name: string
|
|
23
|
-
instructions: string
|
|
24
|
-
tools: Tool<TPlugins, TExtraData>[]
|
|
25
|
-
campaignsContext?: CampaignV2[]
|
|
26
|
-
contactInfo: ContactInfo[]
|
|
27
|
-
inputGuardrailRules: GuardrailRule[]
|
|
28
|
-
sourceIds: string[]
|
|
29
|
-
outputMessagesSchemas?: z.ZodObject<any>[]
|
|
30
|
-
llmConfig: LLMConfig
|
|
31
|
-
logger: DebugLogger
|
|
32
|
-
guardrailTrackingContext: GuardrailTrackingContext
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export class AIAgentBuilder<
|
|
36
|
-
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
37
|
-
TExtraData = any,
|
|
38
|
-
> {
|
|
39
|
-
private name: string
|
|
40
|
-
private instructions: string
|
|
41
|
-
private tools: Tool<TPlugins, TExtraData>[]
|
|
42
|
-
private externalOutputMessagesSchemas: z.ZodObject<any>[]
|
|
43
|
-
private inputGuardrails: InputGuardrail[]
|
|
44
|
-
public llmConfig: LLMConfig
|
|
45
|
-
private logger: DebugLogger
|
|
46
|
-
|
|
47
|
-
constructor(options: AIAgentBuilderOptions<TPlugins, TExtraData>) {
|
|
48
|
-
this.name = options.name
|
|
49
|
-
this.instructions = this.addExtraInstructions(
|
|
50
|
-
options.instructions,
|
|
51
|
-
options.contactInfo,
|
|
52
|
-
options.campaignsContext
|
|
53
|
-
)
|
|
54
|
-
this.tools = this.addHubtypeTools(options.tools, options.sourceIds)
|
|
55
|
-
this.externalOutputMessagesSchemas = options.outputMessagesSchemas || []
|
|
56
|
-
this.inputGuardrails = []
|
|
57
|
-
this.llmConfig = options.llmConfig
|
|
58
|
-
this.logger = options.logger
|
|
59
|
-
if (options.inputGuardrailRules.length > 0) {
|
|
60
|
-
const inputGuardrail = createInputGuardrail(
|
|
61
|
-
options.inputGuardrailRules,
|
|
62
|
-
options.llmConfig,
|
|
63
|
-
options.guardrailTrackingContext
|
|
64
|
-
)
|
|
65
|
-
this.inputGuardrails.push(inputGuardrail)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
build(): AIAgent<TPlugins, TExtraData> {
|
|
70
|
-
// When using standard OpenAI API, we need to specify the model
|
|
71
|
-
// Azure OpenAI uses deployment name instead
|
|
72
|
-
|
|
73
|
-
const model = this.llmConfig.modelName
|
|
74
|
-
const hasRetrieveKnowledge = this.tools.includes(retrieveKnowledge)
|
|
75
|
-
|
|
76
|
-
this.logger.logModelSettings({
|
|
77
|
-
provider: OPENAI_PROVIDER,
|
|
78
|
-
model,
|
|
79
|
-
reasoning: this.llmConfig.modelSettings.reasoning as
|
|
80
|
-
| { effort: string }
|
|
81
|
-
| undefined,
|
|
82
|
-
text: this.llmConfig.modelSettings.text as
|
|
83
|
-
| { verbosity: string }
|
|
84
|
-
| undefined,
|
|
85
|
-
toolChoice: this.llmConfig.modelSettings.toolChoice as string | undefined,
|
|
86
|
-
hasRetrieveKnowledge,
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
return new Agent<
|
|
90
|
-
Context<TPlugins, TExtraData>,
|
|
91
|
-
AgentOutputType<typeof OutputSchema>
|
|
92
|
-
>({
|
|
93
|
-
name: this.name,
|
|
94
|
-
model,
|
|
95
|
-
instructions: this.instructions,
|
|
96
|
-
tools: this.tools,
|
|
97
|
-
outputType: getOutputSchema(this.externalOutputMessagesSchemas),
|
|
98
|
-
inputGuardrails: this.inputGuardrails,
|
|
99
|
-
outputGuardrails: [],
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private addExtraInstructions(
|
|
104
|
-
initialInstructions: string,
|
|
105
|
-
contactInfo: ContactInfo[],
|
|
106
|
-
campaignsContext?: CampaignV2[]
|
|
107
|
-
): string {
|
|
108
|
-
const instructions = `<instructions>\n${initialInstructions.trim()}\n</instructions>`
|
|
109
|
-
const metadataInstructions = this.getMetadataInstructions()
|
|
110
|
-
const contactInfoInstructions = this.getContactInfoInstructions(contactInfo)
|
|
111
|
-
const campaignInstructions = this.getCampaignInstructions(campaignsContext)
|
|
112
|
-
const outputInstructions = this.getOutputInstructions()
|
|
113
|
-
return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${campaignInstructions}\n\n${outputInstructions}`
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private getContactInfoInstructions(contactInfo: ContactInfo[]): string {
|
|
117
|
-
const structuredContactInfo = contactInfo
|
|
118
|
-
.map(
|
|
119
|
-
info =>
|
|
120
|
-
` <contact_info>
|
|
121
|
-
<name>${info.name}</name>
|
|
122
|
-
<value>${info.value}</value>
|
|
123
|
-
<type>${info.type}</type>
|
|
124
|
-
${
|
|
125
|
-
info.description
|
|
126
|
-
? `<description>${info.description}</description>`
|
|
127
|
-
: ''
|
|
128
|
-
}
|
|
129
|
-
</contact_info>`
|
|
130
|
-
)
|
|
131
|
-
.join('\n')
|
|
132
|
-
return `<contact_info_fields>\n${structuredContactInfo}</contact_info_fields>`
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private getMetadataInstructions(): string {
|
|
136
|
-
const metadata = `Current Date: ${new Date().toISOString()}`
|
|
137
|
-
return `<metadata>\n${metadata}\n</metadata>`
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private getCampaignInstructions(campaignsContext?: CampaignV2[]): string {
|
|
141
|
-
if (!campaignsContext || campaignsContext.length === 0) {
|
|
142
|
-
return ''
|
|
143
|
-
}
|
|
144
|
-
const campaignsWithContext = campaignsContext.filter(
|
|
145
|
-
campaign => campaign.agent_context
|
|
146
|
-
)
|
|
147
|
-
if (campaignsWithContext.length === 0) {
|
|
148
|
-
return ''
|
|
149
|
-
}
|
|
150
|
-
return campaignsWithContext
|
|
151
|
-
.map(
|
|
152
|
-
(campaign, index) =>
|
|
153
|
-
`<campaign_context_${index + 1}>\n${campaign.agent_context}\n</campaign_context_${index + 1}>`
|
|
154
|
-
)
|
|
155
|
-
.join('\n')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
private getOutputInstructions(): string {
|
|
159
|
-
const example = {
|
|
160
|
-
messages: [
|
|
161
|
-
{
|
|
162
|
-
type: 'text',
|
|
163
|
-
content: {
|
|
164
|
-
text: 'Hello, how can I help you today?',
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
],
|
|
168
|
-
}
|
|
169
|
-
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>`
|
|
170
|
-
return `<output>\n${output}\n</output>`
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private addHubtypeTools(
|
|
174
|
-
tools: Tool<TPlugins, TExtraData>[],
|
|
175
|
-
sourceIds: string[]
|
|
176
|
-
): Tool<TPlugins, TExtraData>[] {
|
|
177
|
-
const hubtypeTools: Tool[] = [...mandatoryTools]
|
|
178
|
-
if (sourceIds.length > 0) {
|
|
179
|
-
hubtypeTools.push(retrieveKnowledge)
|
|
180
|
-
}
|
|
181
|
-
return [...hubtypeTools, ...tools]
|
|
182
|
-
}
|
|
183
|
-
}
|
package/src/bot-config-tools.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { ToolConfigJSON } from '@botonic/core'
|
|
2
|
-
import { zodToJsonSchema } from 'zod-to-json-schema'
|
|
3
|
-
|
|
4
|
-
import type { CustomTool } from './types'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Maps custom AI agent tools to the format expected by bot config (e.g. for flow-builder).
|
|
8
|
-
* Converts each tool's Zod schema to JSON Schema so it can be serialized in the config.
|
|
9
|
-
*/
|
|
10
|
-
export function getToolsForBotConfig(
|
|
11
|
-
customTools: CustomTool[]
|
|
12
|
-
): ToolConfigJSON[] {
|
|
13
|
-
return customTools.map(tool => ({
|
|
14
|
-
name: tool.name,
|
|
15
|
-
description: tool.description,
|
|
16
|
-
// Cast to avoid TS "Type instantiation is excessively deep" with zodToJsonSchema + ZodSchema
|
|
17
|
-
schema: zodToJsonSchema(tool.schema as never, {
|
|
18
|
-
$refStrategy: 'none',
|
|
19
|
-
}) as ToolConfigJSON['schema'],
|
|
20
|
-
}))
|
|
21
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const HUBTYPE_API_URL =
|
|
2
|
-
process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
|
|
3
|
-
|
|
4
|
-
// OpenAI Provider Configuration
|
|
5
|
-
export const OPENAI_API_KEY = process.env.OPENAI_API_KEY // pragma: allowlist secret
|
|
6
|
-
export const OPENAI_MODEL = process.env.OPENAI_MODEL || 'gpt-4.1-mini'
|
|
7
|
-
export const OPENAI_PROVIDER: 'openai' | 'azure' =
|
|
8
|
-
(process.env.OPENAI_PROVIDER as 'openai' | 'azure') || 'azure'
|
|
9
|
-
|
|
10
|
-
// Azure OpenAI Configuration
|
|
11
|
-
export const AZURE_OPENAI_API_KEY = process.env.AZURE_OPENAI_API_KEY // pragma: allowlist secret
|
|
12
|
-
export const AZURE_OPENAI_API_BASE = process.env.AZURE_OPENAI_API_BASE
|
|
13
|
-
export const AZURE_OPENAI_API_VERSION =
|
|
14
|
-
process.env.AZURE_OPENAI_API_VERSION || '2025-01-01-preview'
|
|
15
|
-
|
|
16
|
-
export const isProd = process.env.NODE_ENV === 'production'
|
|
17
|
-
|
|
18
|
-
export const MAX_MEMORY_LENGTH = 25
|
|
19
|
-
|
|
20
|
-
export const DEFAULT_TIMEOUT_16_SECONDS = 16000
|
|
21
|
-
export const DEFAULT_MAX_RETRIES = 2
|
package/src/debug-logger.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import type { AiAgentArgs, ToolExecution } from '@botonic/core'
|
|
2
|
-
import type { ModelSettings } from '@openai/agents'
|
|
3
|
-
import { MAX_MEMORY_LENGTH, OPENAI_PROVIDER } from './constants'
|
|
4
|
-
import type { AgenticInputMessage, MemoryOptions, RunResult } from './types'
|
|
5
|
-
|
|
6
|
-
const PREFIX = '[BotonicPluginAiAgents]'
|
|
7
|
-
|
|
8
|
-
export interface DebugLoggerConfig {
|
|
9
|
-
messageHistoryApiVersion: string
|
|
10
|
-
maxRetries: number
|
|
11
|
-
timeout: number
|
|
12
|
-
customToolNames: string[]
|
|
13
|
-
memory: MemoryOptions
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ModelSettingsInfo {
|
|
17
|
-
provider: string
|
|
18
|
-
model: string | undefined
|
|
19
|
-
reasoning?: { effort: string }
|
|
20
|
-
text?: { verbosity: string }
|
|
21
|
-
toolChoice?: string
|
|
22
|
-
hasRetrieveKnowledge: boolean
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface DebugLogger {
|
|
26
|
-
logInitialConfig(config: DebugLoggerConfig): void
|
|
27
|
-
logAgentDebugInfo(
|
|
28
|
-
aiAgentArgs: AiAgentArgs,
|
|
29
|
-
toolNames: string[],
|
|
30
|
-
messages: AgenticInputMessage[]
|
|
31
|
-
): void
|
|
32
|
-
logModelSettings(settings: ModelSettingsInfo): void
|
|
33
|
-
logRunnerStart(model: string, modelSettings: ModelSettings): void
|
|
34
|
-
logRunResult(runResult: RunResult, startTime: number): void
|
|
35
|
-
logGuardrailTriggered(): void
|
|
36
|
-
logRunnerError(startTime: number, error: unknown): void
|
|
37
|
-
logToolExecution(toolExecution: ToolExecution): void
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
class EnabledDebugLogger implements DebugLogger {
|
|
41
|
-
logInitialConfig(config: DebugLoggerConfig): void {
|
|
42
|
-
console.log(`${PREFIX} === Plugin Initialization ===`)
|
|
43
|
-
console.log(`${PREFIX} Provider: ${OPENAI_PROVIDER}`)
|
|
44
|
-
console.log(
|
|
45
|
-
`${PREFIX} Message History API Version: ${config.messageHistoryApiVersion}`
|
|
46
|
-
)
|
|
47
|
-
console.log(`${PREFIX} Max Retries: ${config.maxRetries}`)
|
|
48
|
-
console.log(`${PREFIX} Timeout: ${config.timeout}ms`)
|
|
49
|
-
console.log(
|
|
50
|
-
`${PREFIX} Custom Tools Registered: ${config.customToolNames.join(', ') || 'none'}`
|
|
51
|
-
)
|
|
52
|
-
console.log(`${PREFIX} Memory Options:`, {
|
|
53
|
-
maxMessages: config.memory.maxMessages ?? MAX_MEMORY_LENGTH,
|
|
54
|
-
includeToolCalls: config.memory.includeToolCalls ?? true,
|
|
55
|
-
maxFullToolResults: config.memory.maxFullToolResults ?? 1,
|
|
56
|
-
debugMode: config.memory.debugMode ?? false,
|
|
57
|
-
})
|
|
58
|
-
console.log(`${PREFIX} === End Plugin Initialization ===`)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
logAgentDebugInfo(
|
|
62
|
-
aiAgentArgs: AiAgentArgs,
|
|
63
|
-
toolNames: string[],
|
|
64
|
-
messages: AgenticInputMessage[]
|
|
65
|
-
): void {
|
|
66
|
-
console.log(`${PREFIX} === AI Agent Debug Info ===`)
|
|
67
|
-
console.log(`${PREFIX} Agent Name: ${aiAgentArgs.name}`)
|
|
68
|
-
console.log(`${PREFIX} Active Tools: ${JSON.stringify(toolNames)}`)
|
|
69
|
-
console.log(
|
|
70
|
-
`${PREFIX} Source IDs: ${JSON.stringify(aiAgentArgs.sourceIds || [])}`
|
|
71
|
-
)
|
|
72
|
-
console.log(`${PREFIX} Message History Count: ${messages.length}`)
|
|
73
|
-
console.log(
|
|
74
|
-
`${PREFIX} Input Guardrail Rules: ${aiAgentArgs.inputGuardrailRules?.length || 0}`
|
|
75
|
-
)
|
|
76
|
-
console.log(`${PREFIX} Instructions:`)
|
|
77
|
-
console.log(aiAgentArgs.instructions)
|
|
78
|
-
console.log(`${PREFIX} === End Debug Info ===`)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
logModelSettings(settings: ModelSettingsInfo): void {
|
|
82
|
-
console.log(`${PREFIX} === Agent Model Settings ===`)
|
|
83
|
-
console.log(
|
|
84
|
-
`${PREFIX} Has Retrieve Knowledge Tool: ${settings.hasRetrieveKnowledge}`
|
|
85
|
-
)
|
|
86
|
-
if (settings.reasoning) {
|
|
87
|
-
console.log(`${PREFIX} Reasoning Effort: ${settings.reasoning.effort}`)
|
|
88
|
-
}
|
|
89
|
-
if (settings.text) {
|
|
90
|
-
console.log(`${PREFIX} Text Verbosity: ${settings.text.verbosity}`)
|
|
91
|
-
}
|
|
92
|
-
if (settings.toolChoice) {
|
|
93
|
-
console.log(`${PREFIX} Tool Choice: ${settings.toolChoice}`)
|
|
94
|
-
}
|
|
95
|
-
console.log(`${PREFIX} === End Model Settings ===`)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
logRunnerStart(model: string, modelSettings: ModelSettings): void {
|
|
99
|
-
console.log(`${PREFIX} === Runner Execution Start ===`)
|
|
100
|
-
console.log(`${PREFIX} Model: ${model}`)
|
|
101
|
-
console.log(`${PREFIX} Model Settings: ${JSON.stringify(modelSettings)}`)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
logRunResult(runResult: RunResult, startTime: number): void {
|
|
105
|
-
const elapsedMs = Date.now() - startTime
|
|
106
|
-
|
|
107
|
-
console.log(`${PREFIX} === Runner Execution Complete ===`)
|
|
108
|
-
console.log(`${PREFIX} Execution Time: ${elapsedMs}ms`)
|
|
109
|
-
console.log(`${PREFIX} Output Messages Count: ${runResult.messages.length}`)
|
|
110
|
-
console.log(`${PREFIX} Exit: ${runResult.exit}`)
|
|
111
|
-
console.log(`${PREFIX} Error: ${runResult.error}`)
|
|
112
|
-
console.log(
|
|
113
|
-
`${PREFIX} Tools Executed: ${JSON.stringify(runResult.toolsExecuted.map(t => t.toolName))}`
|
|
114
|
-
)
|
|
115
|
-
if (runResult.inputGuardrailsTriggered.length > 0) {
|
|
116
|
-
console.log(
|
|
117
|
-
`${PREFIX} Input Guardrails Triggered: ${runResult.inputGuardrailsTriggered.length}`
|
|
118
|
-
)
|
|
119
|
-
}
|
|
120
|
-
if (runResult.outputGuardrailsTriggered.length > 0) {
|
|
121
|
-
console.log(
|
|
122
|
-
`${PREFIX} Output Guardrails Triggered: ${runResult.outputGuardrailsTriggered.length}`
|
|
123
|
-
)
|
|
124
|
-
}
|
|
125
|
-
console.log(`${PREFIX} === End Runner Execution ===`)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
logGuardrailTriggered(): void {
|
|
129
|
-
console.log(`${PREFIX} Input guardrail triggered`)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
logRunnerError(startTime: number, error: unknown): void {
|
|
133
|
-
const elapsedMs = Date.now() - startTime
|
|
134
|
-
console.log(`${PREFIX} Runner execution failed after ${elapsedMs}ms`)
|
|
135
|
-
console.log(`${PREFIX} Error:`, error)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
logToolExecution(toolExecution: ToolExecution): void {
|
|
139
|
-
console.log(`${PREFIX} Tool Execution: ${toolExecution.toolName}`)
|
|
140
|
-
console.log(
|
|
141
|
-
`${PREFIX} Tool Arguments: ${JSON.stringify(toolExecution.toolArguments)}`
|
|
142
|
-
)
|
|
143
|
-
console.log(`${PREFIX} Tool Results: ${toolExecution.toolResults}`)
|
|
144
|
-
console.log(
|
|
145
|
-
`${PREFIX} Knowledgebase Sources IDs: ${JSON.stringify(toolExecution.knowledgebaseSourcesIds)}`
|
|
146
|
-
)
|
|
147
|
-
console.log(
|
|
148
|
-
`${PREFIX} Knowledgebase Chunks IDs: ${JSON.stringify(toolExecution.knowledgebaseChunksIds)}`
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
class DisabledDebugLogger implements DebugLogger {
|
|
154
|
-
logInitialConfig(): void {}
|
|
155
|
-
logAgentDebugInfo(): void {}
|
|
156
|
-
logModelSettings(): void {}
|
|
157
|
-
logRunnerStart(_model: string, _modelSettings: ModelSettings): void {}
|
|
158
|
-
logRunResult(): void {}
|
|
159
|
-
logGuardrailTriggered(): void {}
|
|
160
|
-
logRunnerError(): void {}
|
|
161
|
-
logToolExecution(): void {}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function createDebugLogger(enableDebug: boolean): DebugLogger {
|
|
165
|
-
return enableDebug ? new EnabledDebugLogger() : new DisabledDebugLogger()
|
|
166
|
-
}
|
package/src/guardrails/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './input'
|
package/src/guardrails/input.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Agent,
|
|
3
|
-
type InputGuardrail,
|
|
4
|
-
Runner,
|
|
5
|
-
type UserMessageItem,
|
|
6
|
-
} from '@openai/agents'
|
|
7
|
-
import { z } from 'zod'
|
|
8
|
-
import { AZURE_OPENAI_API_VERSION, isProd, OPENAI_PROVIDER } from '../constants'
|
|
9
|
-
import type { LLMConfig } from '../llm-config'
|
|
10
|
-
import { HubtypeApiClient } from '../services/hubtype-api-client'
|
|
11
|
-
import { TrackFeature, TrackProductName } from '../services/types'
|
|
12
|
-
import type { GuardrailRule, ResultRawResponse } from '../types'
|
|
13
|
-
|
|
14
|
-
export interface GuardrailTrackingContext {
|
|
15
|
-
botId: string
|
|
16
|
-
isTest: boolean
|
|
17
|
-
authToken: string
|
|
18
|
-
inferenceId: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function createInputGuardrail(
|
|
22
|
-
rules: GuardrailRule[],
|
|
23
|
-
llmConfig: LLMConfig,
|
|
24
|
-
trackingContext: GuardrailTrackingContext
|
|
25
|
-
): InputGuardrail {
|
|
26
|
-
const outputType = z.object(
|
|
27
|
-
Object.fromEntries(
|
|
28
|
-
rules.map(rule => [rule.name, z.boolean().describe(rule.description)])
|
|
29
|
-
)
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
const agent = new Agent({
|
|
33
|
-
name: 'InputGuardrail',
|
|
34
|
-
instructions:
|
|
35
|
-
'Check if the user triggers some of the following guardrails.',
|
|
36
|
-
outputType,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
name: 'InputGuardrail',
|
|
41
|
-
execute: async ({ input, context }) => {
|
|
42
|
-
const lastMessage = input[input.length - 1] as UserMessageItem
|
|
43
|
-
const modelProvider = llmConfig.modelProvider
|
|
44
|
-
const modelSettings = llmConfig.modelSettings
|
|
45
|
-
modelSettings.toolChoice = undefined
|
|
46
|
-
const runner = new Runner({
|
|
47
|
-
modelSettings,
|
|
48
|
-
modelProvider,
|
|
49
|
-
tracingDisabled: true,
|
|
50
|
-
})
|
|
51
|
-
const startTime = Date.now()
|
|
52
|
-
const result = await runner.run(agent, [lastMessage], { context })
|
|
53
|
-
const endTime = Date.now()
|
|
54
|
-
|
|
55
|
-
void sendGuardrailLlmRunTracking(
|
|
56
|
-
result,
|
|
57
|
-
trackingContext,
|
|
58
|
-
llmConfig,
|
|
59
|
-
startTime,
|
|
60
|
-
endTime
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
const finalOutput = result.finalOutput as Record<string, boolean>
|
|
64
|
-
if (finalOutput === undefined) {
|
|
65
|
-
throw new Error('Guardrail agent failed to produce output')
|
|
66
|
-
}
|
|
67
|
-
const triggered = Object.values(finalOutput).some(value => value === true)
|
|
68
|
-
const triggeredGuardrails = Object.keys(finalOutput).filter(
|
|
69
|
-
key => finalOutput[key] === true
|
|
70
|
-
)
|
|
71
|
-
return {
|
|
72
|
-
outputInfo: triggeredGuardrails,
|
|
73
|
-
tripwireTriggered: triggered,
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async function sendGuardrailLlmRunTracking(
|
|
80
|
-
result: {
|
|
81
|
-
rawResponses?: ResultRawResponse[]
|
|
82
|
-
},
|
|
83
|
-
trackingContext: GuardrailTrackingContext,
|
|
84
|
-
llmConfig: LLMConfig,
|
|
85
|
-
startTime: number,
|
|
86
|
-
endTime: number
|
|
87
|
-
): Promise<void> {
|
|
88
|
-
if (!isProd) {
|
|
89
|
-
return
|
|
90
|
-
}
|
|
91
|
-
const rawResponses = result.rawResponses ?? []
|
|
92
|
-
if (rawResponses.length === 0) {
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
const totalDuration = endTime - startTime
|
|
96
|
-
const durationPerCall = Math.round(totalDuration / rawResponses.length)
|
|
97
|
-
const temperature =
|
|
98
|
-
(llmConfig.modelSettings.temperature as number | undefined) ?? 0
|
|
99
|
-
const apiVersion = OPENAI_PROVIDER === 'azure' ? AZURE_OPENAI_API_VERSION : ''
|
|
100
|
-
|
|
101
|
-
const llmRuns = rawResponses.map(response => ({
|
|
102
|
-
inference_id: trackingContext.inferenceId,
|
|
103
|
-
is_test: trackingContext.isTest,
|
|
104
|
-
product_name: TrackProductName.AI_AGENT,
|
|
105
|
-
deployment_name: llmConfig.modelName,
|
|
106
|
-
model_name:
|
|
107
|
-
(response.providerData?.['model'] as string | undefined) ??
|
|
108
|
-
llmConfig.modelName,
|
|
109
|
-
feature: TrackFeature.AI_AGENT_GUARDRAIL,
|
|
110
|
-
api_version: apiVersion,
|
|
111
|
-
num_prompt_tokens: response.usage.inputTokens,
|
|
112
|
-
num_completion_tokens: response.usage.outputTokens,
|
|
113
|
-
duration_in_milliseconds: durationPerCall,
|
|
114
|
-
temperature,
|
|
115
|
-
error: null,
|
|
116
|
-
}))
|
|
117
|
-
|
|
118
|
-
const client = new HubtypeApiClient(trackingContext.authToken)
|
|
119
|
-
await client.trackLlmRuns(trackingContext.botId, {
|
|
120
|
-
llm_runs: llmRuns,
|
|
121
|
-
})
|
|
122
|
-
}
|