Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ jobs:
repository: ${{ github.repository }}
run-id: ${{ github.event.workflow_run.id }}

- run: npm publish --access public --ignore-scripts
- name: Publish package
run: |
VERSION=$(node -p "require('./package.json').version")
MAJOR=${VERSION%%.*}
if [ "$MAJOR" -ge 2 ]; then
npm publish --access public --ignore-scripts --tag v2
else
npm publish --access public --ignore-scripts
fi
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@ OpenCode plugin that automatically switches to fallback models when rate limited

## Installation

Add the plugin to your `opencode.json`:
OpenCode v2 / Web uses the `plugins` key:

```json
{
"plugin": ["@azumag/opencode-rate-limit-fallback"]
"plugins": ["@azumag/opencode-rate-limit-fallback@2.0.0"]
}
```

OpenCode v1 must stay on the latest 1.x release:

```json
{
"plugin": ["@azumag/opencode-rate-limit-fallback@1.70.11"]
}
```

Version 2 is published under the `v2` npm tag so unpinned OpenCode v1
installations do not receive the incompatible v2 entry point.

The v2 entry point currently implements `fallbackMode: "wait"`. Model-switching
modes remain available in the v1-compatible 1.x line.

OpenCode will automatically install the plugin on startup.

This release is tested with OpenCode `1.18.16`. Older OpenCode releases may
Expand Down
89 changes: 89 additions & 0 deletions __tests__/v2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import plugin from "../v2.js";
import { existsSync, readFileSync } from "fs";

vi.mock("fs", () => ({
existsSync: vi.fn(),
readFileSync: vi.fn(),
}));

describe("OpenCode v2 plugin", () => {
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});

it("exports the v2 definition shape", () => {
expect(plugin.id).toBe("opencode-rate-limit-fallback");
expect(plugin.setup).toBeTypeOf("function");
});

it("overrides rate-limit retries with the configured cooldown", async () => {
vi.mocked(existsSync).mockReturnValue(true);
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({
enabled: true,
fallbackMode: "wait",
cooldownMs: 60000,
fallbackModels: [],
}));
let retryHook: ((event: any) => void) | undefined;
const dispose = vi.fn();
const cleanup = await plugin.setup({
location: { directory: "/test" },
session: {
hook: vi.fn(async (_name, callback) => {
retryHook = callback;
return { dispose };
}),
prompt: vi.fn(),
},
});

const event = {
sessionID: "session-1",
attempt: 1,
error: { type: "provider", message: "Rate limit exceeded", status: 429 },
decision: { retry: false },
};
retryHook?.(event);
expect(event.decision).toEqual({ retry: true, delay: 60000 });

await cleanup?.();
expect(dispose).toHaveBeenCalledOnce();
});

it("resumes a terminal rate-limit failure after cooldown", async () => {
vi.useFakeTimers();
vi.mocked(existsSync).mockReturnValue(true);
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({
enabled: true,
fallbackMode: "wait",
cooldownMs: 1000,
fallbackModels: [],
}));
let retryHook: ((event: any) => void) | undefined;
const prompt = vi.fn().mockResolvedValue(undefined);
const cleanup = await plugin.setup({
location: { directory: "/test" },
session: {
hook: vi.fn(async (_name, callback) => {
retryHook = callback;
return { dispose: vi.fn() };
}),
prompt,
},
});

retryHook?.({
sessionID: "session-1",
attempt: 5,
error: { status: 429, message: "Rate limit exceeded" },
decision: { retry: false },
});
await vi.advanceTimersByTimeAsync(1250);

expect(prompt).toHaveBeenCalledWith({ sessionID: "session-1", text: "", resume: true });

await cleanup?.();
});
});
Loading