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
4 changes: 3 additions & 1 deletion nodejs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import type {
TraceContextProvider,
TypedSessionLifecycleHandler,
} from "./types.js";
import { defaultJoinSessionPermissionHandler } from "./types.js";

/**
* Minimum protocol version this SDK can communicate with.
Expand Down Expand Up @@ -868,7 +869,8 @@ export class CopilotClient {
})),
provider: config.provider,
modelCapabilities: config.modelCapabilities,
requestPermission: true,
requestPermission:
config.onPermissionRequest !== defaultJoinSessionPermissionHandler,
requestUserInput: !!config.onUserInputRequest,
requestElicitation: !!config.onElicitationRequest,
hooks: !!(config.hooks && Object.values(config.hooks).some(Boolean)),
Expand Down
10 changes: 5 additions & 5 deletions nodejs/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import { CopilotClient } from "./client.js";
import type { CopilotSession } from "./session.js";
import type { PermissionHandler, PermissionRequestResult, ResumeSessionConfig } from "./types.js";

const defaultJoinSessionPermissionHandler: PermissionHandler = (): PermissionRequestResult => ({
kind: "no-result",
});
import {
defaultJoinSessionPermissionHandler,
type PermissionHandler,
type ResumeSessionConfig,
} from "./types.js";

export type JoinSessionConfig = Omit<ResumeSessionConfig, "onPermissionRequest"> & {
onPermissionRequest?: PermissionHandler;
Expand Down
5 changes: 5 additions & 0 deletions nodejs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@ export type PermissionHandler = (

export const approveAll: PermissionHandler = () => ({ kind: "approved" });

export const defaultJoinSessionPermissionHandler: PermissionHandler =
(): PermissionRequestResult => ({
kind: "no-result",
});

// ============================================================================
// User Input Request Types
// ============================================================================
Expand Down
55 changes: 55 additions & 0 deletions nodejs/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it, onTestFinished, vi } from "vitest";
import { approveAll, CopilotClient, type ModelInfo } from "../src/index.js";
import { defaultJoinSessionPermissionHandler } from "../src/types.js";

// This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead

Expand Down Expand Up @@ -97,6 +98,60 @@ describe("CopilotClient", () => {
spy.mockRestore();
});

it("does not request permissions on session.resume when using the default joinSession handler", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());

const session = await client.createSession({ onPermissionRequest: approveAll });
const spy = vi
.spyOn((client as any).connection!, "sendRequest")
.mockImplementation(async (method: string, params: any) => {
if (method === "session.resume") return { sessionId: params.sessionId };
throw new Error(`Unexpected method: ${method}`);
});

await client.resumeSession(session.sessionId, {
onPermissionRequest: defaultJoinSessionPermissionHandler,
});

expect(spy).toHaveBeenCalledWith(
"session.resume",
expect.objectContaining({
sessionId: session.sessionId,
requestPermission: false,
})
);
spy.mockRestore();
});

it("requests permissions on session.resume when using an explicit handler", async () => {
const client = new CopilotClient();
await client.start();
onTestFinished(() => client.forceStop());

const session = await client.createSession({ onPermissionRequest: approveAll });
const spy = vi
.spyOn((client as any).connection!, "sendRequest")
.mockImplementation(async (method: string, params: any) => {
if (method === "session.resume") return { sessionId: params.sessionId };
throw new Error(`Unexpected method: ${method}`);
});

await client.resumeSession(session.sessionId, {
onPermissionRequest: approveAll,
});

expect(spy).toHaveBeenCalledWith(
"session.resume",
expect.objectContaining({
sessionId: session.sessionId,
requestPermission: true,
})
);
spy.mockRestore();
});

it("sends session.model.switchTo RPC with correct params", async () => {
const client = new CopilotClient();
await client.start();
Expand Down
2 changes: 2 additions & 0 deletions nodejs/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { CopilotClient } from "../src/client.js";
import { approveAll } from "../src/index.js";
import { joinSession } from "../src/extension.js";
import { defaultJoinSessionPermissionHandler } from "../src/types.js";

describe("joinSession", () => {
const originalSessionId = process.env.SESSION_ID;
Expand All @@ -19,12 +20,13 @@
process.env.SESSION_ID = "session-123";
const resumeSession = vi
.spyOn(CopilotClient.prototype, "resumeSession")
.mockResolvedValue({} as any);

Check warning on line 23 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 23 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 23 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (windows-latest)

Unexpected any. Specify a different type

await joinSession({ tools: [] });

const [, config] = resumeSession.mock.calls[0]!;
expect(config.onPermissionRequest).toBeDefined();
expect(config.onPermissionRequest).toBe(defaultJoinSessionPermissionHandler);
const result = await Promise.resolve(
config.onPermissionRequest!({ kind: "write" }, { sessionId: "session-123" })
);
Expand All @@ -36,7 +38,7 @@
process.env.SESSION_ID = "session-123";
const resumeSession = vi
.spyOn(CopilotClient.prototype, "resumeSession")
.mockResolvedValue({} as any);

Check warning on line 41 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (ubuntu-latest)

Unexpected any. Specify a different type

Check warning on line 41 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (macos-latest)

Unexpected any. Specify a different type

Check warning on line 41 in nodejs/test/extension.test.ts

View workflow job for this annotation

GitHub Actions / Node.js SDK Tests (windows-latest)

Unexpected any. Specify a different type

await joinSession({ onPermissionRequest: approveAll, disableResume: false });

Expand Down
Loading