@botonic/plugin-ai-agents 0.46.0 → 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.
Files changed (181) hide show
  1. package/CHANGELOG.md +209 -0
  2. package/package.json +23 -41
  3. package/src/index.d.ts +2 -0
  4. package/src/index.js +3 -0
  5. package/src/index.js.map +1 -0
  6. package/{lib/cjs → src/lib}/agent-builder.d.ts +5 -10
  7. package/{lib/cjs → src/lib}/agent-builder.js +31 -25
  8. package/src/lib/agent-builder.js.map +1 -0
  9. package/{lib/cjs → src/lib}/constants.d.ts +1 -3
  10. package/src/lib/constants.js +13 -0
  11. package/src/lib/constants.js.map +1 -0
  12. package/{lib/esm → src/lib}/debug-logger.d.ts +3 -5
  13. package/{lib/esm → src/lib}/debug-logger.js +44 -29
  14. package/src/lib/debug-logger.js.map +1 -0
  15. package/src/lib/guardrails/index.js +2 -0
  16. package/src/lib/guardrails/index.js.map +1 -0
  17. package/src/lib/guardrails/input.d.ts +3 -0
  18. package/src/lib/guardrails/input.js +29 -0
  19. package/src/lib/guardrails/input.js.map +1 -0
  20. package/{lib/cjs/services → src/lib}/hubtype-api-client.d.ts +13 -5
  21. package/src/lib/hubtype-api-client.js +206 -0
  22. package/src/lib/hubtype-api-client.js.map +1 -0
  23. package/{lib/cjs → src/lib}/index.d.ts +2 -7
  24. package/src/lib/index.js +111 -0
  25. package/src/lib/index.js.map +1 -0
  26. package/src/lib/openai.d.ts +1 -0
  27. package/src/lib/openai.js +21 -0
  28. package/src/lib/openai.js.map +1 -0
  29. package/src/lib/runner.d.ts +12 -0
  30. package/src/lib/runner.js +96 -0
  31. package/src/lib/runner.js.map +1 -0
  32. package/{lib/esm → src/lib}/structured-output/carousel.d.ts +1 -1
  33. package/src/lib/structured-output/carousel.js +19 -0
  34. package/src/lib/structured-output/carousel.js.map +1 -0
  35. package/{lib/cjs → src/lib}/structured-output/exit.d.ts +1 -1
  36. package/src/lib/structured-output/exit.js +7 -0
  37. package/src/lib/structured-output/exit.js.map +1 -0
  38. package/{lib/cjs → src/lib}/structured-output/index.d.ts +10 -73
  39. package/src/lib/structured-output/index.js +11 -0
  40. package/src/lib/structured-output/index.js.map +1 -0
  41. package/{lib/esm → src/lib}/structured-output/text-with-buttons.d.ts +8 -8
  42. package/src/lib/structured-output/text-with-buttons.js +14 -0
  43. package/src/lib/structured-output/text-with-buttons.js.map +1 -0
  44. package/{lib/esm → src/lib}/structured-output/text.d.ts +1 -1
  45. package/src/lib/structured-output/text.js +10 -0
  46. package/src/lib/structured-output/text.js.map +1 -0
  47. package/{lib/esm → src/lib}/tools/index.d.ts +1 -1
  48. package/src/lib/tools/index.js +3 -0
  49. package/src/lib/tools/index.js.map +1 -0
  50. package/{lib/cjs → src/lib}/tools/retrieve-knowledge.d.ts +2 -2
  51. package/src/lib/tools/retrieve-knowledge.js +30 -0
  52. package/src/lib/tools/retrieve-knowledge.js.map +1 -0
  53. package/{lib/esm → src/lib}/types.d.ts +11 -21
  54. package/src/lib/types.js +2 -0
  55. package/src/lib/types.js.map +1 -0
  56. package/README.md +0 -156
  57. package/lib/cjs/agent-builder.js.map +0 -1
  58. package/lib/cjs/bot-config-tools.d.ts +0 -7
  59. package/lib/cjs/bot-config-tools.js +0 -19
  60. package/lib/cjs/bot-config-tools.js.map +0 -1
  61. package/lib/cjs/constants.js +0 -17
  62. package/lib/cjs/constants.js.map +0 -1
  63. package/lib/cjs/debug-logger.d.ts +0 -33
  64. package/lib/cjs/debug-logger.js +0 -97
  65. package/lib/cjs/debug-logger.js.map +0 -1
  66. package/lib/cjs/guardrails/index.js +0 -5
  67. package/lib/cjs/guardrails/index.js.map +0 -1
  68. package/lib/cjs/guardrails/input.d.ts +0 -10
  69. package/lib/cjs/guardrails/input.js +0 -77
  70. package/lib/cjs/guardrails/input.js.map +0 -1
  71. package/lib/cjs/index.js +0 -133
  72. package/lib/cjs/index.js.map +0 -1
  73. package/lib/cjs/llm-config.d.ts +0 -15
  74. package/lib/cjs/llm-config.js +0 -66
  75. package/lib/cjs/llm-config.js.map +0 -1
  76. package/lib/cjs/runner.d.ts +0 -18
  77. package/lib/cjs/runner.js +0 -181
  78. package/lib/cjs/runner.js.map +0 -1
  79. package/lib/cjs/services/hubtype-api-client.js +0 -206
  80. package/lib/cjs/services/hubtype-api-client.js.map +0 -1
  81. package/lib/cjs/services/types.d.ts +0 -85
  82. package/lib/cjs/services/types.js +0 -13
  83. package/lib/cjs/services/types.js.map +0 -1
  84. package/lib/cjs/structured-output/bot-executor.d.ts +0 -49
  85. package/lib/cjs/structured-output/bot-executor.js +0 -18
  86. package/lib/cjs/structured-output/bot-executor.js.map +0 -1
  87. package/lib/cjs/structured-output/carousel.d.ts +0 -90
  88. package/lib/cjs/structured-output/carousel.js +0 -22
  89. package/lib/cjs/structured-output/carousel.js.map +0 -1
  90. package/lib/cjs/structured-output/exit.js +0 -11
  91. package/lib/cjs/structured-output/exit.js.map +0 -1
  92. package/lib/cjs/structured-output/index.js +0 -21
  93. package/lib/cjs/structured-output/index.js.map +0 -1
  94. package/lib/cjs/structured-output/text-with-buttons.d.ts +0 -49
  95. package/lib/cjs/structured-output/text-with-buttons.js +0 -18
  96. package/lib/cjs/structured-output/text-with-buttons.js.map +0 -1
  97. package/lib/cjs/structured-output/text.d.ts +0 -23
  98. package/lib/cjs/structured-output/text.js +0 -13
  99. package/lib/cjs/structured-output/text.js.map +0 -1
  100. package/lib/cjs/tools/index.d.ts +0 -3
  101. package/lib/cjs/tools/index.js +0 -7
  102. package/lib/cjs/tools/index.js.map +0 -1
  103. package/lib/cjs/tools/retrieve-knowledge.js +0 -32
  104. package/lib/cjs/tools/retrieve-knowledge.js.map +0 -1
  105. package/lib/cjs/types.d.ts +0 -56
  106. package/lib/cjs/types.js +0 -3
  107. package/lib/cjs/types.js.map +0 -1
  108. package/lib/esm/agent-builder.d.ts +0 -34
  109. package/lib/esm/agent-builder.js +0 -105
  110. package/lib/esm/agent-builder.js.map +0 -1
  111. package/lib/esm/bot-config-tools.d.ts +0 -7
  112. package/lib/esm/bot-config-tools.js +0 -19
  113. package/lib/esm/bot-config-tools.js.map +0 -1
  114. package/lib/esm/constants.d.ts +0 -11
  115. package/lib/esm/constants.js +0 -17
  116. package/lib/esm/constants.js.map +0 -1
  117. package/lib/esm/debug-logger.js.map +0 -1
  118. package/lib/esm/guardrails/index.d.ts +0 -1
  119. package/lib/esm/guardrails/index.js +0 -5
  120. package/lib/esm/guardrails/index.js.map +0 -1
  121. package/lib/esm/guardrails/input.d.ts +0 -10
  122. package/lib/esm/guardrails/input.js +0 -77
  123. package/lib/esm/guardrails/input.js.map +0 -1
  124. package/lib/esm/index.d.ts +0 -18
  125. package/lib/esm/index.js +0 -133
  126. package/lib/esm/index.js.map +0 -1
  127. package/lib/esm/llm-config.d.ts +0 -15
  128. package/lib/esm/llm-config.js +0 -66
  129. package/lib/esm/llm-config.js.map +0 -1
  130. package/lib/esm/runner.d.ts +0 -18
  131. package/lib/esm/runner.js +0 -181
  132. package/lib/esm/runner.js.map +0 -1
  133. package/lib/esm/services/hubtype-api-client.d.ts +0 -14
  134. package/lib/esm/services/hubtype-api-client.js +0 -206
  135. package/lib/esm/services/hubtype-api-client.js.map +0 -1
  136. package/lib/esm/services/types.d.ts +0 -85
  137. package/lib/esm/services/types.js +0 -13
  138. package/lib/esm/services/types.js.map +0 -1
  139. package/lib/esm/structured-output/bot-executor.d.ts +0 -49
  140. package/lib/esm/structured-output/bot-executor.js +0 -18
  141. package/lib/esm/structured-output/bot-executor.js.map +0 -1
  142. package/lib/esm/structured-output/carousel.js +0 -22
  143. package/lib/esm/structured-output/carousel.js.map +0 -1
  144. package/lib/esm/structured-output/exit.d.ts +0 -10
  145. package/lib/esm/structured-output/exit.js +0 -11
  146. package/lib/esm/structured-output/exit.js.map +0 -1
  147. package/lib/esm/structured-output/index.d.ts +0 -291
  148. package/lib/esm/structured-output/index.js +0 -21
  149. package/lib/esm/structured-output/index.js.map +0 -1
  150. package/lib/esm/structured-output/text-with-buttons.js +0 -18
  151. package/lib/esm/structured-output/text-with-buttons.js.map +0 -1
  152. package/lib/esm/structured-output/text.js +0 -13
  153. package/lib/esm/structured-output/text.js.map +0 -1
  154. package/lib/esm/tools/index.js +0 -7
  155. package/lib/esm/tools/index.js.map +0 -1
  156. package/lib/esm/tools/retrieve-knowledge.d.ts +0 -9
  157. package/lib/esm/tools/retrieve-knowledge.js +0 -32
  158. package/lib/esm/tools/retrieve-knowledge.js.map +0 -1
  159. package/lib/esm/types.js +0 -3
  160. package/lib/esm/types.js.map +0 -1
  161. package/src/agent-builder.ts +0 -179
  162. package/src/bot-config-tools.ts +0 -21
  163. package/src/constants.ts +0 -21
  164. package/src/debug-logger.ts +0 -166
  165. package/src/guardrails/index.ts +0 -1
  166. package/src/guardrails/input.ts +0 -122
  167. package/src/index.ts +0 -209
  168. package/src/llm-config.ts +0 -96
  169. package/src/runner.ts +0 -287
  170. package/src/services/hubtype-api-client.ts +0 -240
  171. package/src/services/types.ts +0 -106
  172. package/src/structured-output/bot-executor.ts +0 -21
  173. package/src/structured-output/carousel.ts +0 -24
  174. package/src/structured-output/exit.ts +0 -12
  175. package/src/structured-output/index.ts +0 -25
  176. package/src/structured-output/text-with-buttons.ts +0 -21
  177. package/src/structured-output/text.ts +0 -13
  178. package/src/tools/index.ts +0 -5
  179. package/src/tools/retrieve-knowledge.ts +0 -37
  180. package/src/types.ts +0 -110
  181. /package/{lib/cjs → src/lib}/guardrails/index.d.ts +0 -0
