feat: idempotency key
This commit is contained in:
parent
4a22a641e3
commit
6965584e4a
4 changed files with 189 additions and 7 deletions
|
|
@ -68,8 +68,29 @@ cn serve --timeout 600 # 10 minute timeout
|
|||
```bash
|
||||
cn remote "help debug this" # Create new remote environment
|
||||
cn remote --url https://my-server.com "work" # Connect to existing server
|
||||
cn remote --idempotency-key my-session "work" # Resume or create idempotent session
|
||||
```
|
||||
|
||||
### Idempotent Sessions
|
||||
|
||||
Use `--idempotency-key` to create resumable remote sessions:
|
||||
|
||||
```bash
|
||||
# First time - creates new session
|
||||
cn remote --idempotency-key "project-review" "start code review"
|
||||
|
||||
# Later - resumes same session if it exists
|
||||
cn remote --idempotency-key "project-review" "continue where we left off"
|
||||
|
||||
# Different key - creates separate session
|
||||
cn remote --idempotency-key "feature-dev" "work on new feature"
|
||||
```
|
||||
|
||||
The backend manages session lifecycle based on the idempotency key:
|
||||
- New key = new remote environment
|
||||
- Existing key = connect to existing session
|
||||
- Sessions may expire based on backend configuration
|
||||
|
||||
## Common Examples
|
||||
|
||||
```bash
|
||||
|
|
|
|||
154
src/commands/remote.test.ts
Normal file
154
src/commands/remote.test.ts
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { remote } from "./remote.js";
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock("../auth/workos.js");
|
||||
vi.mock("../env.js");
|
||||
vi.mock("../telemetry/telemetryService.js");
|
||||
vi.mock("../ui/index.js");
|
||||
vi.mock("../util/logger.js");
|
||||
|
||||
const mockWorkos = vi.mocked(
|
||||
await import("../auth/workos.js")
|
||||
);
|
||||
const mockEnv = vi.mocked(await import("../env.js"));
|
||||
const mockStartRemoteTUIChat = vi.mocked(
|
||||
await import("../ui/index.js")
|
||||
);
|
||||
|
||||
// Mock fetch globally
|
||||
const mockFetch = vi.fn();
|
||||
global.fetch = mockFetch as any;
|
||||
|
||||
// Mock console methods
|
||||
const mockConsoleInfo = vi.fn();
|
||||
const mockConsoleError = vi.fn();
|
||||
global.console = {
|
||||
...global.console,
|
||||
info: mockConsoleInfo,
|
||||
error: mockConsoleError,
|
||||
};
|
||||
|
||||
describe("remote command", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Setup default mocks
|
||||
mockWorkos.loadAuthConfig.mockReturnValue({
|
||||
userId: "test-user-id",
|
||||
userEmail: "test@example.com",
|
||||
accessToken: "test-token",
|
||||
refreshToken: "test-refresh-token",
|
||||
expiresAt: Date.now() + 3600000,
|
||||
organizationId: null,
|
||||
});
|
||||
|
||||
mockWorkos.isAuthenticatedConfig.mockReturnValue(true);
|
||||
mockWorkos.getAccessToken.mockReturnValue("test-token");
|
||||
|
||||
mockEnv.env = {
|
||||
apiBase: "https://api.example.com",
|
||||
workOsClientId: "test-client-id",
|
||||
appUrl: "https://test.example.com",
|
||||
};
|
||||
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
url: "ws://test-url.com",
|
||||
port: 8080,
|
||||
}),
|
||||
});
|
||||
|
||||
mockStartRemoteTUIChat.startRemoteTUIChat.mockResolvedValue({} as any);
|
||||
});
|
||||
|
||||
it("should include idempotency key in request body when provided", async () => {
|
||||
const testIdempotencyKey = "test-idempotency-key-123";
|
||||
|
||||
await remote("test prompt", { idempotencyKey: testIdempotencyKey });
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
new URL("agents/devboxes", mockEnv.env.apiBase),
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer test-token",
|
||||
},
|
||||
body: expect.stringContaining(`"idempotencyKey":"${testIdempotencyKey}"`),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should not include idempotency key in request body when not provided", async () => {
|
||||
await remote("test prompt", {});
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
new URL("agents/devboxes", mockEnv.env.apiBase),
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer test-token",
|
||||
},
|
||||
body: expect.not.stringContaining("idempotencyKey"),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should display idempotency key in console when provided", async () => {
|
||||
const testIdempotencyKey = "test-key-456";
|
||||
|
||||
await remote("test prompt", { idempotencyKey: testIdempotencyKey });
|
||||
|
||||
expect(mockConsoleInfo).toHaveBeenCalledWith(
|
||||
expect.stringContaining(`Using idempotency key: ${testIdempotencyKey}`)
|
||||
);
|
||||
});
|
||||
|
||||
it("should not display idempotency key message when not provided", async () => {
|
||||
await remote("test prompt", {});
|
||||
|
||||
expect(mockConsoleInfo).not.toHaveBeenCalledWith(
|
||||
expect.stringContaining("Using idempotency key:")
|
||||
);
|
||||
});
|
||||
|
||||
it("should work with direct URL connection and idempotency key", async () => {
|
||||
const testUrl = "ws://direct-url.com";
|
||||
const testIdempotencyKey = "direct-key-789";
|
||||
|
||||
await remote("test prompt", {
|
||||
url: testUrl,
|
||||
idempotencyKey: testIdempotencyKey
|
||||
});
|
||||
|
||||
// Should not make POST request when connecting directly to URL
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
|
||||
// Should connect directly to the provided URL
|
||||
expect(mockStartRemoteTUIChat.startRemoteTUIChat).toHaveBeenCalledWith(
|
||||
testUrl,
|
||||
"test prompt"
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle proper request body structure with all fields", async () => {
|
||||
const testIdempotencyKey = "structured-test-key";
|
||||
|
||||
await remote("test prompt", { idempotencyKey: testIdempotencyKey });
|
||||
|
||||
const fetchCall = mockFetch.mock.calls[0];
|
||||
const requestBody = JSON.parse(fetchCall[1].body);
|
||||
|
||||
expect(requestBody).toEqual({
|
||||
cUserId: "test-user-id",
|
||||
repoUrl: "https://github.com/continuedev/amplified.dev",
|
||||
name: expect.stringMatching(/^devbox-\d+$/),
|
||||
prompt: "test prompt",
|
||||
idempotencyKey: testIdempotencyKey,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -12,7 +12,7 @@ import { logger } from "../util/logger.js";
|
|||
|
||||
export async function remote(
|
||||
prompt: string | undefined,
|
||||
options: { url?: string } = {}
|
||||
options: { url?: string; idempotencyKey?: string } = {}
|
||||
) {
|
||||
// If --url is provided, connect directly to that URL
|
||||
if (options.url) {
|
||||
|
|
@ -47,18 +47,21 @@ export async function remote(
|
|||
|
||||
const accessToken = getAccessToken(authConfig);
|
||||
|
||||
const requestBody = {
|
||||
cUserId: authConfig.userId,
|
||||
repoUrl: "https://github.com/continuedev/amplified.dev", // getRepoUrl(),
|
||||
name: `devbox-${Date.now()}`,
|
||||
prompt: prompt,
|
||||
idempotencyKey: options.idempotencyKey,
|
||||
};
|
||||
|
||||
const response = await fetch(new URL("agents/devboxes", env.apiBase), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
cUserId: authConfig.userId,
|
||||
repoUrl: "https://github.com/continuedev/amplified.dev", // getRepoUrl(),
|
||||
name: `devbox-${Date.now()}`,
|
||||
prompt: prompt,
|
||||
}),
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
|
|
|||
|
|
@ -156,6 +156,10 @@ program
|
|||
"--url <url>",
|
||||
"Connect directly to the specified URL instead of creating a new remote environment"
|
||||
)
|
||||
.option(
|
||||
"--idempotency-key <key>",
|
||||
"Idempotency key for session management - allows resuming existing sessions"
|
||||
)
|
||||
.action(async (prompt: string | undefined, options) => {
|
||||
await remote(prompt, options);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue