-
Notifications
You must be signed in to change notification settings - Fork 148
AI-266: Add @temporalio/openai-agents sample suite #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d33a6dc
AI-266: Add @temporalio/openai-agents sample suite
xumaple d10c33d
refactor down
xumaple 348c9f6
AI-266: Restructure openai-agents samples to standard single-src layout
xumaple 543f39c
Merge main into AI-266 openai-agents restructure
xumaple 7ca2174
AI-266: Reformat openai-agents README and reasoning-content to satisf…
xumaple e403ea7
AI-266: Give openai-agents a per-scenario post-create message
xumaple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules | ||
| lib | ||
| .eslintrc.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| const { builtinModules } = require('module'); | ||
|
|
||
| const ALLOWED_NODE_BUILTINS = new Set(['assert']); | ||
|
|
||
| module.exports = { | ||
| root: true, | ||
| parser: '@typescript-eslint/parser', | ||
| parserOptions: { | ||
| project: './tsconfig.json', | ||
| tsconfigRootDir: __dirname, | ||
| }, | ||
| plugins: ['@typescript-eslint', 'deprecation'], | ||
| extends: [ | ||
| 'eslint:recommended', | ||
| 'plugin:@typescript-eslint/eslint-recommended', | ||
| 'plugin:@typescript-eslint/recommended', | ||
| 'prettier', | ||
| ], | ||
| rules: { | ||
| // recommended for safety | ||
| '@typescript-eslint/no-floating-promises': 'error', // forgetting to await Activities and Workflow APIs is bad | ||
| 'deprecation/deprecation': 'warn', | ||
|
|
||
| // code style preference | ||
| 'object-shorthand': ['error', 'always'], | ||
|
|
||
| // relaxed rules, for convenience | ||
| '@typescript-eslint/no-unused-vars': [ | ||
| 'warn', | ||
| { | ||
| argsIgnorePattern: '^_', | ||
| varsIgnorePattern: '^_', | ||
| }, | ||
| ], | ||
| '@typescript-eslint/no-explicit-any': 'off', | ||
| }, | ||
| overrides: [ | ||
| { | ||
| files: ['src/*/workflows.ts', 'src/*/workflows-*.ts', 'src/*/workflows/*.ts'], | ||
| rules: { | ||
| 'no-restricted-imports': [ | ||
| 'error', | ||
| ...builtinModules.filter((m) => !ALLOWED_NODE_BUILTINS.has(m)).flatMap((m) => [m, `node:${m}`]), | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| lib | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 22 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| To begin development, install the Temporal CLI: | ||
|
|
||
| Mac: {cyan brew install temporal} | ||
| Other: Download and extract the latest release from https://github.com/temporalio/cli/releases/latest | ||
|
|
||
| Start Temporal Server: | ||
|
|
||
| {cyan temporal server start-dev} | ||
|
|
||
| Use Node version 18+ (v22.x is recommended): | ||
|
|
||
| Mac: {cyan brew install node@22} | ||
| Other: https://nodejs.org/en/download/ | ||
|
|
||
| This sample has several scenarios under {cyan src/}. Using two other shells, start a Worker for one scenario and run its client (example: {cyan basic}): | ||
|
|
||
| {cyan OPENAI_API_KEY=<your-key> npx ts-node src/basic/worker.ts} | ||
| {cyan npx ts-node src/basic/client.ts hello-world} | ||
|
|
||
| See README.md for the full list of scenarios. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| lib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| printWidth: 120 | ||
| singleQuote: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # OpenAI Agents | ||
|
|
||
| These samples use the `@temporalio/openai-agents` integration to run [OpenAI Agents SDK](https://github.com/openai/openai-agents-js) agents as Temporal Workflows. Agent orchestration — the agent loop, handoffs, tool calls, and guardrails — runs inside the Workflow, while model calls run as durable Activities, so they retry on failure and are not repeated during Workflow replay. | ||
|
|
||
| This is a single project: one `package.json` and one set of configs at the `openai-agents/` root, with each scenario in its own subdirectory. Run `npm install` once here, then run any scenario by path (see each scenario's README). The integration package itself is documented in the [`@temporalio/openai-agents` README](https://github.com/temporalio/sdk-typescript/tree/main/packages/openai-agents). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| These apply to every sample in this directory: | ||
|
|
||
| - A running Temporal dev server: `temporal server start-dev`. | ||
| - Node 22 or later. | ||
| - An OpenAI API key: `export OPENAI_API_KEY=...`. | ||
| - Dependencies installed once at the `openai-agents/` root: `npm install`. | ||
|
|
||
| Each scenario's README describes how to start its Worker and run its scenarios by path. | ||
|
|
||
| ## Samples | ||
|
|
||
| | Sample | Demonstrates | | ||
| | :----------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | [`basic`](./src/basic) | A single agent plus the core building blocks: Activity-backed and inline tools, local-Activity tools, agent context, structured output, per-run model override, and dynamic instructions. | | ||
| | [`handoffs`](./src/handoffs) | A triage agent routes each request to a specialist agent, using both the `Agent[]` and `handoff()` forms and a per-handoff input filter. | | ||
| | [`agent-patterns`](./src/agent-patterns) | Multi-agent orchestration patterns: deterministic chaining, parallelization, LLM-as-judge, agents-as-tools, and input/output guardrails. | | ||
| | [`sessions`](./src/sessions) | Conversation history with `WorkflowSafeMemorySession`, including carrying history across a `continueAsNew` boundary. | | ||
| | [`human-approval`](./src/human-approval) | A human-in-the-loop tool that pauses the run for an `approve` Signal, then resumes by serializing and rehydrating the run state across `continueAsNew`. | | ||
| | [`tools`](./src/tools) | Server-side hosted tools — web search, image generation, and code interpreter — executed by the model provider during the model Activity. | | ||
| | [`tracing`](./src/tracing) | The three supported tracing paths: a custom `TracingProcessor`, the OpenAI hosted exporter, and OpenTelemetry, plus `temporal:*` orchestration spans. | | ||
| | [`model-providers`](./src/model-providers) | Pass a custom `ModelProvider` to point an agent at any OpenAI-compatible endpoint. | | ||
| | [`reasoning-content`](./src/reasoning-content) | Read a reasoning model's `reasoning_content` field by calling the `openai` SDK directly from an Activity. | | ||
| | [`mcp`](./src/mcp) | Stateless and stateful Model Context Protocol servers (stdio, Streamable HTTP, SSE, and prompt servers) running locally. | | ||
| | [`hosted-mcp`](./src/hosted-mcp) | A `HostedMCPTool` the model calls server-side, with and without a Signal-driven approval round trip. | | ||
| | [`multi-agent`](./src/multi-agent) | A planner agent fans out concurrent web searches and a writer agent synthesizes a final report. | | ||
| | [`stateful-conversation`](./src/stateful-conversation) | A long-running, multi-turn Workflow driven by Updates and Queries, with triage handoffs and `continueAsNew` to bound history. | | ||
| | [`nexus-tools`](./src/nexus-tools) | Expose a [Nexus](https://docs.temporal.io/nexus) Operation as an agent tool with `nexusOperationAsTool`. | | ||
|
|
||
| ## Feature support | ||
|
|
||
| Any OpenAI Agents SDK `ModelProvider` can drive the model Activity. The provider runs in the Activity, never inside the Workflow sandbox. | ||
|
|
||
| | Feature | Status | Notes | | ||
| | :---------------------- | :------------ | :-------------------------------------------------------------------------------------- | | ||
| | Multi-turn agents | Supported | Agent loop runs durably in the Workflow | | ||
| | Handoffs | Supported | `Agent` and `handoff()` forms | | ||
| | Inline function tools | Supported | Must be deterministic | | ||
| | Activity-backed tools | Supported | Via `activityAsTool()` | | ||
| | Nexus operation tools | Supported | Via `nexusOperationAsTool()` | | ||
| | Nested agent tools | Supported | Via `agentAsTool()` | | ||
| | Hosted tools | Supported | Executed server-side by the model provider | | ||
| | Stateless MCP servers | Supported | Via `StatelessMCPServerProvider` and `statelessMcpServer()` | | ||
| | Stateful MCP servers | Supported | Via `StatefulMCPServerProvider` and `statefulMcpServer()` | | ||
| | Sessions | Supported | Via `WorkflowSafeMemorySession`; upstream `MemorySession` is rejected | | ||
| | Run state and approvals | Supported | Serialize with `result.state.toString()` and rehydrate with `RunState.fromString` | | ||
| | Guardrails | Supported | Guardrail callbacks must be deterministic | | ||
| | Tracing | Supported | OpenAI hosted traces, custom `TracingProcessor`s, OTel, and optional `temporal:*` spans | | ||
| | Agent context | Supported | Activity tools receive a copy | | ||
| | `continueAsNew` | Supported | Plugin config propagates to the continuation | | ||
| | Child Workflows | Supported | Plugin config propagates to children | | ||
| | Local Activities | Supported | Set `useLocalActivity: true` in `modelParams` | | ||
| | Model override per run | Supported | `runConfig.model` accepts a string model name | | ||
| | Streaming | Not supported | Use `runner.run()` | | ||
| | Voice agents | Not supported | | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| { | ||
| "name": "temporal-openai-agents", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "build": "tsc --build", | ||
| "build.watch": "tsc --build --watch", | ||
| "format": "prettier --write .", | ||
| "format:check": "prettier --check .", | ||
| "lint": "eslint .", | ||
| "test": "mocha --exit --require ts-node/register --require source-map-support/register \"src/*/mocha/*.test.ts\"" | ||
| }, | ||
| "dependencies": { | ||
| "@temporalio/activity": "^1.18.0", | ||
| "@temporalio/client": "^1.18.0", | ||
| "@temporalio/nexus": "^1.18.0", | ||
| "@temporalio/openai-agents": "^1.18.0", | ||
| "@temporalio/worker": "^1.18.0", | ||
| "@temporalio/workflow": "^1.18.0", | ||
| "@openai/agents-core": "^0.11.6", | ||
| "@openai/agents-openai": "^0.11.6", | ||
| "@modelcontextprotocol/sdk": "^1.29.0", | ||
| "@opentelemetry/api": "^1.9.0", | ||
| "openai": "^6.0.0", | ||
| "nexus-rpc": "^0.0.2", | ||
| "nanoid": "3.x", | ||
| "zod": "^4.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@temporalio/testing": "^1.18.0", | ||
| "@tsconfig/node22": "^22.0.0", | ||
| "@types/mocha": "8.x", | ||
| "@types/node": "^22.9.1", | ||
| "@typescript-eslint/eslint-plugin": "^8.18.0", | ||
| "@typescript-eslint/parser": "^8.18.0", | ||
| "@opentelemetry/sdk-trace-base": "^1.30.0", | ||
| "eslint": "^8.57.1", | ||
| "eslint-config-prettier": "^9.1.0", | ||
| "eslint-plugin-deprecation": "^3.0.0", | ||
| "mocha": "8.x", | ||
| "prettier": "^3.4.2", | ||
| "ts-node": "^10.9.2", | ||
| "typescript": "^5.6.3", | ||
| "source-map-support": "^0.5.21" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # OpenAI Agents: Agent Patterns | ||
|
|
||
| Demonstrates common multi-agent orchestration patterns with the Temporal OpenAI Agents | ||
| integration. Each pattern is its own Workflow in `src/agent-patterns/workflows.ts`. | ||
|
|
||
| Scenarios: | ||
|
|
||
| - **deterministic** — three agents run in sequence, each gating the next (outline → draft → polish). | ||
| - **parallelization** — `Promise.all` fans out to three agents, then a judge agent picks the best answer. | ||
| - **llm-as-judge** — a generate→judge loop that retries until the judge approves. | ||
| - **agents-as-tools** — an orchestrator uses `agentAsTool` to call a specialist agent as a tool. | ||
| - **input-guardrails** — `runConfig.inputGuardrails` blocks forbidden input before the model runs. | ||
| - **output-guardrails** — `runConfig.outputGuardrails` blocks unsafe model output after the model runs. | ||
|
|
||
| ## Run | ||
|
|
||
| Run these from the `openai-agents/` root (run `npm install` there once first). | ||
|
|
||
| ```bash | ||
| # In one terminal, start the Worker (requires a local Temporal server and OPENAI_API_KEY): | ||
| OPENAI_API_KEY=sk-... npx ts-node src/agent-patterns/worker.ts | ||
|
|
||
| # In another terminal, start a scenario: | ||
| npx ts-node src/agent-patterns/client.ts deterministic | ||
| npx ts-node src/agent-patterns/client.ts parallelization | ||
| npx ts-node src/agent-patterns/client.ts llm-as-judge | ||
| npx ts-node src/agent-patterns/client.ts agents-as-tools | ||
| npx ts-node src/agent-patterns/client.ts input-guardrails | ||
| npx ts-node src/agent-patterns/client.ts output-guardrails | ||
| ``` | ||
|
|
||
| ## Test | ||
|
|
||
| ```bash | ||
| npx mocha --exit --require ts-node/register --require source-map-support/register "src/agent-patterns/mocha/*.test.ts" | ||
| ``` | ||
|
|
||
| Tests run a real Worker against `TestWorkflowEnvironment` with a scripted fake model, so no | ||
| `OPENAI_API_KEY` is required. Each pattern has a test asserting its mechanism (call counts, | ||
| history threading, tool round-trips, and guardrail tripwires). |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.