Co-Pilot / 辅助式
更新于 24 days ago

openai-agents-js

Oopenai
2.2k
openai/openai-agents-js
84
Agent 评分

💡 摘要

一个用于在JavaScript/TypeScript中构建多智能体工作流的框架,支持各种集成和功能。

🎯 适合人群

JavaScript/TypeScript开发者人工智能研究人员科技产品经理从事人工智能解决方案的软件工程师对人工智能应用感兴趣的爱好者

🤖 AI 吐槽:看起来很能打,但别让配置把人劝退。

安全分析中风险

风险:Medium。建议检查:是否执行 shell/命令行指令;是否发起外网请求(SSRF/数据外发);API Key/Token 的获取、存储与泄露风险;文件读写范围与路径穿越风险;依赖锁定与供应链风险。以最小权限运行,并在生产环境启用前审计代码与依赖。

OpenAI Agents SDK (JavaScript/TypeScript)

npm version CI

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows in JavaScript/TypeScript. It is provider-agnostic, supporting OpenAI APIs and more.

[!NOTE] Looking for the Python version? Check out OpenAI Agents SDK Python.

Core concepts

  1. Agents: LLMs configured with instructions, tools, guardrails, and handoffs.
  2. Handoffs: Specialized tool calls for transferring control between agents.
  3. Guardrails: Configurable safety checks for input and output validation.
  4. Tracing: Built-in tracking of agent runs, allowing you to view, debug, and optimize your workflows.

Explore the examples/ directory to see the SDK in action.

Supported Features

  • [x] Multi-Agent Workflows: Compose and orchestrate multiple agents in a single workflow.
  • [x] Tool Integration: Seamlessly call tools/functions from within agent responses.
  • [x] Handoffs: Transfer control between agents dynamically during a run.
  • [x] Structured Outputs: Support for both plain text and schema-validated structured outputs.
  • [x] Streaming Responses: Stream agent outputs and events in real time.
  • [x] Tracing & Debugging: Built-in tracing for visualizing and debugging agent runs.
  • [x] Guardrails: Input and output validation for safety and reliability.
  • [x] Parallelization: Run agents or tool calls in parallel and aggregate results.
  • [x] Human-in-the-Loop: Integrate human approval or intervention into workflows.
  • [x] Realtime Voice Agents: Build realtime voice agents using WebRTC or WebSockets
  • [x] Local MCP Server Support: Give an Agent access to a locally running MCP server to provide tools
  • [x] Separate optimized browser package: Dedicated package meant to run in the browser for Realtime agents.
  • [x] Broader model support: Use non-OpenAI models through the Vercel AI SDK adapter
  • [ ] Long running functions: Suspend an agent loop to execute a long-running function and revive it later
  • [ ] Voice pipeline: Chain text-based agents using speech-to-text and text-to-speech into a voice agent

Get started

Supported environments

  • Node.js 22 or later
  • Deno
  • Bun

Experimental support:

  • Cloudflare Workers with nodejs_compat enabled

Check out the documentation for more detailed information.

Installation

npm install @openai/agents zod

The Agents SDK requires Zod v4; npm install zod pulls the latest v4.x by default.

Hello world example

import { Agent, run } from '@openai/agents'; const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant', }); const result = await run( agent, 'Write a haiku about recursion in programming.', ); console.log(result.finalOutput); // Code within the code, // Functions calling themselves, // Infinite loop's dance.

(If running this, ensure you set the OPENAI_API_KEY environment variable)

Functions example

import { z } from 'zod'; import { Agent, run, tool } from '@openai/agents'; const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', parameters: z.object({ city: z.string() }), execute: async (input) => { return `The weather in ${input.city} is sunny`; }, }); const agent = new Agent({ name: 'Data agent', instructions: 'You are a data agent', tools: [getWeatherTool], }); async function main() { const result = await run(agent, 'What is the weather in Tokyo?'); console.log(result.finalOutput); } main().catch(console.error);

Handoffs example

import { z } from 'zod'; import { Agent, run, tool } from '@openai/agents'; const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', parameters: z.object({ city: z.string() }), execute: async (input) => { return `The weather in ${input.city} is sunny`; }, }); const dataAgent = new Agent({ name: 'Data agent', instructions: 'You are a data agent', handoffDescription: 'You know everything about the weather', tools: [getWeatherTool], }); // Use Agent.create method to ensure the finalOutput type considers handoffs const agent = Agent.create({ name: 'Basic test agent', instructions: 'You are a basic agent', handoffs: [dataAgent], }); async function main() { const result = await run(agent, 'What is the weather in San Francisco?'); console.log(result.finalOutput); } main().catch(console.error);

