-
Notifications
You must be signed in to change notification settings - Fork 909
feat(acp-adapter): implement session/close and logout stable methods #1728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xy200303
wants to merge
5
commits into
MoonshotAI:main
Choose a base branch
from
xy200303:fix/acp-stable-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6da7c6d
feat(acp-adapter): implement session/close and logout stable methods
xy200303 2d4cf17
fix(acp-adapter): cancel pending prompts before closing session
xy200303 ad616fe
Merge branch 'main' into fix/acp-stable-methods
xy200303 24581e6
Merge branch 'main' into fix/acp-stable-methods
xy200303 56354d7
Merge branch 'main' into fix/acp-stable-methods
xy200303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Implement the remaining stable ACP agent-side methods, `session/close` and `logout`, so ACP clients can close sessions and sign out through the protocol. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { | ||
| AgentSideConnection, | ||
| ClientSideConnection, | ||
| ndJsonStream, | ||
| type Client, | ||
| type ReadTextFileRequest, | ||
| type ReadTextFileResponse, | ||
| type RequestPermissionRequest, | ||
| type RequestPermissionResponse, | ||
| type SessionNotification, | ||
| type WriteTextFileRequest, | ||
| type WriteTextFileResponse, | ||
| } from '@agentclientprotocol/sdk'; | ||
| import { type KimiHarness } from '@moonshot-ai/kimi-code-sdk'; | ||
|
|
||
| import { AcpServer } from '../src/server'; | ||
| import { AUTHED_STATUS } from './_helpers/harness-stubs'; | ||
|
|
||
| class StubClient implements Client { | ||
| async requestPermission(_p: RequestPermissionRequest): Promise<RequestPermissionResponse> { | ||
| throw new Error('StubClient.requestPermission should not be called in logout test'); | ||
| } | ||
| async sessionUpdate(_n: SessionNotification): Promise<void> { | ||
| throw new Error('StubClient.sessionUpdate should not be called in logout test'); | ||
| } | ||
| async writeTextFile(_p: WriteTextFileRequest): Promise<WriteTextFileResponse> { | ||
| throw new Error('StubClient.writeTextFile should not be called in logout test'); | ||
| } | ||
| async readTextFile(_p: ReadTextFileRequest): Promise<ReadTextFileResponse> { | ||
| throw new Error('StubClient.readTextFile should not be called in logout test'); | ||
| } | ||
| } | ||
|
|
||
| function makeInMemoryStreamPair(): { | ||
| agentStream: ReturnType<typeof ndJsonStream>; | ||
| clientStream: ReturnType<typeof ndJsonStream>; | ||
| } { | ||
| const clientToAgent = new TransformStream<Uint8Array, Uint8Array>(); | ||
| const agentToClient = new TransformStream<Uint8Array, Uint8Array>(); | ||
| const agentStream = ndJsonStream(agentToClient.writable, clientToAgent.readable); | ||
| const clientStream = ndJsonStream(clientToAgent.writable, agentToClient.readable); | ||
| return { agentStream, clientStream }; | ||
| } | ||
|
|
||
| describe('AcpServer logout', () => { | ||
| it('forwards logout to harness.auth.logout', async () => { | ||
| const logout = vi.fn().mockResolvedValue(undefined); | ||
| const harness = { | ||
| auth: { | ||
| status: async () => AUTHED_STATUS, | ||
| logout, | ||
| }, | ||
| } as unknown as KimiHarness; | ||
|
|
||
| const { agentStream, clientStream } = makeInMemoryStreamPair(); | ||
| new AgentSideConnection((c) => new AcpServer(harness, c), agentStream); | ||
| const client = new ClientSideConnection((_a) => new StubClient(), clientStream); | ||
|
|
||
| await client.logout({}); | ||
|
|
||
| expect(logout).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { | ||
| AgentSideConnection, | ||
| ClientSideConnection, | ||
| ndJsonStream, | ||
| type Client, | ||
| type ReadTextFileRequest, | ||
| type ReadTextFileResponse, | ||
| type RequestPermissionRequest, | ||
| type RequestPermissionResponse, | ||
| type SessionNotification, | ||
| type WriteTextFileRequest, | ||
| type WriteTextFileResponse, | ||
| } from '@agentclientprotocol/sdk'; | ||
| import { type KimiHarness, type Session } from '@moonshot-ai/kimi-code-sdk'; | ||
|
|
||
| import { AcpServer } from '../src/server'; | ||
| import { AUTHED_STATUS } from './_helpers/harness-stubs'; | ||
|
|
||
| class StubClient implements Client { | ||
| async requestPermission(_p: RequestPermissionRequest): Promise<RequestPermissionResponse> { | ||
| throw new Error('StubClient.requestPermission should not be called in session/close test'); | ||
| } | ||
| async sessionUpdate(_n: SessionNotification): Promise<void> { | ||
| throw new Error('StubClient.sessionUpdate should not be called in session/close test'); | ||
| } | ||
| async writeTextFile(_p: WriteTextFileRequest): Promise<WriteTextFileResponse> { | ||
| throw new Error('StubClient.writeTextFile should not be called in session/close test'); | ||
| } | ||
| async readTextFile(_p: ReadTextFileRequest): Promise<ReadTextFileResponse> { | ||
| throw new Error('StubClient.readTextFile should not be called in session/close test'); | ||
| } | ||
| } | ||
|
|
||
| function makeInMemoryStreamPair(): { | ||
| agentStream: ReturnType<typeof ndJsonStream>; | ||
| clientStream: ReturnType<typeof ndJsonStream>; | ||
| } { | ||
| const clientToAgent = new TransformStream<Uint8Array, Uint8Array>(); | ||
| const agentToClient = new TransformStream<Uint8Array, Uint8Array>(); | ||
| const agentStream = ndJsonStream(agentToClient.writable, clientToAgent.readable); | ||
| const clientStream = ndJsonStream(clientToAgent.writable, agentToClient.readable); | ||
| return { agentStream, clientStream }; | ||
| } | ||
|
|
||
| describe('AcpServer session/close', () => { | ||
| it('closes a known session and removes it from the server map', async () => { | ||
| let closeCalls = 0; | ||
| let cancelCalls = 0; | ||
| const fakeSession = { | ||
| id: 'sess-close-known', | ||
| prompt: async () => undefined, | ||
| cancel: async () => { | ||
| cancelCalls += 1; | ||
| }, | ||
| close: async () => { | ||
| closeCalls += 1; | ||
| }, | ||
| onEvent: () => () => undefined, | ||
| } as unknown as Session; | ||
| const harness = { | ||
| auth: { status: async () => AUTHED_STATUS }, | ||
| createSession: async () => fakeSession, | ||
| } as unknown as KimiHarness; | ||
|
|
||
| const { agentStream, clientStream } = makeInMemoryStreamPair(); | ||
| const serverRef = { current: undefined as AcpServer | undefined }; | ||
| new AgentSideConnection( | ||
| (c) => { | ||
| const server = new AcpServer(harness, c); | ||
| serverRef.current = server; | ||
| return server; | ||
| }, | ||
| agentStream, | ||
| ); | ||
| const client = new ClientSideConnection((_a) => new StubClient(), clientStream); | ||
|
|
||
| await client.newSession({ cwd: '/tmp/x', mcpServers: [] }); | ||
| expect(serverRef.current?.getSession('sess-close-known')).toBeDefined(); | ||
|
|
||
| await client.closeSession({ sessionId: 'sess-close-known' }); | ||
|
|
||
| expect(cancelCalls).toBe(1); | ||
| expect(closeCalls).toBe(1); | ||
| expect(serverRef.current?.getSession('sess-close-known')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('throws invalid_params when sessionId is unknown', async () => { | ||
| const harness = { | ||
| auth: { status: async () => AUTHED_STATUS }, | ||
| createSession: async () => { | ||
| throw new Error('createSession should not be called when no session is created'); | ||
| }, | ||
| } as unknown as KimiHarness; | ||
|
|
||
| const { agentStream, clientStream } = makeInMemoryStreamPair(); | ||
| new AgentSideConnection((c) => new AcpServer(harness, c), agentStream); | ||
| const client = new ClientSideConnection((_a) => new StubClient(), clientStream); | ||
|
|
||
| await expect(client.closeSession({ sessionId: 'sess-unknown' })).rejects.toThrow( | ||
| /Unknown sessionId/, | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.