# sylarlong/openai-iztro-agents-js architecture

> Architecture diagram of the GitHub repository sylarlong/openai-iztro-agents-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sylarlong/openai-iztro-agents-js
- Repository: https://github.com/sylarlong/openai-iztro-agents-js

## Overview

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: [`src/index.ts`](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/index.ts) (Index), [`src/agent.ts`](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/agent.ts) (Agent factory), [`src/model.ts`](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/model.ts) (Model factory), [`src/session.ts`](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/session.ts) (Chat session)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_src_index["Index<br/>re-exports<br/>[index.ts]"]
end

subgraph group_core_adapter["Core Adapter"]
  node_src_agent["Agent factory<br/>adapter<br/>[agent.ts]"]
  node_src_model["Model factory<br/>model-boundary<br/>[model.ts]"]
  node_src_session["Chat session<br/>memory-adapter<br/>[session.ts]"]
end

subgraph group_runtime_boundaries["Runtime Boundaries"]
  node_openai_agents_sdk{{"Agents SDK<br/>runtime"}}
  node_openai_chat_model["Chat model<br/>sdk-model"]
  node_iztro_backend[("Iztro backend<br/>hosted-service")]
  node_local_tools["Local tools<br/>sdk-tools"]
  node_tool_events["Tool events<br/>event-stream"]
  node_conversation_store[("Conversation store<br/>server-memory")]
end

subgraph group_docs_and_tests["Docs & Tests"]
  node_examples_readme["Examples<br/>usage-docs<br/>[README.md]"]
  node_tests_mock[("Mock backend<br/>test-fixture<br/>[_mock.ts]")]
  node_tests_factories["Factory tests<br/>[factories.test.ts]"]
  node_tests_streaming["Streaming tests<br/>[streaming.test.ts]"]
  node_tests_sessions["Session tests<br/>[session.test.ts]"]
  node_tests_tools["Tool-loop tests<br/>[toolLoops.test.ts]"]
end

node_consumer(("Consumer code<br/>caller"))

node_consumer -->|"imports"| node_src_index
node_src_index -->|"exports"| node_src_agent
node_src_index -->|"exports"| node_src_model
node_src_index -->|"exports"| node_src_session
node_src_index -->|"passes through"| node_openai_agents_sdk
node_src_agent -->|"builds on"| node_openai_agents_sdk
node_src_agent -->|"uses"| node_src_model
node_src_model -->|"constructs"| node_openai_chat_model
node_openai_chat_model -->|"calls"| node_iztro_backend
node_src_session -->|"maps to"| node_conversation_store
node_openai_agents_sdk -->|"orchestrates"| node_local_tools
node_openai_agents_sdk -->|"emits"| node_tool_events
node_src_model -->|"exposes"| node_tool_events
node_src_session -->|"persists"| node_iztro_backend
node_examples_readme -.->|"demonstrates"| node_src_index
node_tests_mock -.->|"stubs"| node_openai_chat_model
node_tests_mock -.->|"simulates"| node_conversation_store
node_tests_factories -.->|"verifies"| node_src_agent
node_tests_streaming -.->|"verifies"| node_tool_events
node_tests_sessions -.->|"verifies"| node_src_session
node_tests_tools -.->|"verifies"| node_local_tools

click node_src_index "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/index.ts"
click node_src_agent "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/agent.ts"
click node_src_model "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/model.ts"
click node_src_session "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/session.ts"
click node_examples_readme "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/examples/README.md"
click node_tests_mock "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/_mock.ts"
click node_tests_factories "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/factories.test.ts"
click node_tests_streaming "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/streaming.test.ts"
click node_tests_sessions "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/session.test.ts"
click node_tests_tools "https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/toolLoops.test.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index toneBlue
class node_src_agent,node_src_model,node_src_session toneAmber
class node_openai_agents_sdk,node_openai_chat_model,node_iztro_backend,node_local_tools,node_tool_events,node_conversation_store toneMint
class node_examples_readme,node_tests_mock,node_tests_factories,node_tests_streaming,node_tests_sessions,node_tests_tools toneRose
class node_consumer toneNeutral
```

## Components

### Public API

Package entrypoint and user-facing factories

- [Index](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/index.ts): re-exports · Package entrypoint exporting factories, SDK primitives, and constants · `src/index.ts`

### Core Adapter

Model, agent, and session layers

- [Agent factory](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/agent.ts): adapter · Builds ready-to-use Agents around the hosted Iztro models · `src/agent.ts`
- [Model factory](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/model.ts): model-boundary · Creates OpenAI chat-completions models pointed at the Iztro backend · `src/model.ts`
- [Chat session](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/src/session.ts): memory-adapter · Maps sessions to server-side conversation storage · `src/session.ts`

### Runtime Boundaries

External SDK/runtime and hosted service boundaries

- **Agents SDK**: runtime · OpenAI Agents runtime used for tools, streaming, tracing, and orchestration
- **Chat model**: sdk-model · OpenAI chat-completions model abstraction used by the adapter
- **Iztro backend**: hosted-service · Remote hosted Ziwei/Qimen service that performs reasoning and hidden tools
- **Local tools**: sdk-tools · User-defined function tools, MCP servers, and approval flows handled by the SDK
- **Tool events**: event-stream · Surface for hidden hosted-tool activity in metadata and streaming
- **Conversation store**: server-memory · Server-side persisted chats keyed by external user and conversation IDs

### Docs & Tests

Examples and validation surfaces

- [Examples](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/examples/README.md): usage-docs · Scenario-based sample integrations and architecture documentation · `examples/README.md`
- [Mock backend](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/_mock.ts): test-fixture · Offline stubbed chat endpoint plus in-memory conversation store · `tests/_mock.ts`
- [Factory tests](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/factories.test.ts): tests · Covers factory configuration and adapter wiring · `tests/factories.test.ts`
- [Streaming tests](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/streaming.test.ts): tests · Covers streamed responses and tool-event ordering · `tests/streaming.test.ts`
- [Session tests](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/session.test.ts): tests · Covers conversation memory and resume behavior · `tests/session.test.ts`
- [Tool-loop tests](https://github.com/sylarlong/openai-iztro-agents-js/blob/master/tests/toolLoops.test.ts): tests · Covers local tools, approvals, and multi-step loops · `tests/toolLoops.test.ts`

### Other components

- **Consumer code**: caller · Downstream app code using the adapter package

## Connections

- 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