@@ -1,179 +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
-
8
- import { OPENAI_PROVIDER } from './constants'
9
- import type { DebugLogger } from './debug-logger'
10
- import { createInputGuardrail } from './guardrails'
11
- import type { GuardrailTrackingContext } from './guardrails/input'
12
- import type { LLMConfig } from './llm-config'
13
- import { OutputSchema } from './structured-output'
14
- import { mandatoryTools, retrieveKnowledge } from './tools'
15
- import type { AIAgent, Context, GuardrailRule, Tool } from './types'
16
-
17
- interface AIAgentBuilderOptions<
18
- TPlugins extends ResolvedPlugins = ResolvedPlugins,
19
- TExtraData = any,
20
- > {
21
- name: string
22
- instructions: string
23
- tools: Tool<TPlugins, TExtraData>[]
24
- campaignsContext?: CampaignV2[]
25
- contactInfo: ContactInfo[]
26
- inputGuardrailRules: GuardrailRule[]
27
- sourceIds: string[]
28
- llmConfig: LLMConfig
29
- logger: DebugLogger
30
- guardrailTrackingContext: GuardrailTrackingContext
31
- }
32
-
33
- export class AIAgentBuilder<
34
- TPlugins extends ResolvedPlugins = ResolvedPlugins,
35
- TExtraData = any,
36
- > {
37
- private name: string
38
- private instructions: string
39
- private tools: Tool<TPlugins, TExtraData>[]
40
- private inputGuardrails: InputGuardrail[]
41
- public llmConfig: LLMConfig
42
- private logger: DebugLogger
43
-
44
- constructor(options: AIAgentBuilderOptions<TPlugins, TExtraData>) {
45
- this.name = options.name
46
- this.instructions = this.addExtraInstructions(
47
- options.instructions,
48
- options.contactInfo,
49
- options.campaignsContext
50
- )
51
- this.tools = this.addHubtypeTools(options.tools, options.sourceIds)
52
- this.inputGuardrails = []
53
- this.llmConfig = options.llmConfig
54
- this.logger = options.logger
55
- if (options.inputGuardrailRules.length > 0) {
56
- const inputGuardrail = createInputGuardrail(
57
- options.inputGuardrailRules,
58
- options.llmConfig,
59
- options.guardrailTrackingContext
60
- )
61
- this.inputGuardrails.push(inputGuardrail)
62
- }
63
- }
64
-
65
- build(): AIAgent<TPlugins, TExtraData> {
66
- // When using standard OpenAI API, we need to specify the model
67
- // Azure OpenAI uses deployment name instead
68
-
69
- const model = this.llmConfig.modelName
70
- const hasRetrieveKnowledge = this.tools.includes(retrieveKnowledge)
71
-
72
- this.logger.logModelSettings({
73
- provider: OPENAI_PROVIDER,
74
- model,
75
- reasoning: this.llmConfig.modelSettings.reasoning as
76
- | { effort: string }
77
- | undefined,
78
- text: this.llmConfig.modelSettings.text as
79
- | { verbosity: string }
80
- | undefined,
81
- toolChoice: this.llmConfig.modelSettings.toolChoice as string | undefined,
82
- hasRetrieveKnowledge,
83
- })
84
-
85
- return new Agent<
86
- Context<TPlugins, TExtraData>,
87
- AgentOutputType<typeof OutputSchema>
88
- >({
89
- name: this.name,
90
- model,
91
- instructions: this.instructions,
92
- tools: this.tools,
93
- outputType: OutputSchema,
94
- inputGuardrails: this.inputGuardrails,
95
- outputGuardrails: [],
96
- })
97
- }
98
-
99
- private addExtraInstructions(
100
- initialInstructions: string,
101
- contactInfo: ContactInfo[],
102
- campaignsContext?: CampaignV2[]
103
- ): string {
104
- const instructions = `<instructions>\n${initialInstructions.trim()}\n</instructions>`
105
- const metadataInstructions = this.getMetadataInstructions()
106
- const contactInfoInstructions = this.getContactInfoInstructions(contactInfo)
107
- const campaignInstructions = this.getCampaignInstructions(campaignsContext)
108
- const outputInstructions = this.getOutputInstructions()
109
- return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${campaignInstructions}\n\n${outputInstructions}`
110
- }
111
-
112
- private getContactInfoInstructions(contactInfo: ContactInfo[]): string {
113
- const structuredContactInfo = contactInfo
114
- .map(
115
- info =>
116
- ` <contact_info>
117
- <name>${info.name}</name>
118
- <value>${info.value}</value>
119
- <type>${info.type}</type>
120
- ${
121
- info.description
122
- ? `<description>${info.description}</description>`
123
- : ''
124
- }
125
- </contact_info>`
126
- )
127
- .join('\n')
128
- return `<contact_info_fields>\n${structuredContactInfo}</contact_info_fields>`
129
- }
130
-
131
- private getMetadataInstructions(): string {
132
- const metadata = `Current Date: ${new Date().toISOString()}`
133
- return `<metadata>\n${metadata}\n</metadata>`
134
- }
135
-
136
- private getCampaignInstructions(campaignsContext?: CampaignV2[]): string {
137
- if (!campaignsContext || campaignsContext.length === 0) {
138
- return ''
139
- }
140
- const campaignsWithContext = campaignsContext.filter(
141
- campaign => campaign.agent_context
142
- )
143
- if (campaignsWithContext.length === 0) {
144
- return ''
145
- }
146
- return campaignsWithContext
147
- .map(
148
- (campaign, index) =>
149
- `<campaign_context_${index + 1}>\n${campaign.agent_context}\n</campaign_context_${index + 1}>`
150
- )
151
- .join('\n')
152
- }
153
-
154
- private getOutputInstructions(): string {
155
- const example = {
156
- messages: [
157
- {
158
- type: 'text',
159
- content: {
160
- text: 'Hello, how can I help you today?',
161
- },
162
- },
163
- ],
164
- }
165
- 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>`
166
- return `<output>\n${output}\n</output>`
167
- }
168
-
169
- private addHubtypeTools(
170
- tools: Tool<TPlugins, TExtraData>[],
171
- sourceIds: string[]
172
- ): Tool<TPlugins, TExtraData>[] {
173
- const hubtypeTools: Tool[] = [...mandatoryTools]
174
- if (sourceIds.length > 0) {
175
- hubtypeTools.push(retrieveKnowledge)
176
- }
177
- return [...hubtypeTools, ...tools]
178
- }
179
- }
@@ -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
@@ -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
- }
@@ -1 +0,0 @@
1
- export * from './input'
@@ -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
- }