Commit Graph

88 Commits

Author SHA1 Message Date
Ibrahim Kazimov 97c39b316c
feat: add tool allowlist, denylist, preset list (#83)
* feat: add allowlist denylist and preset list for tools

* feat: update readme and add AGENT_FRAMEWORK_DISALLOWED

* fix: update filtering logic to allow custom tools

* fix: enhance tool registration and filtering for runtime-added tools

---------

Co-authored-by: MrAvalonApple <74775400+ibrahimkazimov@users.noreply.github.com>
2026-04-08 02:04:40 +08:00
JackChen 48fbec6659
Merge pull request #70 from EchoOfZion/feature/smart-shortcircuit
feat: skip coordinator for simple goals in runTeam()
2026-04-07 23:52:16 +08:00
EchoOfZion 9463dbb28e refactor(orchestrator): address PR #70 review feedback
Addresses all five review points from @JackChen-me on PR #70:

1. Extract shared keyword helpers into src/utils/keywords.ts so the
   short-circuit selector and Scheduler.capability-match cannot drift.
   Both orchestrator.ts and scheduler.ts now import the same module.

2. selectBestAgent now mirrors Scheduler.capability-match exactly,
   including the asymmetric use of agent.model: agentKeywords includes
   model, agentText does not. This restores parity with the documented
   capability-match behaviour.

3. Remove isSimpleGoal and selectBestAgent from the public barrel
   (src/index.ts). They remain exported from orchestrator.ts for unit
   tests but are no longer part of the package API surface.

4. Forward the AbortSignal from runTeam(options) through the
   short-circuit path. runAgent() now accepts an optional
   { abortSignal } argument; runTeam's short-circuit branch passes
   the caller's signal so cancellation works for simple goals too.

5. Tighten the collaborate/coordinate complexity regexes so they only
   fire on imperative directives ("collaborate with X", "coordinate
   the team") and not on descriptive uses ("explain how pods
   coordinate", "what is microservice collaboration").

Also fixes a pre-existing test failure in token-budget.test.ts:
"enforces orchestrator budget in runTeam" was using "Do work" as its
goal which now short-circuits, so the coordinator path the test was
exercising never ran. Switched to a multi-step goal.

Adds 60 new tests across short-circuit.test.ts and the new
keywords.test.ts covering all five fixes.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 21:46:03 +09:00
EchoOfZion cfbbd24601 feat: skip coordinator for simple goals in runTeam()
When a goal is short (<200 chars) and contains no multi-step or
coordination signals, runTeam() now dispatches directly to the
best-matching agent — skipping the coordinator decomposition and
synthesis round-trips. This saves ~2 LLM calls worth of tokens
and latency for genuinely simple goals.

Complexity detection uses regex patterns for sequencing markers
(first...then, step N, numbered lists), coordination language
(collaborate, coordinate, work together), parallel execution
signals, and multi-deliverable patterns.

Agent selection reuses the same keyword-affinity scoring as the
capability-match scheduler strategy to pick the most relevant
agent from the team roster.

- Add isSimpleGoal() and selectBestAgent() (exported for testing)
- Add 35 unit tests covering heuristic edge cases and integration
- Update existing runTeam tests to use complex goals

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 21:21:36 +09:00
JackChen 607ba57a69
Merge pull request #77 from JackChen-me/fix/per-agent-serialization-72
fix: add per-agent mutex to prevent concurrent runs on same Agent instance
2026-04-07 11:20:40 +08:00
JackChen 5a67d559a3 chore: update .gitignore and revise MCP decision in DECISIONS.md 2026-04-07 11:20:32 +08:00
JackChen a29d87f384 docs: update CLAUDE.md concurrency section for per-agent mutex 2026-04-07 10:46:08 +08:00
JackChen 73b2454c2f fix: add per-agent mutex to prevent concurrent runs on same Agent instance (#72)
AgentPool now maintains a per-agent Semaphore(1) that serializes
concurrent run() calls targeting the same Agent. This prevents
shared-state races on Agent.state (status, messages, tokenUsage)
when multiple independent tasks are assigned to the same agent.

Lock acquisition order: per-agent lock first, then pool semaphore,
so queued tasks don't waste pool slots while waiting.
2026-04-07 03:42:24 +08:00
Ibrahim Kazimov 60fb2b142e
feat: implement token budget management in agent and orchestrator (#71)
* feat: implement token budget management in agent and orchestrator

* fix: resolve TypeScript type errors in event and trace handlers

* feat: add budget exceeded event handling in agent and orchestrator

---------

Co-authored-by: MrAvalonApple <74775400+ibrahimkazimov@users.noreply.github.com>
2026-04-06 23:14:08 +08:00
JackChen 1e3bd1013e fix: correct Team constructor usage in abort-signal tests 2026-04-06 12:54:24 +08:00
Jason 336d94e50d
feat: add AbortSignal support to runTeam() and runTasks() (#69)
Fixes #61

Thread AbortSignal from the top-level API through RunContext to
executeQueue(), enabling graceful cancellation in Express, Next.js,
serverless, and CLI scenarios.

Changes:
- Added optional  to RunContext interface
-  now accepts
-  now accepts
- executeQueue() checks signal.aborted before each dispatch round
  and skips remaining tasks when cancelled
- Signal is forwarded to coordinator's run() and per-task pool.run()
  so in-flight LLM calls are also cancelled
- Full backward compatibility: both methods work without options

The abort infrastructure already existed at lower layers
(AgentRunner, Agent, AgentPool) — this commit bridges the last gap
at the orchestrator level.

Co-authored-by: JasonOA888 <JasonOA888@users.noreply.github.com>
2026-04-06 12:49:01 +08:00
JackChen d59898ce3d
docs: update coverage badge from 71% to 88% (#57) 2026-04-05 14:34:29 +08:00
JackChen c23a20bb6c
test: add LLM adapter contract tests, improve coverage from 71% to 88% (#56)
- Add contract tests for Anthropic, OpenAI, Gemini, Copilot adapters
- Add optional E2E test suite (tests/e2e/, run with npm run test:e2e)
- Add shared test fixtures (tests/helpers/llm-fixtures.ts)
- Configure vitest to exclude e2e tests by default
- Add "files" field to package.json to reduce npm package size by 50%
- Align npm description with GitHub repo description
- Bump version to 1.0.1
2026-04-05 14:14:43 +08:00
JackChen c6b971bc19 chore: bump version to 1.0.0 2026-04-05 13:41:07 +08:00
JackChen 7b77ede3ba
docs: add coverage badge to README (#55) 2026-04-05 13:40:25 +08:00
JackChen 45304dffcf
test: improve coverage from 37% to 71% (#41) (#53)
Add 6 test files covering scheduler strategies, team/messaging lifecycle,
orchestrator (runAgent/runTasks/runTeam), built-in tools, agent pool, and
LLM adapter layer. Add vitest.config.ts to scope coverage to src/.
2026-04-05 13:37:00 +08:00
JackChen 4a48c44d82
Merge pull request #52 from JackChen-me/docs/refresh-cache-bust
docs: refresh Star History and Contributors cache
2026-04-05 13:22:05 +08:00
JackChen 3b6640b3a2 docs: refresh Star History and Contributors cache bust params 2026-04-05 13:21:52 +08:00
JackChen c9358cd406
Merge pull request #51 from JackChen-me/docs/add-translations-cta
docs: add Translations section to README
2026-04-05 13:21:01 +08:00
JackChen b4cdf741bd docs: add Translations section to README 2026-04-05 13:20:49 +08:00
JackChen 96ad7c7664
Merge pull request #50 from JackChen-me/docs/sync-readme-with-recent-prs
docs: sync READMEs with recent PRs
2026-04-05 13:10:58 +08:00
JackChen 42fe51b3e1 docs: sync READMEs with recent PRs (#28, #32, #44, #45, #47, #49)
English README:
- Update source file count 27 → 33
- Add Human-in-the-Loop, Lifecycle Hooks, Loop Detection feature bullets
- Add Example 13 (Gemini)
- Add GrokAdapter to architecture diagram

Chinese README:
- Update source file count 27 → 33
- Add llama.cpp server to model-agnostic description
- Add Human-in-the-Loop, Lifecycle Hooks, Loop Detection feature bullets
- Update Quick Start API key list (add GEMINI_API_KEY, XAI_API_KEY)
- Add Example 12 (Grok) and Example 13 (Gemini)
- Add GrokAdapter to architecture diagram
- Add Grok and llama.cpp rows to Provider table
- Add Local Model Tool-Calling section (fallback, timeout, troubleshooting)
- Add LLM Configuration Examples section (Grok config)
- Update Contributing section (remove outdated Gemini reference)
2026-04-05 13:08:46 +08:00
JackChen a86a448fa0
feat(agent): smart loop detection for stuck agents (#49)
* feat(agent): add smart loop detection for stuck agents (#16)

Detect when agents repeat the same tool calls or text outputs in a
sliding window. Three modes: warn (inject nudge, terminate on 2nd hit),
terminate (immediate stop), or custom callback. Fully opt-in via
`loopDetection` on AgentConfig — zero overhead when unconfigured.

* fix(agent): support async onLoopDetected callbacks and prevent orphaned tool_use events

- Await onLoopDetected callback result so async functions work correctly
  instead of silently falling through to 'continue'
- Move loop detection before yielding tool_use events so terminate mode
  never emits tool_use without a matching tool_result

* fix(agent): reset loopWarned on recovery and rename maxRepeatedToolCalls to maxRepetitions

- Reset loopWarned flag when the agent stops repeating, so a future
  loop gets a fresh warning cycle instead of immediate termination
- Rename maxRepeatedToolCalls → maxRepetitions since the threshold
  applies to both tool call and text output repetition detection

* test(agent): add tests for async callback, warn recovery, and injected warning text

- Verify async onLoopDetected callback is awaited correctly
- Verify loopWarned resets after recovery, giving fresh warning cycle
- Verify WARNING TextBlock is injected into user message content
2026-04-05 13:01:53 +08:00
JackChen ed3753c1f4 ci: fix cross-platform CI failures after Gemini adapter merge
- Add @google/genai to devDependencies so types are available for
  lint/test in CI (stays as optional peerDependency for consumers)
- Delete package-lock.json in CI before npm install to avoid
  Mac-generated lockfile missing Linux platform-specific rollup binaries
2026-04-05 12:09:31 +08:00
JackChen a68d961379 ci: use npm install instead of npm ci for cross-platform compat
npm ci fails on Linux CI when package-lock.json was generated on macOS,
because platform-specific optional deps (@rollup/rollup-linux-x64-gnu)
are missing from the lockfile. This is a known npm bug (#4828).
2026-04-05 12:06:17 +08:00
JackChen a24dcb462a
Merge pull request #47 from JackChen-me/feat/local-model-tool-calling
feat(llm): fallback tool-call extraction for local models
2026-04-05 12:00:35 +08:00
JackChen a4a1add8ca fix(agent): merge abort signals instead of overriding caller's signal
When both timeoutMs and a caller-provided abortSignal were set, the
timeout signal silently replaced the caller's signal. Now they are
combined via mergeAbortSignals() so either source can cancel the run.

Also removes dead array-handling branch in text-tool-extractor.ts
(extractJSONObjects only returns objects, never arrays).
2026-04-05 12:00:16 +08:00
JackChen bc31008f4e feat(llm): add fallback tool-call extraction for local models (#15)
Local models (Ollama, vLLM) sometimes return tool calls as text instead
of using the native tool_calls wire format. This adds a safety-net
extractor that parses tool calls from model text output when native
tool_calls is empty.

- Add text-tool-extractor with support for bare JSON, code fences,
  and Hermes <tool_call> tags
- Wire fallback into OpenAI adapter chat() and stream() paths
- Add onWarning callback when model ignores configured tools
- Add timeoutMs on AgentConfig for per-run abort (local models can
  be slow)
- Add 26 tests for extractor and fallback behavior
- Document local model compatibility in README
2026-04-05 11:49:10 +08:00
JackChen 9a81a13982
feat(llm): add Gemini adapter (#28)
feat: Add support for Gemini model
2026-04-05 11:48:32 +08:00
MrAvalonApple 553bf24e61 chore: support Node >=18, add optional @google/genai peer dependency and API key fallback 2026-04-04 22:12:28 +03:00
JackChen 9f5afb10f5
feat(orchestrator): add onApproval callback for human-in-the-loop (#32)
* feat(orchestrator): add onApproval callback for human-in-the-loop (#32)

Add an optional `onApproval` callback to OrchestratorConfig that gates
between task execution rounds. After each batch of parallel tasks
completes, the callback receives the completed tasks and the tasks about
to start, returning true to continue or false to abort gracefully.

Key changes:
- Add 'skipped' to TaskStatus for user-initiated abort (distinct from 'failed')
- Add skip(), skipRemaining(), cascadeSkip() to TaskQueue
- Add 'task_skipped' to OrchestratorEvent for progress monitoring
- Approval gate in executeQueue() with try/catch for callback errors
- Synthesis prompt now includes skipped tasks section
- 17 new tests covering queue skip operations and orchestrator integration

Closes #32

* docs: clarify onApproval contract and add missing test scenarios

- Document skip() cascade semantics, skipRemaining() in-flight constraint,
  and onApproval trigger conditions / mutation warning
- Add concurrency safety comment on completedThisRound
- Note task_skipped as breaking union addition on OrchestratorEvent
- Add 3 test scenarios: single-batch no-callback, mixed success/failure
  batch, and onProgress task_skipped event relay
2026-04-05 02:46:20 +08:00
JackChen d327acb89b
Merge pull request #35 from tmchow/osc/34-add-llama-cpp-server
docs: add llama.cpp server to local model providers
2026-04-05 02:26:35 +08:00
Trevin Chow 9e72c5029e docs: add llama.cpp server to local model providers
llama-server exposes an OpenAI-compatible API at /v1/chat/completions,
so it works with provider: 'openai' + baseURL like Ollama and vLLM.
Added to the supported providers table and the feature bullet.

Fixes #34
2026-04-04 10:49:21 -07:00
JackChen a1ccbfea61
feat(agent): add beforeRun / afterRun lifecycle hooks (#45)
* feat(agent): add beforeRun / afterRun lifecycle hooks (#31)

Add optional hook callbacks to AgentConfig for cross-cutting concerns
(guardrails, logging, token budgets) without modifying framework internals.

- beforeRun: receives prompt + agent config, can modify or throw to abort
- afterRun: receives AgentRunResult, can modify or throw to fail
- Works with all three execution modes: run(), prompt(), stream()
- 15 test cases covering modify, throw, async, composition, and history integrity

* fix(agent): preserve non-text content blocks in beforeRun hook

- applyHookContext now replaces only text blocks, keeping images and
  tool results intact (was silently stripping them)
- Use backward loop instead of reverse() + find() for efficiency
- Clarify JSDoc that only `prompt` is applied from hook return value
- Add test for mixed-content user messages

* fix(agent): address review feedback on beforeRun/afterRun hooks

- Normalize stream done event to always yield AgentRunResult
- Move transitionTo('completed') after afterRun to fix state ordering
- Strip hook functions from BeforeRunHookContext.agent to avoid self-references
- Pass originalPrompt to applyHookContext to avoid redundant message scan
- Clarify afterRun JSDoc: not called when the run throws
- Add tests: error-path skip, outputSchema+afterRun, ctx.agent shape, multi-turn hooks
2026-04-05 00:41:21 +08:00
Ibrahim Kazimov 944918d5ae
Merge branch 'main' into main 2026-04-04 13:43:40 +03:00
JackChen 25b144acf3 chore: remove hardcoded cache bust param from Star History URLs 2026-04-04 18:29:11 +08:00
Marcelo Ceccon 10074c9b7d
feat(llm): add first-class Grok (xAI) support with dedicated GrokAdapter (#44)
feat(llm): add first-class Grok (xAI) support with dedicated GrokAdapter
2026-04-04 18:20:55 +08:00
JackChen 071d5dce61 docs: add Latent Space mention to README 2026-04-03 19:18:52 +08:00
JackChen fea9df3f7a chore: add coverage/ to .gitignore 2026-04-03 17:38:41 +08:00
JackChen 2253122515 docs: reposition README around TypeScript-native, goal-to-result, lightweight differentiators 2026-04-03 17:35:12 +08:00
JackChen 0111876264
feat: add onTrace observability callback (#18)
Add lightweight onTrace callback to OrchestratorConfig that emits
structured span events (llm_call, tool_call, task, agent) with timing,
token usage, and runId correlation. Zero overhead when not subscribed.

Closes #18
2026-04-03 15:28:59 +08:00
JackChen 17546fd93e docs: merge Gemma 4 examples, reorder README sections
- Merge examples 08 (runTasks) and 09 (runTeam) into a single Gemma 4 example
- Renumber: structured output → 09, task retry → 10
- Move Author and Contributors sections to bottom in both READMEs
- Add Author section to English README
2026-04-03 14:32:48 +08:00
JackChen 27c0103736 docs: add examples for structured output and task retry 2026-04-03 14:23:22 +08:00
JackChen 043deaf562 docs: update CLAUDE.md with structured output and task retry 2026-04-03 14:16:44 +08:00
JackChen d9b20c0cf6 fix: guard retry fields against Infinity/NaN
Use Number.isFinite() to sanitize maxRetries, retryDelayMs, and
retryBackoff before entering the retry loop. Prevents unbounded
retries from Infinity or broken loop bounds from NaN.
2026-04-03 14:14:34 +08:00
JackChen 42f3717115 chore: bump version to 0.2.0 2026-04-03 14:13:33 +08:00
JackChen 2187f3b310
docs: add Structured Output and Task Retry to feature list (#39) 2026-04-03 14:11:54 +08:00
JackChen 4d7564b71a
feat: task-level retry with exponential backoff (#37)
* feat: add task-level retry with exponential backoff

Add `maxRetries`, `retryDelayMs`, and `retryBackoff` to task config.
When a task fails and retries remain, the orchestrator waits with
exponential backoff and re-runs the task with a fresh agent conversation.
A `task_retry` event is emitted via `onProgress` for observability.
Cascade failure only occurs after all retries are exhausted.

Closes #30

* fix: address review — extract executeWithRetry, add delay cap, fix tests

- Extract `executeWithRetry()` as a testable exported function
- Add `computeRetryDelay()` with 30s max cap (prevents runaway backoff)
- Remove retry fields from `ParsedTaskSpec` (dead code for runTeam path)
- Deduplicate retry event emission (single code path for both error types)
- Injectable delay function for test determinism
- Rewrite tests to call the real `executeWithRetry`, not a copy
- 15 tests covering: success, retry+success, retry+failure, backoff
  calculation, delay cap, delay function injection, no-retry default

* fix: clamp negative maxRetries/retryBackoff to safe values

- maxRetries clamped to >= 0 (negative values treated as no retry)
- retryBackoff clamped to >= 1 (prevents zero/negative delay oscillation)
- retryDelayMs clamped to >= 0
- Add tests for negative maxRetries and negative backoff

Addresses Codex review P1 on #37

* fix: accumulate token usage across retry attempts

Previously only the final attempt's tokenUsage was returned, causing
under-reporting of actual model consumption when retries occurred.
Now all attempts' token counts are summed in the returned result.

Addresses Codex review P2 (token usage) on #37
2026-04-03 14:08:36 +08:00
JackChen 99b028dc1d
fix: address Codex review for structured output (#36) (#38)
- Include error feedback user turn in mergedMessages to maintain
  alternating user/assistant roles required by Anthropic API
- Use explicit undefined check instead of ?? for structured merge
  to preserve null as a valid structured output value
2026-04-03 14:08:27 +08:00
JackChen fbc5546fa1
feat: add optional outputSchema (Zod) for structured agent output (#36)
When `outputSchema` is set on AgentConfig, the agent's final text output
is parsed as JSON, validated against the Zod schema, and exposed via
`result.structured`. On validation failure a single retry with error
feedback is attempted automatically.

Closes #29
2026-04-03 13:45:47 +08:00