Skip to content

Commit c9978c5

Browse files
authored
Formatting update / PR 10
2 parents bcdf2e7 + 48d9656 commit c9978c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+7152
-7056
lines changed

.eslintrc.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
module.exports = {
2-
parser: '@typescript-eslint/parser',
3-
extends: [
4-
'eslint:recommended',
5-
],
6-
plugins: ['@typescript-eslint'],
7-
env: {
8-
node: true,
9-
es2020: true,
10-
},
11-
parserOptions: {
12-
ecmaVersion: 2020,
13-
sourceType: 'module',
14-
},
15-
rules: {
16-
'@typescript-eslint/no-unused-vars': 'warn',
17-
'@typescript-eslint/explicit-function-return-type': 'off',
18-
'@typescript-eslint/explicit-module-boundary-types': 'off',
19-
'@typescript-eslint/no-explicit-any': 'warn',
20-
},
21-
};
2+
parser: '@typescript-eslint/parser',
3+
extends: ['eslint:recommended'],
4+
plugins: ['@typescript-eslint'],
5+
env: {
6+
node: true,
7+
es2020: true,
8+
},
9+
parserOptions: {
10+
ecmaVersion: 2020,
11+
sourceType: 'module',
12+
},
13+
rules: {
14+
'@typescript-eslint/no-unused-vars': 'warn',
15+
'@typescript-eslint/explicit-function-return-type': 'off',
16+
'@typescript-eslint/explicit-module-boundary-types': 'off',
17+
'@typescript-eslint/no-explicit-any': 'warn',
18+
},
19+
};

examples/basic/agents_sdk.ts

Lines changed: 109 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,146 @@
11
#!/usr/bin/env node
22
/**
33
* Example: Basic async guardrail bundle using Agents SDK with GuardrailAgent.
4-
*
4+
*
55
* Run with: npx tsx agents_sdk.ts
6-
*
6+
*
77
* Prerequisites:
88
* - Install @openai/agents: npm install @openai/agents
99
* - Set OPENAI_API_KEY environment variable
1010
*/
1111

1212
import * as readline from 'readline';
1313
import { GuardrailAgent } from '../../dist/index.js';
14-
import {
15-
InputGuardrailTripwireTriggered,
16-
OutputGuardrailTripwireTriggered
17-
} from '@openai/agents';
14+
import { InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered } from '@openai/agents';
1815

1916
// Define your pipeline configuration
2017
const PIPELINE_CONFIG = {
18+
version: 1,
19+
pre_flight: {
2120
version: 1,
22-
pre_flight: {
23-
version: 1,
24-
guardrails: [
25-
{
26-
name: "Moderation",
27-
config: {
28-
categories: ["hate", "violence", "self-harm"],
29-
},
30-
},
31-
],
32-
},
33-
input: {
34-
version: 1,
35-
guardrails: [
36-
{
37-
name: "Custom Prompt Check",
38-
config: {
39-
model: "gpt-4.1-nano-2025-04-14",
40-
confidence_threshold: 0.7,
41-
system_prompt_details: "Check if the text contains any math problems.",
42-
},
43-
},
44-
],
45-
},
46-
output: {
47-
version: 1,
48-
guardrails: [
49-
{ name: "URL Filter", config: { url_allow_list: ["example.com"] } },
50-
],
51-
},
21+
guardrails: [
22+
{
23+
name: 'Moderation',
24+
config: {
25+
categories: ['hate', 'violence', 'self-harm'],
26+
},
27+
},
28+
],
29+
},
30+
input: {
31+
version: 1,
32+
guardrails: [
33+
{
34+
name: 'Custom Prompt Check',
35+
config: {
36+
model: 'gpt-4.1-nano-2025-04-14',
37+
confidence_threshold: 0.7,
38+
system_prompt_details: 'Check if the text contains any math problems.',
39+
},
40+
},
41+
],
42+
},
43+
output: {
44+
version: 1,
45+
guardrails: [{ name: 'URL Filter', config: { url_allow_list: ['example.com'] } }],
46+
},
5247
};
5348

5449
/**
5550
* Create a readline interface for user input.
5651
*/
5752
function createReadlineInterface(): readline.Interface {
58-
return readline.createInterface({
59-
input: process.stdin,
60-
output: process.stdout,
61-
});
53+
return readline.createInterface({
54+
input: process.stdin,
55+
output: process.stdout,
56+
});
6257
}
6358