Voice Agent

import { z } from 'zod'; import { RealtimeAgent, RealtimeSession, tool } from '@openai/agents-realtime'; const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', parameters: z.object({ city: z.string() }), execute: async (input) => { return `The weather in ${input.city} is sunny`; }, }); const agent = new RealtimeAgent({ name: 'Data agent', instructions: 'You are a data agent. When you are asked to check weather, you must use the available tools.', tools: [getWeatherTool], }); // Intended to run in the browser const { apiKey } = await fetch('/path/to/ephemeral/key/generation').then( (resp) => resp.json(), ); // Automatically configures audio input/output — start talking const session = new RealtimeSession(agent); await session.connect({ apiKey });

Running Complete Examples

The examples/ directory contains a series of examples to get started:

  • pnpm examples:basic - Basic example with handoffs and tool calling
  • pnpm examples:agents-as-tools - Using agents as tools for translation
  • pnpm examples:tools-web-search - Using the web search tool
  • pnpm examples:tools-file-search - Using the file search tool
  • pnpm examples:deterministic - Deterministic multi-agent workflow
  • pnpm examples:parallelization - Running agents in parallel and picking the best result
  • pnpm examples:human-in-the-loop - Human approval for certain tool calls
  • pnpm examples:streamed - Streaming agent output and events in real time
  • pnpm examples:streamed:human-in-the-loop - Streaming output with human-in-the-loop approval
  • pnpm examples:routing - Routing between agents based on language or context
  • pnpm examples:realtime-demo - Framework agnostic Voice Agent example
  • pnpm examples:realtime-next - Next.js Voice Agent example application

The agent loop

When you call Runner.run(), the SDK executes a loop until a final output is produced.

  1. The agent is invoked with the given input, using the model and settings configured on the agent (or globally).
  2. The LLM returns a response, which may include tool calls or handoff requests.
  3. If the response contains a final output (see below), the loop ends and the result is returned.
  4. If the response contains a handoff, the agent is switched to the new agent and the loop continues.
  5. If there are tool calls, the tools are executed, their results are appended to the message history, and the loop continues.

You can control the maximum number of iterations with the maxTurns parameter.

Final output

The final output is the last thing the agent produces in the loop.

  1. If the agent has an outputType (structured output), the loop ends when the LLM returns a response matching that type.
  2. If there is no outputType (plain text), the first LLM response without tool calls or handoffs is considered the final output.

Summary of the agent loop:

  • If the current agent has an outputType, the loop runs until structured output of that type is produced.
  • If not, the loop runs until a message is produced with no tool calls or handoffs.

Error handling

  • If the maximum number of turns is exceeded, a MaxTurnsExceededError is thrown.
  • If a guardrail is triggered, a GuardrailTripwireTriggered exception is raised.

Documentation

To view the documentation locally:

pnpm docs:dev

Then visit http://localhost:4321 in your browser.

Development

If you want to contribute or edit the SDK/examples:

  1. Install dependencies

    pnpm install
  2. Build the project

    pnpm build && pnpm -r build-check
  3. Run tests and linter

    pnpm test && pnpm lint

See AGENTS.md and CONTRIBUTING.md for the full contributor guide.

Acknowledgements

We'd like to acknowledge the excellent work of the open-source community, especially:

We're committed to building the Agents SDK as an open source framework so others in the community can expand on our approach.

For more details, see the documentation or explore the examples/ directory.

五维分析
清晰度9/10
创新性8/10
实用性9/10
完整性8/10
可维护性8/10
优缺点分析

优点

  • 支持多智能体工作流
  • 提供者无关的框架
  • 内置跟踪和调试
  • 灵活的工具集成

缺点

  • 高级功能的文档有限
  • 某些环境的实验性支持
  • 依赖外部库
  • 多智能体设置可能复杂

相关技能

ai-context

B
toolCo-Pilot / 辅助式
76/ 100

“看起来很能打,但别让配置把人劝退。”

hono-skill

B
toolCo-Pilot / 辅助式
70/ 100

“它只是一个 CLI 工具的薄包装,提供了便利性,但并没有提供多少代理本身无法编写的智能。”

pytorch

S
toolCode Lib / 代码库
92/ 100

“它是深度学习的瑞士军刀,但祝你好运能从47种安装方法里找到那个不会搞崩你系统的那一个。”

免责声明:本内容来源于 GitHub 开源项目,仅供展示和评分分析使用。

版权归原作者所有 openai.