Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions examples/agent-patterns/agents-as-tools-conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ async function main() {
}
}

if (require.main === module) {
main().catch((error) => {
console.error('Error:', error);
process.exitCode = 1;
});
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
3 changes: 2 additions & 1 deletion examples/agent-patterns/agents-as-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ async function main() {
}

main().catch((error) => {
console.error('Error:', error);
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion examples/agent-patterns/deterministic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ async function main() {
}

main().catch((error) => {
console.error('Error:', error);
console.error(error);
process.exit(1);
});
37 changes: 21 additions & 16 deletions examples/agent-patterns/forcing-tool-use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,28 @@ async function main(
}

// CLI argument parsing
if (require.main === module) {
const args = process.argv.slice(2);
let toolUseBehavior: 'default' | 'first_tool' | 'custom' = 'default';
const idx = args.findIndex((a) => a === '-t' || a === '--tool-use-behavior');
if (idx !== -1 && args[idx + 1]) {
const val = args[idx + 1];
if (val === 'default' || val === 'first_tool' || val === 'custom') {
toolUseBehavior = val;
} else {
console.error('Invalid tool use behavior:', val);
process.exit(1);
}
const args = process.argv.slice(2);
let toolUseBehavior: 'default' | 'first_tool' | 'custom' = 'default';
const flagIndex = args.findIndex(
(arg) => arg === '-t' || arg === '--tool-use-behavior',
);

if (flagIndex !== -1 && args[flagIndex + 1]) {
const value = args[flagIndex + 1];
if (value === 'default' || value === 'first_tool' || value === 'custom') {
toolUseBehavior = value;
} else {
console.log(
'Usage: pnpm run start:forcing-tool-use -t <default|first_tool|custom>',
);
console.error('Invalid tool use behavior:', value);
process.exit(1);
}
main(toolUseBehavior).catch(console.error);
} else {
console.log(
'Usage: pnpm run start:forcing-tool-use -t <default|first_tool|custom>',
);
process.exit(1);
}

main(toolUseBehavior).catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/agent-patterns/human-in-the-loop-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ async function main() {
console.log('\n\nDone');
}

main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(1);
});
3 changes: 2 additions & 1 deletion examples/agent-patterns/human-in-the-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ async function main() {
}

main().catch((error) => {
console.dir(error, { depth: null });
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/agent-patterns/input-guardrails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ async function main() {
});
}

main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion examples/agent-patterns/llm-as-a-judge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ async function main() {
}

main().catch((error) => {
console.error('Error:', error);
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/agent-patterns/output-guardrails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ async function main() {
});
}

main();
main().catch((error) => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion examples/agent-patterns/parallelization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ async function main() {
}

main().catch((error) => {
console.error('Error:', error);
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion examples/agent-patterns/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ async function main() {
}

main().catch((error) => {
console.error('Error:', error);
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/agent-patterns/streamed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ async function main() {
}
}

main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/agent-patterns/streaming-guardrails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ async function main() {
console.log(`\n\n${result.finalOutput}`);
}

main();
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/agent-lifecycle-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async function main() {
console.log(result.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/basic/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ async function main() {
});
}

main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(1);
});
8 changes: 5 additions & 3 deletions examples/basic/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ async function main() {
}
}
}
if (require.main === module) {
main().catch(console.error);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/dynamic-system-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function main() {
console.log(`Assistant: ${result.finalOutput}`);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/hello-world-gpt-5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function main() {
// console.log(completionsResult2.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/hello-world-gpt-oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function main() {
// console.log(result.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function main() {
// Endless by design.
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ async function main() {
console.log(result.finalOutput);
}

main();
main().catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/basic/json-schema-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ async function main() {
// { city: 'London', forecast: '...'}
}

main().catch(console.error);
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/lifecycle-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async function main() {
console.log(result.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/local-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async function main() {
// OpenAI o3 and OpenAI o4-mini combine state-of-the-art reasoning with full tool capabilities — web browsing, Python, image and file analysis, image generation, canvas, automations, file search, and memory.
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/local-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function main() {
// This image shows a large American bison standing on a grassy hill. The bison has a shaggy brown coat, with parts of its fur shedding, and prominent curved horns. The background is mostly a light, overcast sky, which makes the bison stand out prominently in the image. There is green grass and some small wild plants in the foreground. The overall scene appears natural and serene, likely in a prairie or grassland environment.
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
17 changes: 9 additions & 8 deletions examples/basic/previous-response-id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Agent, run } from '@openai/agents';

async function main() {
async function runBasic() {
const agent = new Agent({
name: 'Assistant',
instructions: 'You are a helpful assistant. be VERY concise.',
Expand All @@ -18,7 +18,7 @@ async function main() {
console.log(result.finalOutput); // e.g., Brasilia
}

async function mainStream() {
async function runStream() {
const agent = new Agent({
name: 'Assistant',
instructions: 'You are a helpful assistant. be VERY concise.',
Expand Down Expand Up @@ -54,7 +54,7 @@ async function mainStream() {
console.log();
}

async function promptAndRun() {
async function main() {
const readline = await import('node:readline/promises');
const rl = readline.createInterface({
input: process.stdin,
Expand All @@ -63,12 +63,13 @@ async function promptAndRun() {
const isStream = await rl.question('Run in stream mode? (y/n): ');
rl.close();
if (isStream.trim().toLowerCase() === 'y') {
await mainStream();
await runStream();
} else {
await main();
await runBasic();
}
}

if (require.main === module) {
promptAndRun().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/prompt-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main() {
console.log(result.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/remote-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function main() {
// This image shows the Golden Gate Bridge, a famous suspension bridge located in San Francisco, California. The bridge is painted in its signature "International Orange" color and spans the Golden Gate Strait, connecting San Francisco to Marin County. The photo is taken during daylight, with the city and hills visible in the background and water beneath the bridge. The bridge is an iconic landmark and a symbol of San Francisco.
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/remote-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function main() {
console.log(result.finalOutput);
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/stream-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async function main() {
// === Run complete ===
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function main() {
console.log();
}

if (require.main === module) {
main().catch(console.error);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion examples/basic/tool-use-behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ async function main() {
console.log(finalOutput3);
}

main();
main().catch((error) => {
console.error(error);
process.exit(1);
});
7 changes: 4 additions & 3 deletions examples/basic/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function main() {
// The weather in Tokyo is sunny with some wind, and the temperature ranges between 14°C and 20°C.
}

if (require.main === module) {
main();
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
Loading