6459
/**
6560
* Main input loop for the customer support agent with input/output guardrails.
6661
*/
6762
async function main(): Promise<void> {
68-
console.log('🤖 Customer Support Agent with Guardrails');
69-
console.log('==========================================');
70-
console.log('This agent has the following guardrails configured:');
71-
console.log('• Pre-flight: Moderation (hate, violence, self-harm)');
72-
console.log('• Input: Custom Prompt Check (math problems)');
73-
console.log('• Output: URL Filter (only example.com allowed)');
74-
console.log('==========================================\n');
75-
76-
try {
77-
// Create agent with guardrails automatically configured from pipeline configuration
78-
// Set raiseGuardrailErrors to true for strict error handling
79-
const agent = await GuardrailAgent.create(
80-
PIPELINE_CONFIG,
81-
"Customer support agent",
82-
"You are a customer support agent. You help customers with their questions.",
83-
{},
84-
true // raiseGuardrailErrors = true
85-
);
86-
87-
// Dynamic import to avoid bundling issues
88-
const { run } = await import('@openai/agents');
89-
90-
const rl = createReadlineInterface();
91-
92-
// Handle graceful shutdown
93-
const shutdown = () => {
94-
console.log('\n👋 Exiting the program.');
95-
rl.close();
96-
process.exit(0);
97-
};
98-
99-
process.on('SIGINT', shutdown);
100-
process.on('SIGTERM', shutdown);
101-
102-
while (true) {
103-
try {
104-
const userInput = await new Promise<string>((resolve) => {
105-
rl.question('Enter a message: ', resolve);
106-
});
107-
108-
if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') {
109-
shutdown();
110-
break;
111-
}
112-
113-
console.log('🤔 Processing...\n');
114-
115-
const result = await run(agent, userInput);
116-
console.log(`Assistant: ${result.finalOutput}\n`);
117-
118-
} catch (error: any) {
119-
// Handle guardrail tripwire exceptions
120-
if (error instanceof InputGuardrailTripwireTriggered) {
121-
console.log('🛑 Input guardrail triggered! Please try a different message.\n');
122-
continue;
123-
} else if (error instanceof OutputGuardrailTripwireTriggered) {
124-
console.log('🛑 Output guardrail triggered! The response was blocked.\n');
125-
continue;
126-
} else {
127-
console.error('❌ An error occurred:', error.message);
128-
console.log('Please try again.\n');
129-
}
130-
}
63+
console.log('🤖 Customer Support Agent with Guardrails');
64+
console.log('==========================================');
65+
console.log('This agent has the following guardrails configured:');
66+
console.log('• Pre-flight: Moderation (hate, violence, self-harm)');
67+
console.log('• Input: Custom Prompt Check (math problems)');
68+
console.log('• Output: URL Filter (only example.com allowed)');
69+
console.log('==========================================\n');
70+
71+
try {
72+
// Create agent with guardrails automatically configured from pipeline configuration
73+
// Set raiseGuardrailErrors to true for strict error handling
74+
const agent = await GuardrailAgent.create(
75+
PIPELINE_CONFIG,
76+
'Customer support agent',
77+
'You are a customer support agent. You help customers with their questions.',
78+
{},
79+
true // raiseGuardrailErrors = true
80+
);
81+
82+
// Dynamic import to avoid bundling issues
83+
const { run } = await import('@openai/agents');
84+
85+
const rl = createReadlineInterface();
86+
87+
// Handle graceful shutdown
88+
const shutdown = () => {
89+
console.log('\n👋 Exiting the program.');
90+
rl.close();
91+
process.exit(0);
92+
};
93+
94+
process.on('SIGINT', shutdown);
95+
process.on('SIGTERM', shutdown);
96+
97+
while (true) {
98+
try {
99+
const userInput = await new Promise<string>((resolve) => {
100+
rl.question('Enter a message: ', resolve);
101+
});
102+
103+
if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') {
104+
shutdown();
105+
break;
131106
}
132107

133-
} catch (error: any) {
134-
if (error.message.includes('@openai/agents')) {
135-
console.error('❌ Error: The @openai/agents package is required.');
136-
console.error('Please install it with: npm install @openai/agents');
137-
} else if (error.message.includes('OPENAI_API_KEY')) {
138-
console.error('❌ Error: OPENAI_API_KEY environment variable is required.');
139-
console.error('Please set it with: export OPENAI_API_KEY=sk-...');
108+
console.log('🤔 Processing...\n');
109+
110+
const result = await run(agent, userInput);
111+
console.log(`Assistant: ${result.finalOutput}\n`);
112+
} catch (error: any) {
113+
// Handle guardrail tripwire exceptions
114+
if (error instanceof InputGuardrailTripwireTriggered) {
115+
console.log('🛑 Input guardrail triggered! Please try a different message.\n');
116+
continue;
117+
} else if (error instanceof OutputGuardrailTripwireTriggered) {
118+
console.log('🛑 Output guardrail triggered! The response was blocked.\n');
119+
continue;
140120
} else {
141-
console.error('❌ Unexpected error:', error.message);
121+
console.error('❌ An error occurred:', error.message);
122+
console.log('Please try again.\n');
142123
}
143-
process.exit(1);
124+
}
125+
}
126+
} catch (error: any) {
127+
if (error.message.includes('@openai/agents')) {
128+
console.error('❌ Error: The @openai/agents package is required.');
129+
console.error('Please install it with: npm install @openai/agents');
130+
} else if (error.message.includes('OPENAI_API_KEY')) {
131+
console.error('❌ Error: OPENAI_API_KEY environment variable is required.');
132+
console.error('Please set it with: export OPENAI_API_KEY=sk-...');
133+
} else {
134+
console.error('❌ Unexpected error:', error.message);
144135
}
136+
process.exit(1);
137+
}
145138
}
146139

147140
// Run the main function
148141
if (import.meta.url === `file://${process.argv[1]}`) {
149-
main().catch((error) => {
150-
console.error('❌ Fatal error:', error);
151-
process.exit(1);
152-
});
142+
main().catch((error) => {
143+
console.error('❌ Fatal error:', error);
144+
process.exit(1);
145+
});
153146
}

0 commit comments

Comments
 (0)