How sylarlong/openai-iztro-agents-js works
1. What this repo is
- openai-iztro-agents is a TypeScript adapter library, not a standalone app.
- It sits on top of @openai/agents and makes the hosted Ziwei and Qimen models look like ordinary OpenAI Agents SDK models/agents.
2. Public API surface
- The package entrypoint (src/index.ts) re-exports the main factories and SDK primitives.
- The core exports are:
- iztroZiweiAgent, iztroZiweiModel
- iztroQimenAgent, iztroQimenModel
- ChatSession, listUserConversations
- constants like DEFAULT_BASE_URL, model names, and tool-event type
- passthroughs such as Agent, Runner, run, and tool from @openai/agents
3. Hosted model boundary
- src/model.ts is the boundary to the remote Iztro service.
- It constructs stock OpenAIChatCompletionsModel instances pointed at the Iztro backend, using apiKey, baseUrl, and modelName.
- The hosted models handle the astrology-specific reasoning and their own hidden tools server-side.
4. Agent factory layer
- src/agent.ts wraps the model factories into ready-to-use SDK Agent instances.
- This layer preserves normal SDK behavior: local tools, MCP servers, modelSettings, streaming, tracing, handoffs, and tool choice all work as they do in vanilla @openai/agents.
5. Conversation memory subsystem
- src/session.ts implements ChatSession, which maps to server-side conversation storage.
- Memory is not kept in-process; it is owned by the Iztro backend and keyed by externalUserId plus a server-generated conversationId.
- listUserConversations is the management surface for enumerating a user’s stored chats.
6. Key request/data flow
- A caller creates an Iztro agent, optionally a ChatSession, then passes both into run(...).
- The SDK sends the prompt to the hosted model; if session is attached, conversation state is resolved server-side.
- The model may produce a final answer directly or request hidden hosted astrology tools.
7. Tooling split: hosted vs local
- A major architectural boundary is that Iztro tools are hidden inside the model.
- Local function tools, MCP servers, and human-in-the-loop approval still run through the normal OpenAI Agents runtime.
- This means your own tools are visible to the SDK; the Ziwei/Qimen tools are not exposed as regular tool_calls.
8. Tool-event reporting
- Because hosted tools are hidden, the wrapper surfaces them as tool events in the response metadata instead of standard local tool calls.
- The repo preserves older compatibility names (.iztroTools, lastIztroTools, IztroToolsStreamEvent) but the current architecture centers on toolEvent / IztroToolEvent.
9. Streaming and interruptions
- Streaming follows the SDK’s standard run(..., { stream: true }) flow.
- For Iztro-hosted tools, the stream emits a tool-event marker before the text that depends on that tool result.
- Local approval workflows still use native SDK interruptions for needsApproval tools.
10. Configuration and compatibility
- The library uses JS/TS naming (camelCase) for options like apiKey, baseUrl, externalUserId, and modelName.
- Defaults and environment-derived behavior are part of the adapter layer, so consumers can use it like a regular SDK model/provider rather than a custom runtime.
11. Testing architecture
- tests/_mock.ts provides the offline test backend: a stubbed chat-completions endpoint plus an in-memory conversation store.
- The test suite exercises the main architectural seams: tool loops, streaming, human approval, session memory, and factory configuration.
- tests/live.test.ts is opt-in and hits a real deployed backend; the rest is deterministic and offline.
12. Examples as architecture documentation
- examples/ is organized as scenario-based samples rather than a single app.
- The examples demonstrate the intended integration patterns: local tools, multiple tools, streaming, memory/resume, human-in-the-loop, cost control, Chinese chat, and agent-as-tool composition.
13. Repo shape and runtime
- This is a Node/TypeScript package with vitest for tests and standard npm packaging metadata.
- The external dependency that materially defines the architecture is @openai/agents; the Iztro backend is the other critical service boundary.
14. Diagram-worthy summary
- The architecture is best drawn as:
- caller code → Iztro agent/model factory → OpenAI Agents SDK runtime → hosted Iztro model/backend
- with two side channels:
- local tools/MCP/approval through the SDK
- conversation memory through the server-side ChatSession
Main entry points: index.ts Index · agent.ts Agent factory · model.ts Model factory · session.ts Chat session
Components 17
Public API
Package entrypoint and user-facing factories
- Index · Package entrypoint exporting factories, SDK primitives, and constants
src/index.ts
Core Adapter
Model, agent, and session layers
- Agent factory · Builds ready-to-use Agents around the hosted Iztro models
src/agent.ts - Model factory · Creates OpenAI chat-completions models pointed at the Iztro backend
src/model.ts - Chat session · Maps sessions to server-side conversation storage
src/session.ts
Runtime Boundaries
External SDK/runtime and hosted service boundaries
- Agents SDK · OpenAI Agents runtime used for tools, streaming, tracing, and orchestration
- Chat model · OpenAI chat-completions model abstraction used by the adapter
- Iztro backend · Remote hosted Ziwei/Qimen service that performs reasoning and hidden tools
- Local tools · User-defined function tools, MCP servers, and approval flows handled by the SDK
- Tool events · Surface for hidden hosted-tool activity in metadata and streaming
- Conversation store · Server-side persisted chats keyed by external user and conversation IDs
Docs & Tests
Examples and validation surfaces
- Examples · Scenario-based sample integrations and architecture documentation
examples/README.md - Mock backend · Offline stubbed chat endpoint plus in-memory conversation store
tests/_mock.ts - Factory tests · Covers factory configuration and adapter wiring
tests/factories.test.ts - Streaming tests · Covers streamed responses and tool-event ordering
tests/streaming.test.ts - Session tests · Covers conversation memory and resume behavior
tests/session.test.ts - Tool-loop tests · Covers local tools, approvals, and multi-step loops
tests/toolLoops.test.ts
Other components
- Consumer code · Downstream app code using the adapter package
Connections 21
- Consumer code → Index: imports · Consumers enter through the package entrypoint
- Index → Agent factory: exports · Re-exports agent factories
- Index → Model factory: exports · Re-exports model factories
- Index → Chat session: exports · Re-exports session helpers
- Index → Agents SDK: passes through · Re-exports SDK primitives like Agent, Runner, run, and tool
- Agent factory → Agents SDK: builds on · Wraps the SDK Agent runtime
- Agent factory → Model factory: uses · Agent factories are composed around model factories
- Model factory → Chat model: constructs · Creates stock chat-completions model instances
- Chat model → Iztro backend: calls · Requests are sent to the hosted Iztro service
- Chat session → Conversation store: maps to · Sessions resolve to server-side conversation storage
- Agents SDK → Local tools: orchestrates · Local tools, MCP, and approval stay in the normal SDK path
- Agents SDK → Tool events: emits · Hidden hosted-tool activity is surfaced as tool events
- Model factory → Tool events: exposes · Hosted astrology tool usage is reported as tool events rather than local tool calls
- Chat session → Iztro backend: persists · Conversation state is owned by the backend, not in-process
- Examples → Index: demonstrates · Examples show the intended integration patterns
- Mock backend → Chat model: stubs · Offline tests replace the chat endpoint with a mock backend
- Mock backend → Conversation store: simulates · Offline tests include an in-memory conversation store
- Factory tests → Agent factory: verifies · Checks factory wiring and configuration
- Streaming tests → Tool events: verifies · Checks streamed tool-event behavior
- Session tests → Chat session: verifies · Checks memory and resume behavior
- Tool-loop tests → Local tools: verifies · Checks local tools, handoffs, and approval workflows