ceulen 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +317 -12
- package/README.md +87 -91
- package/extensions/lib/advisor-marker.ts +33 -0
- package/extensions/lib/panel.ts +25 -2
- package/extensions/lib/rate.ts +24 -0
- package/extensions/lib/registry.ts +93 -7
- package/extensions/lib/tools.ts +47 -0
- package/extensions/lib/usage-store.ts +33 -0
- package/extensions/modules/advisor/commands/advisor.ts +244 -0
- package/extensions/modules/advisor/configPanel.ts +194 -0
- package/extensions/modules/advisor/index.ts +217 -0
- package/extensions/modules/advisor/lib/config.ts +248 -0
- package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
- package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
- package/extensions/modules/advisor/lib/model-picker.ts +47 -0
- package/extensions/modules/advisor/lib/watcher.ts +286 -0
- package/extensions/modules/advisor/test/compliance.test.ts +125 -0
- package/extensions/modules/advisor/test/config.test.ts +202 -0
- package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
- package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
- package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
- package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
- package/extensions/modules/advisor/test/module.test.ts +586 -0
- package/extensions/modules/advisor/test/watcher.test.ts +478 -0
- package/extensions/modules/classifier/configPanel.ts +104 -0
- package/extensions/modules/classifier/index.ts +297 -0
- package/extensions/modules/classifier/lib/settings.ts +95 -0
- package/extensions/modules/classifier/test/classifier.test.ts +475 -0
- package/extensions/modules/composer/index.ts +152 -3
- package/extensions/modules/composer/lib/footer.ts +119 -0
- package/extensions/modules/composer/lib/shapes.ts +265 -26
- package/extensions/modules/composer/test/editor.test.ts +1 -1
- package/extensions/modules/composer/test/footer.test.ts +96 -0
- package/extensions/modules/composer/test/module.test.ts +113 -11
- package/extensions/modules/composer/test/screen.test.ts +29 -1
- package/extensions/modules/composer/test/shapes.test.ts +224 -8
- package/extensions/modules/config/defaultTools.ts +96 -0
- package/extensions/modules/config/index.ts +147 -7
- package/extensions/modules/config/piSettings.ts +254 -69
- package/extensions/modules/config/test/defaultTools.test.ts +122 -0
- package/extensions/modules/config/test/menu.test.ts +64 -0
- package/extensions/modules/config/test/panel.test.ts +38 -23
- package/extensions/modules/config/test/piSettings.test.ts +130 -3
- package/extensions/modules/config/test/renderTools.test.ts +71 -0
- package/extensions/modules/config/test/tools.test.ts +146 -0
- package/extensions/modules/fff/index.ts +2094 -0
- package/extensions/modules/fff/lib/query.ts +129 -0
- package/extensions/modules/fff/test/index.test.ts +607 -0
- package/extensions/modules/fff/test/query.test.ts +71 -0
- package/extensions/modules/munin/configPanel.ts +105 -0
- package/extensions/modules/munin/index.ts +641 -0
- package/extensions/modules/munin/lib/helpers.ts +471 -0
- package/extensions/modules/munin/lib/retry.ts +20 -0
- package/extensions/modules/munin/lib/sdk.ts +218 -0
- package/extensions/modules/munin/test/config.test.ts +168 -0
- package/extensions/modules/munin/test/configPanel.test.ts +161 -0
- package/extensions/modules/munin/test/extension.test.ts +224 -0
- package/extensions/modules/munin/test/helpers.test.ts +523 -0
- package/extensions/modules/munin/test/retry.test.ts +145 -0
- package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
- package/extensions/modules/plan/configPanel.ts +152 -0
- package/extensions/modules/plan/index.ts +917 -0
- package/extensions/modules/plan/lib/plan-tools.ts +72 -0
- package/extensions/modules/plan/lib/prompt.ts +65 -0
- package/extensions/modules/plan/lib/settings.ts +166 -0
- package/extensions/modules/plan/lib/shell-gate.ts +200 -0
- package/extensions/modules/plan/test/configPanel.test.ts +96 -0
- package/extensions/modules/plan/test/harness.ts +133 -0
- package/extensions/modules/plan/test/module.test.ts +305 -0
- package/extensions/modules/plan/test/settings.test.ts +114 -0
- package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
- package/extensions/modules/ponytail/index.ts +23 -73
- package/extensions/modules/ponytail/lib/config.ts +1 -6
- package/extensions/modules/ponytail/test/extension.test.ts +1 -85
- package/extensions/modules/repair/configPanel.ts +96 -0
- package/extensions/modules/repair/index.ts +414 -0
- package/extensions/modules/repair/lib/auto-bg.ts +333 -0
- package/extensions/modules/repair/lib/edit-repair.ts +155 -0
- package/extensions/modules/repair/lib/editor.ts +251 -0
- package/extensions/modules/repair/lib/guards.ts +34 -0
- package/extensions/modules/repair/lib/input-repair.ts +324 -0
- package/extensions/modules/repair/lib/patch.ts +481 -0
- package/extensions/modules/repair/lib/settings.ts +114 -0
- package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
- package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
- package/extensions/modules/repair/test/editor.test.ts +141 -0
- package/extensions/modules/repair/test/guards.test.ts +64 -0
- package/extensions/modules/repair/test/input-repair.test.ts +231 -0
- package/extensions/modules/repair/test/patch.test.ts +563 -0
- package/extensions/modules/repair/test/settings.test.ts +167 -0
- package/extensions/modules/repair/test/wrapper.test.ts +210 -0
- package/extensions/modules/router/commands/commands.ts +4 -0
- package/extensions/modules/router/lib/client.ts +61 -0
- package/extensions/modules/router/lib/provider.ts +34 -13
- package/extensions/modules/router/lib/systemone.ts +152 -0
- package/extensions/modules/router/test/unit.test.ts +179 -0
- package/extensions/modules/rtk/findFallback.js +68 -0
- package/extensions/modules/rtk/index.ts +248 -0
- package/extensions/modules/rtk/safe-rewrite.js +74 -0
- package/extensions/modules/rtk/test/findFallback.test.js +66 -0
- package/extensions/modules/rtk/test/index.test.js +131 -0
- package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
- package/extensions/modules/rtk/version-gate.js +34 -0
- package/extensions/modules/serena/index.ts +629 -0
- package/extensions/modules/serena/lib/detect.ts +73 -0
- package/extensions/modules/serena/lib/env.ts +76 -0
- package/extensions/modules/serena/lib/guidance.ts +26 -0
- package/extensions/modules/serena/lib/normalize.ts +27 -0
- package/extensions/modules/serena/lib/symbol-key.ts +20 -0
- package/extensions/modules/serena/lib/truncate.ts +20 -0
- package/extensions/modules/serena/test/env.test.ts +122 -0
- package/extensions/modules/serena/test/index.test.ts +347 -0
- package/extensions/modules/serena/test/worker.test.ts +562 -0
- package/extensions/modules/serena/worker.ts +858 -0
- package/extensions/modules/steering/configPanel.ts +122 -0
- package/extensions/modules/steering/index.ts +616 -0
- package/extensions/modules/steering/lib/anchor.ts +175 -0
- package/extensions/modules/steering/lib/errors.ts +253 -0
- package/extensions/modules/steering/lib/family.ts +33 -0
- package/extensions/modules/steering/lib/guidance.ts +172 -0
- package/extensions/modules/steering/lib/history.ts +170 -0
- package/extensions/modules/steering/lib/settings.ts +127 -0
- package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
- package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
- package/extensions/modules/steering/test/anchor.test.ts +160 -0
- package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
- package/extensions/modules/steering/test/errors.test.ts +309 -0
- package/extensions/modules/steering/test/family.test.ts +43 -0
- package/extensions/modules/steering/test/guidance.test.ts +357 -0
- package/extensions/modules/steering/test/harness.ts +93 -0
- package/extensions/modules/steering/test/history.test.ts +118 -0
- package/extensions/modules/steering/test/settings.test.ts +182 -0
- package/extensions/modules/steering/test/status.test.ts +71 -0
- package/extensions/modules/steering/test/toolCall.test.ts +99 -0
- package/extensions/modules/subagent/agents/planner.md +14 -0
- package/extensions/modules/subagent/agents/reviewer.md +42 -0
- package/extensions/modules/subagent/agents/scout.md +47 -0
- package/extensions/modules/subagent/agents/tester.md +12 -0
- package/extensions/modules/subagent/agents/worker.md +18 -0
- package/extensions/modules/subagent/configPanel.ts +219 -0
- package/extensions/modules/subagent/index.ts +2451 -0
- package/extensions/modules/subagent/lib/agents.ts +408 -0
- package/extensions/modules/subagent/lib/background.ts +383 -0
- package/extensions/modules/subagent/lib/herdr.ts +1019 -0
- package/extensions/modules/subagent/lib/history.ts +126 -0
- package/extensions/modules/subagent/lib/model.ts +156 -0
- package/extensions/modules/subagent/lib/render.ts +335 -0
- package/extensions/modules/subagent/lib/result.ts +110 -0
- package/extensions/modules/subagent/lib/roles.ts +296 -0
- package/extensions/modules/subagent/lib/routing.ts +223 -0
- package/extensions/modules/subagent/lib/runner.ts +559 -0
- package/extensions/modules/subagent/lib/schema.ts +21 -0
- package/extensions/modules/subagent/lib/security.ts +752 -0
- package/extensions/modules/subagent/lib/settings.ts +159 -0
- package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
- package/extensions/modules/subagent/lib/threads.ts +135 -0
- package/extensions/modules/subagent/lib/types.ts +18 -0
- package/extensions/modules/subagent/lib/widget.ts +475 -0
- package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
- package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
- package/extensions/modules/subagent/test/routing.test.ts +350 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/usage/index.ts +59 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/ux/UPSTREAM +24 -0
- package/extensions/modules/ux/index.ts +393 -0
- package/extensions/modules/ux/lib/audit.ts +562 -0
- package/extensions/modules/ux/lib/config.ts +136 -0
- package/extensions/modules/ux/lib/instructions.ts +77 -0
- package/extensions/modules/ux/test/audit.test.ts +756 -0
- package/extensions/modules/ux/test/extension.test.ts +394 -0
- package/extensions/modules/ux/test/instructions.test.ts +19 -0
- package/extensions/modules/web/configPanel.ts +193 -0
- package/extensions/modules/web/index.ts +978 -0
- package/extensions/modules/web/lib/agy.ts +209 -0
- package/extensions/modules/web/lib/brave.ts +42 -0
- package/extensions/modules/web/lib/cdp.ts +686 -0
- package/extensions/modules/web/lib/chatapi.ts +101 -0
- package/extensions/modules/web/lib/chatgpt.ts +796 -0
- package/extensions/modules/web/lib/chrome.ts +328 -0
- package/extensions/modules/web/lib/config.ts +136 -0
- package/extensions/modules/web/lib/content.ts +99 -0
- package/extensions/modules/web/lib/crawl4ai.ts +172 -0
- package/extensions/modules/web/lib/extract.ts +235 -0
- package/extensions/modules/web/lib/firecrawl.ts +56 -0
- package/extensions/modules/web/lib/format.ts +197 -0
- package/extensions/modules/web/lib/gemini-auth.ts +286 -0
- package/extensions/modules/web/lib/gemini-dr.ts +338 -0
- package/extensions/modules/web/lib/gemini.ts +551 -0
- package/extensions/modules/web/lib/imageapi.ts +559 -0
- package/extensions/modules/web/lib/retry.ts +71 -0
- package/extensions/modules/web/lib/search.ts +237 -0
- package/extensions/modules/web/lib/searxng.ts +69 -0
- package/extensions/modules/web/lib/settings.ts +148 -0
- package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
- package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
- package/extensions/modules/web/test/agy.test.ts +383 -0
- package/extensions/modules/web/test/cdp.test.ts +717 -0
- package/extensions/modules/web/test/chat.test.ts +171 -0
- package/extensions/modules/web/test/chatgpt.test.ts +633 -0
- package/extensions/modules/web/test/chrome.test.ts +282 -0
- package/extensions/modules/web/test/config.test.ts +218 -0
- package/extensions/modules/web/test/configPanel.test.ts +132 -0
- package/extensions/modules/web/test/crawl.test.ts +134 -0
- package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
- package/extensions/modules/web/test/extension.test.ts +118 -0
- package/extensions/modules/web/test/extract.test.ts +436 -0
- package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
- package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
- package/extensions/modules/web/test/fixtures/interaction.html +31 -0
- package/extensions/modules/web/test/format.test.ts +230 -0
- package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
- package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
- package/extensions/modules/web/test/gemini.test.ts +472 -0
- package/extensions/modules/web/test/hook.test.ts +101 -0
- package/extensions/modules/web/test/image.test.ts +1089 -0
- package/extensions/modules/web/test/retry.test.ts +119 -0
- package/extensions/modules/web/test/screenshot.test.ts +109 -0
- package/extensions/modules/web/test/search.test.ts +210 -0
- package/extensions/modules/web/test/settings.test.ts +165 -0
- package/extensions/modules/web/test/skill.test.ts +72 -0
- package/extensions/modules/web/types.d.ts +3 -0
- package/extensions/modules/web/vendor/package.json +1 -0
- package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
- package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
- package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
- package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
- package/extensions/modules/web/vendor/readability/index.js +9 -0
- package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
- package/extensions/modules/zai/configPanel.ts +151 -0
- package/extensions/modules/zai/index.ts +186 -0
- package/extensions/modules/zai/lib/anthropic.ts +159 -0
- package/extensions/modules/zai/lib/settings.ts +198 -0
- package/extensions/modules/zai/lib/signing.ts +718 -0
- package/extensions/modules/zai/lib/throttle.ts +198 -0
- package/extensions/modules/zai/test/anthropic.test.ts +132 -0
- package/extensions/modules/zai/test/configPanel.test.ts +207 -0
- package/extensions/modules/zai/test/extension.test.ts +249 -0
- package/extensions/modules/zai/test/settings.test.ts +183 -0
- package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
- package/extensions/modules/zai/test/signing.test.ts +345 -0
- package/extensions/modules/zai/test/throttle.test.ts +329 -0
- package/package.json +19 -4
- package/scripts/validate-themes.mjs +75 -0
- package/skills/munin/SKILL.md +117 -0
- package/skills/ux-capture/SKILL.md +202 -0
- package/skills/ux-design/SKILL.md +279 -0
- package/skills/ux-presets/SKILL.md +484 -0
- package/skills/ux-routing/SKILL.md +46 -0
- package/skills/web/SKILL.md +153 -0
- package/themes/alabaster.json +93 -0
- package/themes/amethyst.json +96 -0
- package/themes/anthracite.json +93 -0
- package/themes/basalt.json +91 -0
- package/themes/birch.json +95 -0
- package/themes/catppuccin-mocha.json +91 -0
- package/themes/dark-abyss.json +91 -0
- package/themes/dark-arctic.json +104 -0
- package/themes/dark-aurora.json +95 -0
- package/themes/dark-catppuccin.json +107 -0
- package/themes/dark-cavern.json +91 -0
- package/themes/dark-celestial.json +106 -0
- package/themes/dark-copper.json +95 -0
- package/themes/dark-cosmos.json +90 -0
- package/themes/dark-cyberpunk.json +102 -0
- package/themes/dark-dracula.json +98 -0
- package/themes/dark-eclipse.json +91 -0
- package/themes/dark-ember.json +95 -0
- package/themes/dark-equinox.json +90 -0
- package/themes/dark-forest.json +96 -0
- package/themes/dark-github.json +105 -0
- package/themes/dark-gruvbox.json +112 -0
- package/themes/dark-lavender.json +95 -0
- package/themes/dark-lunar.json +89 -0
- package/themes/dark-midnight.json +95 -0
- package/themes/dark-monochrome.json +94 -0
- package/themes/dark-monokai.json +98 -0
- package/themes/dark-nebula.json +90 -0
- package/themes/dark-neon-noir.json +100 -0
- package/themes/dark-nord.json +98 -0
- package/themes/dark-ocean.json +101 -0
- package/themes/dark-one.json +100 -0
- package/themes/dark-poimandres.json +115 -0
- package/themes/dark-rainforest.json +91 -0
- package/themes/dark-reef.json +91 -0
- package/themes/dark-retro.json +92 -0
- package/themes/dark-rose-pine.json +96 -0
- package/themes/dark-sakura.json +95 -0
- package/themes/dark-slate.json +95 -0
- package/themes/dark-solarized.json +97 -0
- package/themes/dark-solstice.json +90 -0
- package/themes/dark-starfall.json +91 -0
- package/themes/dark-sunset.json +99 -0
- package/themes/dark-swamp.json +90 -0
- package/themes/dark-synthwave.json +103 -0
- package/themes/dark-taiga.json +91 -0
- package/themes/dark-terminal.json +95 -0
- package/themes/dark-tokyo-night.json +101 -0
- package/themes/dark-tundra.json +91 -0
- package/themes/dark-twilight.json +91 -0
- package/themes/dark-volcanic.json +91 -0
- package/themes/dark.json +80 -0
- package/themes/graphite.json +92 -0
- package/themes/light-arctic.json +107 -0
- package/themes/light-aurora-day.json +91 -0
- package/themes/light-canyon.json +91 -0
- package/themes/light-catppuccin.json +106 -0
- package/themes/light-cirrus.json +90 -0
- package/themes/light-coral.json +95 -0
- package/themes/light-cyberpunk.json +96 -0
- package/themes/light-dawn.json +90 -0
- package/themes/light-dunes.json +91 -0
- package/themes/light-eucalyptus.json +95 -0
- package/themes/light-forest.json +100 -0
- package/themes/light-frost.json +95 -0
- package/themes/light-github.json +115 -0
- package/themes/light-glacier.json +91 -0
- package/themes/light-gruvbox.json +108 -0
- package/themes/light-haze.json +90 -0
- package/themes/light-honeycomb.json +95 -0
- package/themes/light-lagoon.json +91 -0
- package/themes/light-lavender.json +95 -0
- package/themes/light-meadow.json +91 -0
- package/themes/light-mint.json +95 -0
- package/themes/light-monochrome.json +101 -0
- package/themes/light-ocean.json +99 -0
- package/themes/light-one.json +99 -0
- package/themes/light-opal.json +91 -0
- package/themes/light-orchard.json +91 -0
- package/themes/light-paper.json +95 -0
- package/themes/light-poimandres.json +115 -0
- package/themes/light-prism.json +90 -0
- package/themes/light-retro.json +98 -0
- package/themes/light-sand.json +95 -0
- package/themes/light-savanna.json +91 -0
- package/themes/light-solarized.json +102 -0
- package/themes/light-soleil.json +90 -0
- package/themes/light-sunset.json +99 -0
- package/themes/light-synthwave.json +98 -0
- package/themes/light-tokyo-night.json +111 -0
- package/themes/light-wetland.json +91 -0
- package/themes/light-zenith.json +89 -0
- package/themes/light.json +80 -0
- package/themes/limestone.json +94 -0
- package/themes/mahogany.json +97 -0
- package/themes/marble.json +93 -0
- package/themes/mirage.json +79 -0
- package/themes/obsidian.json +91 -0
- package/themes/onyx.json +91 -0
- package/themes/pearl.json +93 -0
- package/themes/porcelain.json +91 -0
- package/themes/quartz.json +96 -0
- package/themes/sandstone.json +95 -0
- package/themes/titanium.json +90 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* throttle.ts — cross-process dispatch-rate gate for the zai-anthropic provider.
|
|
3
|
+
*
|
|
4
|
+
* Z.ai's coding-plan endpoint enforces a per-key REQUEST-rate limit: HTTP 429
|
|
5
|
+
* `{"error":{"code":"1302","message":"[1302][Rate limit reached for requests]"}}`
|
|
6
|
+
* with no retry-after in the body. Multi-agent setups (herdr panes, parallel
|
|
7
|
+
* subagents, advisor) burst dispatches on one key and trip it. This gate spaces
|
|
8
|
+
* DISPATCH STARTS across every Pi process on the machine; streams themselves are
|
|
9
|
+
* never serialized — the mutex below is held only for the claim procedure and is
|
|
10
|
+
* always released before the hook returns, never during the request.
|
|
11
|
+
*
|
|
12
|
+
* Two files under the Pi agent dir:
|
|
13
|
+
* zai-anthropic-dispatch.json shared state: { ts } = last claimed dispatch slot
|
|
14
|
+
* .zai-anthropic-dispatch.lock short-lived mutex (exclusive-create, content = pid)
|
|
15
|
+
*
|
|
16
|
+
* Lock protocol invariants (review-hardened):
|
|
17
|
+
* - No loop path skips the deadline check / poll sleep (unremovable stale lock
|
|
18
|
+
* cannot spin forever — it throws after the deadline and dispatch fail-opens).
|
|
19
|
+
* - A single mutex hold is capped under LOCK_STALE_MS (long waits nap, release,
|
|
20
|
+
* and re-queue), so a legitimate sleeper is never stale-broken mid-claim.
|
|
21
|
+
* - release() deletes the lock only if it still contains OUR pid (a
|
|
22
|
+
* stale-breaker may have replaced it meanwhile).
|
|
23
|
+
* - The state dir is created BEFORE the lock is claimed (fresh agent dir would
|
|
24
|
+
* otherwise ENOENT the lock open and permanently silent-fail-open).
|
|
25
|
+
* - Stale-break stat→unlink is not atomic: two simultaneous breakers can
|
|
26
|
+
* over-delete; bounded to under-throttling, self-heals next claim.
|
|
27
|
+
*
|
|
28
|
+
* ponytail: single-machine scope — machines sharing the key (a2a) can't share
|
|
29
|
+
* the lockfile; provider-level retries (retry.provider.maxRetries) cover that.
|
|
30
|
+
* ponytail: retry attempts inside retryProviderRequest bypass this gate — they
|
|
31
|
+
* carry their own exponential backoff.
|
|
32
|
+
*
|
|
33
|
+
* ponytail: vendored from @bacnh85/pi-model-tools 0.9.5 (extensions/lib/zai-throttle.ts).
|
|
34
|
+
* Local changes: (1) `defaultThrottlePaths` resolves the agent dir through
|
|
35
|
+
* ceulen's `agentDirs()` (honors PI_CODING_AGENT_DIR) instead of a hardcoded
|
|
36
|
+
* ~/.pi/agent; (2) `throttleZaiDispatch` accepts a settings-resolved
|
|
37
|
+
* `intervalMs` override — the `zai.minIntervalMs` settings key wins over the
|
|
38
|
+
* ZAI_ANTHROPIC_MIN_INTERVAL_MS env default.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import { mkdir, open, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
42
|
+
import { dirname, join } from "node:path";
|
|
43
|
+
import { agentDirs } from "../../../lib/registry.js";
|
|
44
|
+
import { isZaiAnthropicProvider } from "./anthropic.js";
|
|
45
|
+
|
|
46
|
+
export const MIN_INTERVAL_ENV = "ZAI_ANTHROPIC_MIN_INTERVAL_MS";
|
|
47
|
+
/** ≤1 dispatch/sec/key machine-wide. Single-agent requests are naturally spaced wider. */
|
|
48
|
+
export const DEFAULT_MIN_INTERVAL_MS = 1000;
|
|
49
|
+
|
|
50
|
+
/** A crash mid-claim must not wedge every future dispatch — break locks this old. */
|
|
51
|
+
const LOCK_STALE_MS = 30_000;
|
|
52
|
+
const LOCK_POLL_MS = 40;
|
|
53
|
+
/** Lock-acquire deadline: fixed 60s default, injectable for tests/exotic configs. Deep-but-legit
|
|
54
|
+
* queues normally resolve within it because holds nap ≤15s and re-queue — anything past it fails
|
|
55
|
+
* open by design (logged via onError). */
|
|
56
|
+
const LOCK_DEADLINE_MS = 60_000;
|
|
57
|
+
/** Cap on a single mutex hold — strictly under LOCK_STALE_MS so a napping claimant
|
|
58
|
+
* is never stale-broken by the next claimant. */
|
|
59
|
+
const MAX_HOLD_MS = LOCK_STALE_MS / 2;
|
|
60
|
+
|
|
61
|
+
const defaultSleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
62
|
+
|
|
63
|
+
/** Default 1000; explicit 0 disables; garbage falls back to default (zaiAnthropicSpeed pattern). */
|
|
64
|
+
export function zaiThrottleIntervalMs(env: Record<string, string | undefined> = process.env): number {
|
|
65
|
+
const raw = env[MIN_INTERVAL_ENV]?.trim() ?? "";
|
|
66
|
+
if (raw === "") return DEFAULT_MIN_INTERVAL_MS;
|
|
67
|
+
const parsed = Number(raw);
|
|
68
|
+
return Number.isFinite(parsed) && parsed >= 0 ? Math.floor(parsed) : DEFAULT_MIN_INTERVAL_MS;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ThrottlePaths {
|
|
72
|
+
statePath: string;
|
|
73
|
+
lockPath: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Agent dir is resolved at CALL time through `agentDirs()` so a test's
|
|
77
|
+
* PI_CODING_AGENT_DIR (and a user's multi-dir setup) is honored. */
|
|
78
|
+
export function defaultThrottlePaths(agentDir: string = agentDirs()[0]!): ThrottlePaths {
|
|
79
|
+
return {
|
|
80
|
+
statePath: join(agentDir, "zai-anthropic-dispatch.json"),
|
|
81
|
+
lockPath: join(agentDir, ".zai-anthropic-dispatch.lock"),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Exclusive-create mutex; stale locks (crash mid-claim) are broken after LOCK_STALE_MS.
|
|
86
|
+
* Every retry path honors the deadline + poll sleep — none can spin. */
|
|
87
|
+
export async function acquireLock(
|
|
88
|
+
lockPath: string,
|
|
89
|
+
now: () => number,
|
|
90
|
+
sleep: (ms: number) => Promise<void>,
|
|
91
|
+
pollMs = LOCK_POLL_MS,
|
|
92
|
+
deadlineMs = LOCK_DEADLINE_MS,
|
|
93
|
+
): Promise<{ release(): Promise<void> }> {
|
|
94
|
+
const deadline = now() + deadlineMs;
|
|
95
|
+
for (;;) {
|
|
96
|
+
let fh: Awaited<ReturnType<typeof open>> | undefined;
|
|
97
|
+
try {
|
|
98
|
+
fh = await open(lockPath, "wx");
|
|
99
|
+
await fh.write(`${process.pid}`);
|
|
100
|
+
await fh.close();
|
|
101
|
+
const myPid = `${process.pid}`;
|
|
102
|
+
return {
|
|
103
|
+
// Delete only OUR lock: a stale-breaker may have replaced it while we held it.
|
|
104
|
+
release: async () => {
|
|
105
|
+
try {
|
|
106
|
+
if ((await readFile(lockPath, "utf-8")) !== myPid) return;
|
|
107
|
+
} catch { return; /* gone already */ }
|
|
108
|
+
await rm(lockPath, { force: true }).catch(() => {});
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
} catch (err) {
|
|
112
|
+
if (fh) { try { await fh.close(); } catch { /* already closed */ } await unlink(lockPath).catch(() => {}); }
|
|
113
|
+
if ((err as NodeJS.ErrnoException)?.code !== "EEXIST") throw err;
|
|
114
|
+
}
|
|
115
|
+
// Lock exists: try to break a crashed holder's stale lock, then ALWAYS
|
|
116
|
+
// honor the deadline + poll sleep before retrying (no hot loops).
|
|
117
|
+
try {
|
|
118
|
+
const st = await stat(lockPath);
|
|
119
|
+
if (now() - st.mtimeMs > LOCK_STALE_MS) await unlink(lockPath).catch(() => {});
|
|
120
|
+
} catch { /* lock vanished — plain retry */ }
|
|
121
|
+
if (now() > deadline) throw new Error(`zai-anthropic throttle: lock busy >${Math.round(deadlineMs / 1000)}s (${lockPath})`);
|
|
122
|
+
await sleep(pollMs);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Claim the next dispatch slot: at least `intervalMs` after the last claimed one,
|
|
128
|
+
* across all processes sharing `paths`. Returns ms actually slept (0 = dispatched
|
|
129
|
+
* immediately). Fail-open: any internal error dispatches unthrottled (reported via
|
|
130
|
+
* `onError`) — a rate limiter must never break a request.
|
|
131
|
+
*/
|
|
132
|
+
export async function throttleDispatch(opts: {
|
|
133
|
+
intervalMs: number;
|
|
134
|
+
statePath: string;
|
|
135
|
+
lockPath: string;
|
|
136
|
+
now?: () => number;
|
|
137
|
+
sleep?: (ms: number) => Promise<void>;
|
|
138
|
+
pollMs?: number;
|
|
139
|
+
lockDeadlineMs?: number;
|
|
140
|
+
onError?: (err: Error) => void;
|
|
141
|
+
}): Promise<number> {
|
|
142
|
+
const { intervalMs, statePath, lockPath } = opts;
|
|
143
|
+
if (intervalMs <= 0) return 0;
|
|
144
|
+
const now = opts.now ?? Date.now;
|
|
145
|
+
const sleep = opts.sleep ?? defaultSleep;
|
|
146
|
+
let sleptMs = 0;
|
|
147
|
+
try {
|
|
148
|
+
// Before the lock: on a fresh agent dir the lock open would ENOENT (≠EEXIST)
|
|
149
|
+
// and fail-open forever before the state mkdir below ever ran.
|
|
150
|
+
await mkdir(dirname(statePath), { recursive: true });
|
|
151
|
+
for (;;) {
|
|
152
|
+
const lock = await acquireLock(lockPath, now, sleep, opts.pollMs, opts.lockDeadlineMs);
|
|
153
|
+
let done = false;
|
|
154
|
+
try {
|
|
155
|
+
let lastTs = 0;
|
|
156
|
+
try {
|
|
157
|
+
const raw = (JSON.parse(await readFile(statePath, "utf-8")) as { ts?: unknown }).ts;
|
|
158
|
+
lastTs = typeof raw === "number" && Number.isFinite(raw) ? raw : 0; // wrong-typed ts = corrupt → dispatch now
|
|
159
|
+
} catch { /* first run or corrupt state → dispatch now */ }
|
|
160
|
+
if (lastTs > now() + LOCK_STALE_MS) lastTs = 0; // future/corrupt ts (clock step-back) → ignore
|
|
161
|
+
const wait = lastTs + intervalMs - now();
|
|
162
|
+
if (wait <= 0) {
|
|
163
|
+
await writeFile(statePath, JSON.stringify({ ts: now(), pid: process.pid }));
|
|
164
|
+
done = true;
|
|
165
|
+
} else {
|
|
166
|
+
// Hold the mutex only for a bounded nap; longer waits release and
|
|
167
|
+
// re-queue so this holder is never mistaken for a crashed one.
|
|
168
|
+
const nap = Math.min(wait, MAX_HOLD_MS);
|
|
169
|
+
await sleep(nap);
|
|
170
|
+
sleptMs += nap;
|
|
171
|
+
}
|
|
172
|
+
} finally {
|
|
173
|
+
await lock.release();
|
|
174
|
+
}
|
|
175
|
+
if (done) return sleptMs;
|
|
176
|
+
// Re-acquire and re-evaluate: state may have advanced while we slept/released.
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
opts.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
180
|
+
return 0; // fail-open
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Wiring entry: gate + interval + default paths in one call (index.ts before_provider_headers).
|
|
185
|
+
* `hooks.intervalMs` (the settings-resolved `zai.minIntervalMs`) wins over the env default. */
|
|
186
|
+
export async function throttleZaiDispatch(
|
|
187
|
+
provider: string | undefined,
|
|
188
|
+
env: Record<string, string | undefined> = process.env,
|
|
189
|
+
paths: ThrottlePaths = defaultThrottlePaths(),
|
|
190
|
+
hooks?: { onError?: (err: Error) => void; intervalMs?: number },
|
|
191
|
+
): Promise<number> {
|
|
192
|
+
if (!isZaiAnthropicProvider(provider)) return 0;
|
|
193
|
+
return throttleDispatch({
|
|
194
|
+
intervalMs: hooks?.intervalMs ?? zaiThrottleIntervalMs(env),
|
|
195
|
+
...paths,
|
|
196
|
+
onError: hooks?.onError,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Ported from pi-model-tools extensions/test/unit/zai-anthropic.test.ts.
|
|
2
|
+
// Dropped: the detectFamily assertion — model-family detection is
|
|
3
|
+
// pi-model-tools' concern; ceulen gates on the PROVIDER id instead.
|
|
4
|
+
// Added: the settings-resolved baseUrl argument on registration.
|
|
5
|
+
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { describe, it } from "node:test";
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_BASE_URL,
|
|
10
|
+
FAST_MODE_BETA,
|
|
11
|
+
PROVIDER_ID,
|
|
12
|
+
applyFastModeBody,
|
|
13
|
+
applyFastModeHeaders,
|
|
14
|
+
isZaiAnthropicProvider,
|
|
15
|
+
registerZaiAnthropicProvider,
|
|
16
|
+
zaiAnthropicBaseUrl,
|
|
17
|
+
zaiAnthropicModels,
|
|
18
|
+
zaiAnthropicSpeed,
|
|
19
|
+
} from "../lib/anthropic.js";
|
|
20
|
+
|
|
21
|
+
describe("zai-anthropic provider", () => {
|
|
22
|
+
it("baseUrl: default, env override, trailing slash trimmed", () => {
|
|
23
|
+
assert.equal(zaiAnthropicBaseUrl({}), DEFAULT_BASE_URL);
|
|
24
|
+
assert.equal(
|
|
25
|
+
zaiAnthropicBaseUrl({ ZAI_ANTHROPIC_BASE_URL: "https://open.bigmodel.cn/api/anthropic/" }),
|
|
26
|
+
"https://open.bigmodel.cn/api/anthropic",
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("speed: fast default, standard via env, garbage falls back to fast", () => {
|
|
31
|
+
assert.equal(zaiAnthropicSpeed({}), "fast");
|
|
32
|
+
assert.equal(zaiAnthropicSpeed({ ZAI_ANTHROPIC_SPEED: "standard" }), "standard");
|
|
33
|
+
assert.equal(zaiAnthropicSpeed({ ZAI_ANTHROPIC_SPEED: "whatever" }), "fast");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("provider match is case-insensitive, and only for zai-anthropic", () => {
|
|
37
|
+
assert.equal(isZaiAnthropicProvider("zai-anthropic"), true);
|
|
38
|
+
assert.equal(isZaiAnthropicProvider("ZAI-Anthropic"), true);
|
|
39
|
+
assert.equal(isZaiAnthropicProvider("zai"), false);
|
|
40
|
+
assert.equal(isZaiAnthropicProvider(undefined), false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("models: anthropic-messages api, forceAdaptiveThinking, zcode effort variants", () => {
|
|
44
|
+
const models = zaiAnthropicModels();
|
|
45
|
+
assert.equal(models.length, 4);
|
|
46
|
+
const flashx = models.find((m) => m.id === "glm-5.3-flashx")!;
|
|
47
|
+
assert.deepEqual(flashx.input, ["text", "image"], "flashx mirrors the flash family shape");
|
|
48
|
+
const glm53 = models.find((m) => m.id === "glm-5.3")!;
|
|
49
|
+
assert.equal(glm53.api, "anthropic-messages");
|
|
50
|
+
assert.equal(glm53.compat?.forceAdaptiveThinking, true);
|
|
51
|
+
assert.equal(glm53.thinkingLevelMap?.max, "max");
|
|
52
|
+
assert.equal(glm53.thinkingLevelMap?.low, "low");
|
|
53
|
+
assert.equal(glm53.thinkingLevelMap?.high, "high");
|
|
54
|
+
assert.equal(glm53.thinkingLevelMap?.medium, "high");
|
|
55
|
+
const flash = models.find((m) => m.id === "glm-5.3-flash")!;
|
|
56
|
+
assert.deepEqual(flash.input, ["text", "image"]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("registration is UNCONDITIONAL — env gating hides provider from /model and /login (regression)", () => {
|
|
60
|
+
const registered: Array<{ name: string; config: any }> = [];
|
|
61
|
+
const pi = { registerProvider: (name: string, config: any) => registered.push({ name, config }) } as any;
|
|
62
|
+
|
|
63
|
+
// No key in env — must STILL register; /login (auth.json) supplies the key.
|
|
64
|
+
const hadKey = process.env.ZAI_ANTHROPIC_API_KEY;
|
|
65
|
+
delete process.env.ZAI_ANTHROPIC_API_KEY;
|
|
66
|
+
registerZaiAnthropicProvider(pi);
|
|
67
|
+
if (hadKey !== undefined) process.env.ZAI_ANTHROPIC_API_KEY = hadKey;
|
|
68
|
+
|
|
69
|
+
assert.equal(registered.length, 1);
|
|
70
|
+
assert.equal(registered[0]!.name, PROVIDER_ID);
|
|
71
|
+
assert.equal(registered[0]!.config.api, "anthropic-messages");
|
|
72
|
+
assert.equal(registered[0]!.config.apiKey, "$ZAI_ANTHROPIC_API_KEY");
|
|
73
|
+
assert.equal(registered[0]!.config.baseUrl, DEFAULT_BASE_URL);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("registration takes an explicit (settings-resolved) baseUrl", () => {
|
|
77
|
+
const registered: Array<{ name: string; config: any }> = [];
|
|
78
|
+
const pi = { registerProvider: (name: string, config: any) => registered.push({ name, config }) } as any;
|
|
79
|
+
registerZaiAnthropicProvider(pi, "https://zcode.z.ai/api/v1/ultra-zai/anthropic");
|
|
80
|
+
assert.equal(registered[0]!.config.baseUrl, "https://zcode.z.ai/api/v1/ultra-zai/anthropic");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("registration skips silently on a fake pi without registerProvider (test harnesses)", () => {
|
|
84
|
+
registerZaiAnthropicProvider({} as any); // must not throw
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("fast-mode headers: merge into existing beta list, no-op for other providers or standard", () => {
|
|
88
|
+
const headers: Record<string, string | null | undefined> = { "anthropic-beta": "fine-grained-tool-streaming-2025-05-14" };
|
|
89
|
+
applyFastModeHeaders(headers, { provider: PROVIDER_ID, speed: "fast" });
|
|
90
|
+
assert.equal(headers["anthropic-beta"], `fine-grained-tool-streaming-2025-05-14,${FAST_MODE_BETA}`);
|
|
91
|
+
|
|
92
|
+
const fresh: Record<string, string | null | undefined> = {};
|
|
93
|
+
applyFastModeHeaders(fresh, { provider: "openai", speed: "fast" });
|
|
94
|
+
assert.equal(fresh["anthropic-beta"], undefined);
|
|
95
|
+
applyFastModeHeaders(fresh, { provider: PROVIDER_ID, speed: "standard" });
|
|
96
|
+
assert.equal(fresh["anthropic-beta"], undefined);
|
|
97
|
+
|
|
98
|
+
const empty: Record<string, string | null | undefined> = {};
|
|
99
|
+
applyFastModeHeaders(empty, { provider: PROVIDER_ID, speed: "fast" });
|
|
100
|
+
assert.equal(empty["anthropic-beta"], FAST_MODE_BETA);
|
|
101
|
+
|
|
102
|
+
// provider-id casing is normalized (reviewer finding: exact match silently skipped)
|
|
103
|
+
const cased: Record<string, string | null | undefined> = {};
|
|
104
|
+
applyFastModeHeaders(cased, { provider: "ZAI-Anthropic", speed: "fast" });
|
|
105
|
+
assert.equal(cased["anthropic-beta"], FAST_MODE_BETA);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("fast-mode body: adds top-level speed field, no-op for other providers or standard", () => {
|
|
109
|
+
const payload = { model: "glm-5.3", messages: [{ role: "user", content: "hi" }], thinking: { type: "adaptive" } };
|
|
110
|
+
const out = applyFastModeBody(payload, { provider: PROVIDER_ID, speed: "fast" }) as any;
|
|
111
|
+
assert.equal(out.speed, "fast");
|
|
112
|
+
assert.equal(out.model, "glm-5.3");
|
|
113
|
+
assert.deepEqual(payload, { model: "glm-5.3", messages: [{ role: "user", content: "hi" }], thinking: { type: "adaptive" } }); // original untouched
|
|
114
|
+
|
|
115
|
+
assert.equal(applyFastModeBody(payload, { provider: "router", speed: "fast" }), payload);
|
|
116
|
+
assert.equal(applyFastModeBody(payload, { provider: PROVIDER_ID, speed: "standard" }), payload);
|
|
117
|
+
|
|
118
|
+
// casing normalized
|
|
119
|
+
const cased = applyFastModeBody(payload, { provider: "ZAI-ANTHROPIC", speed: "fast" }) as any;
|
|
120
|
+
assert.equal(cased.speed, "fast");
|
|
121
|
+
|
|
122
|
+
// shallow-copy contract: nested objects are shared with the original
|
|
123
|
+
const nested = { messages: [{ role: "user" }] };
|
|
124
|
+
const outNested = applyFastModeBody(nested, { provider: PROVIDER_ID, speed: "fast" }) as any;
|
|
125
|
+
assert.equal(outNested.speed, "fast");
|
|
126
|
+
assert.equal(outNested.messages, nested.messages); // same reference — documented contract
|
|
127
|
+
|
|
128
|
+
// non-object payloads pass through untouched
|
|
129
|
+
assert.equal(applyFastModeBody(null, { provider: PROVIDER_ID, speed: "fast" }), null);
|
|
130
|
+
assert.equal(applyFastModeBody([1], { provider: PROVIDER_ID, speed: "fast" }) instanceof Array, true);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// NEW for the ceulen port — the /config contribution: row shape/defaults, the
|
|
2
|
+
// global settings.json save path, and the live provider re-register on a
|
|
3
|
+
// baseUrl change (speed/signing/throttle are read per request).
|
|
4
|
+
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
10
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import zaiModule from "../index.js";
|
|
12
|
+
import { zaiConfig, buildZaiGroups } from "../configPanel.js";
|
|
13
|
+
import { DEFAULT_BASE_URL, KNOWN_BASE_URLS } from "../lib/anthropic.js";
|
|
14
|
+
|
|
15
|
+
const ENV_KEYS = ["ZAI_ANTHROPIC_API_KEY", "ZAI_ANTHROPIC_BASE_URL", "PI_CODING_AGENT_DIR"] as const;
|
|
16
|
+
|
|
17
|
+
let dirs: string[] = [];
|
|
18
|
+
let saved: Record<string, string | undefined> = {};
|
|
19
|
+
|
|
20
|
+
function tempDir(prefix: string): string {
|
|
21
|
+
const dir = mkdtempSync(join(tmpdir(), prefix));
|
|
22
|
+
dirs.push(dir);
|
|
23
|
+
return dir;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
dirs = [];
|
|
28
|
+
saved = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
|
|
29
|
+
for (const k of ENV_KEYS) delete process.env[k];
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
for (const k of ENV_KEYS) {
|
|
34
|
+
if (saved[k] === undefined) delete process.env[k];
|
|
35
|
+
else process.env[k] = saved[k]!;
|
|
36
|
+
}
|
|
37
|
+
for (const dir of dirs) rmSync(dir, { recursive: true, force: true });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
function fakePi() {
|
|
41
|
+
const providers: Array<{ name: string; config: any }> = [];
|
|
42
|
+
const pi = {
|
|
43
|
+
registerProvider: (name: string, config: any) => providers.push({ name, config }),
|
|
44
|
+
on: () => () => {},
|
|
45
|
+
registerCommand: () => {},
|
|
46
|
+
} as unknown as ExtensionAPI;
|
|
47
|
+
return { pi, providers };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function saveCtx(cwd: string, trusted = true) {
|
|
51
|
+
const notifications: Array<{ message: string; level?: string }> = [];
|
|
52
|
+
const ctx = {
|
|
53
|
+
cwd,
|
|
54
|
+
model: undefined,
|
|
55
|
+
isProjectTrusted: () => trusted,
|
|
56
|
+
ui: { notify: (message: string, level?: string) => notifications.push({ message, level }) },
|
|
57
|
+
} as unknown as ExtensionContext;
|
|
58
|
+
return { ctx, notifications };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("buildZaiGroups", () => {
|
|
62
|
+
const cfg = { baseUrl: DEFAULT_BASE_URL, speed: "fast" as const, signing: true, minIntervalMs: 1000 };
|
|
63
|
+
|
|
64
|
+
it("renders one Providers section with the four owned rows and their defaults", () => {
|
|
65
|
+
const groups = buildZaiGroups({ ...cfg });
|
|
66
|
+
assert.equal(groups.length, 1);
|
|
67
|
+
assert.equal(groups[0]!.tab, "Providers");
|
|
68
|
+
assert.equal(groups[0]!.label, "Z.AI Coding Plan");
|
|
69
|
+
assert.deepEqual(groups[0]!.rows.map((r) => r.key), [
|
|
70
|
+
"zai.baseUrl", "zai.speed", "zai.signing", "zai.minIntervalMs",
|
|
71
|
+
]);
|
|
72
|
+
const [baseUrl, speed, signing, gate] = groups[0]!.rows;
|
|
73
|
+
assert.equal(baseUrl!.kind, "string");
|
|
74
|
+
assert.equal(baseUrl!.defaultValue, DEFAULT_BASE_URL);
|
|
75
|
+
assert.match(baseUrl!.description ?? "", /ZAI_ANTHROPIC_BASE_URL/);
|
|
76
|
+
assert.equal(speed!.values?.join(","), "fast,standard");
|
|
77
|
+
assert.equal(speed!.defaultValue, "fast");
|
|
78
|
+
assert.equal(signing!.kind, "toggle");
|
|
79
|
+
assert.equal(signing!.defaultValue, true);
|
|
80
|
+
assert.match(signing!.warning ?? "", /fails open to unsigned/);
|
|
81
|
+
assert.equal(gate!.kind, "number");
|
|
82
|
+
assert.equal(gate!.defaultValue, 1000);
|
|
83
|
+
assert.match(gate!.description ?? "", /0 disables/);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("baseUrl menu offers exactly the known endpoints, each described", () => {
|
|
87
|
+
const rows = buildZaiGroups({ ...cfg })[0]!.rows;
|
|
88
|
+
const options = rows[0]!.menu!();
|
|
89
|
+
assert.deepEqual(options.map((o) => o.value), [...KNOWN_BASE_URLS]);
|
|
90
|
+
assert.ok(options.every((o) => (o.description ?? "").length > 0));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("row setters mutate the working copy (baseUrl trimmed, gate numeric)", () => {
|
|
94
|
+
const working = { ...cfg };
|
|
95
|
+
const rows = buildZaiGroups(working)[0]!.rows;
|
|
96
|
+
rows[0]!.set(" https://open.bigmodel.cn/api/anthropic/ ");
|
|
97
|
+
rows[1]!.set("standard");
|
|
98
|
+
rows[2]!.set(false);
|
|
99
|
+
rows[3]!.set("250");
|
|
100
|
+
assert.equal(working.baseUrl, "https://open.bigmodel.cn/api/anthropic/");
|
|
101
|
+
assert.equal(working.speed, "standard");
|
|
102
|
+
assert.equal(working.signing, false);
|
|
103
|
+
assert.equal(working.minIntervalMs, 250);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("zaiConfig save", () => {
|
|
108
|
+
it("no-ops unless an owned key was edited", async () => {
|
|
109
|
+
const agentDir = tempDir("zai-cfg-agent-");
|
|
110
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
111
|
+
const cwd = tempDir("zai-cfg-cwd-");
|
|
112
|
+
const { pi } = fakePi();
|
|
113
|
+
const { ctx, notifications } = saveCtx(cwd);
|
|
114
|
+
const cfg = zaiConfig(pi);
|
|
115
|
+
|
|
116
|
+
await cfg.save(new Set(["router.baseUrl", "ceulen.disabled"]), ctx);
|
|
117
|
+
assert.equal(notifications.length, 0);
|
|
118
|
+
assert.equal(existsSync(join(agentDir, "settings.json")), false, "no file written for foreign keys");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("writes the changed fields into the GLOBAL settings.json and re-registers on a baseUrl change", async () => {
|
|
122
|
+
const agentDir = tempDir("zai-cfg-agent-");
|
|
123
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
124
|
+
const cwd = tempDir("zai-cfg-cwd-");
|
|
125
|
+
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
|
126
|
+
writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ ceulen: { disabled: ["fff"] }, zai: { speed: "fast" } }));
|
|
127
|
+
|
|
128
|
+
const { pi, providers } = fakePi();
|
|
129
|
+
zaiModule(pi); // load-time registration, like the real bundle
|
|
130
|
+
assert.equal(providers.length, 1);
|
|
131
|
+
|
|
132
|
+
const cfg = zaiConfig(pi);
|
|
133
|
+
const rows = cfg.groups()[0]!.rows;
|
|
134
|
+
rows.find((r) => r.key === "zai.baseUrl")!.set("https://zcode.z.ai/api/v1/ultra-zai/anthropic");
|
|
135
|
+
rows.find((r) => r.key === "zai.signing")!.set(false);
|
|
136
|
+
rows.find((r) => r.key === "zai.minIntervalMs")!.set(0);
|
|
137
|
+
|
|
138
|
+
const { ctx, notifications } = saveCtx(cwd);
|
|
139
|
+
await cfg.save(new Set(["zai.baseUrl", "zai.signing", "zai.minIntervalMs"]), ctx);
|
|
140
|
+
|
|
141
|
+
const written = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8"));
|
|
142
|
+
assert.deepEqual(written.ceulen, { disabled: ["fff"] }, "other sections preserved");
|
|
143
|
+
assert.equal(written.zai.baseUrl, "https://zcode.z.ai/api/v1/ultra-zai/anthropic");
|
|
144
|
+
assert.equal(written.zai.signing, false);
|
|
145
|
+
assert.equal(written.zai.minIntervalMs, 0);
|
|
146
|
+
assert.equal(written.zai.speed, "fast", "untouched field left exactly as it was");
|
|
147
|
+
|
|
148
|
+
assert.equal(providers.length, 2, "baseUrl change live-applies via one extra registration");
|
|
149
|
+
assert.equal(providers[1]!.name, "zai-anthropic");
|
|
150
|
+
assert.equal(providers[1]!.config.baseUrl, "https://zcode.z.ai/api/v1/ultra-zai/anthropic");
|
|
151
|
+
assert.match(notifications[0]!.message, /Z\.AI config saved to/);
|
|
152
|
+
assert.equal(notifications[0]!.level, "info");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("does not re-register when only per-request fields changed", async () => {
|
|
156
|
+
const agentDir = tempDir("zai-cfg-agent-");
|
|
157
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
158
|
+
const cwd = tempDir("zai-cfg-cwd-");
|
|
159
|
+
const { pi, providers } = fakePi();
|
|
160
|
+
zaiModule(pi);
|
|
161
|
+
|
|
162
|
+
const cfg = zaiConfig(pi);
|
|
163
|
+
cfg.groups()[0]!.rows.find((r) => r.key === "zai.speed")!.set("standard");
|
|
164
|
+
const { ctx } = saveCtx(cwd);
|
|
165
|
+
await cfg.save(new Set(["zai.speed"]), ctx);
|
|
166
|
+
|
|
167
|
+
assert.equal(providers.length, 1, "speed is read per request — no re-register");
|
|
168
|
+
assert.equal(JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8")).zai.speed, "standard");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("warns when env shadows the saved value", async () => {
|
|
172
|
+
const agentDir = tempDir("zai-cfg-agent-");
|
|
173
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
174
|
+
process.env.ZAI_ANTHROPIC_BASE_URL = "https://zcode.z.ai/api/v1/zcode-plan/anthropic";
|
|
175
|
+
const cwd = tempDir("zai-cfg-cwd-");
|
|
176
|
+
const { pi } = fakePi();
|
|
177
|
+
zaiModule(pi);
|
|
178
|
+
|
|
179
|
+
const cfg = zaiConfig(pi);
|
|
180
|
+
cfg.groups()[0]!.rows.find((r) => r.key === "zai.baseUrl")!.set("https://open.bigmodel.cn/api/anthropic");
|
|
181
|
+
const { ctx, notifications } = saveCtx(cwd);
|
|
182
|
+
await cfg.save(new Set(["zai.baseUrl"]), ctx);
|
|
183
|
+
|
|
184
|
+
assert.equal(notifications[0]!.level, "warning");
|
|
185
|
+
assert.match(notifications[0]!.message, /env/);
|
|
186
|
+
assert.match(notifications[0]!.message, /Effective endpoint: https:\/\/zcode\.z\.ai/);
|
|
187
|
+
assert.equal(JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8")).zai.baseUrl, "https://open.bigmodel.cn/api/anthropic");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("discloses an untrusted project file that pi will ignore", async () => {
|
|
191
|
+
const agentDir = tempDir("zai-cfg-agent-");
|
|
192
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
193
|
+
const cwd = tempDir("zai-cfg-cwd-");
|
|
194
|
+
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
|
195
|
+
writeFileSync(join(cwd, ".pi", "settings.json"), JSON.stringify({ zai: { baseUrl: "https://open.bigmodel.cn/api/anthropic" } }));
|
|
196
|
+
|
|
197
|
+
const { pi } = fakePi();
|
|
198
|
+
zaiModule(pi);
|
|
199
|
+
const cfg = zaiConfig(pi);
|
|
200
|
+
cfg.groups()[0]!.rows.find((r) => r.key === "zai.speed")!.set("standard");
|
|
201
|
+
const { ctx, notifications } = saveCtx(cwd, false);
|
|
202
|
+
await cfg.save(new Set(["zai.speed"]), ctx);
|
|
203
|
+
|
|
204
|
+
assert.equal(notifications[0]!.level, "info");
|
|
205
|
+
assert.match(notifications[0]!.message, /NOT trusted/);
|
|
206
|
+
});
|
|
207
|
+
});
|