Rework API

This commit is contained in:
Andrey Sobolev
2025-09-15 20:25:13 +07:00
parent 4916937d13
commit 4712fa6c29
26 changed files with 613 additions and 368 deletions
+16 -7
View File
@@ -2,6 +2,7 @@ import {
AgentImpl,
composeCID,
containerOnAgentEndpointRef,
containerUuid,
EndpointKind,
NetworkImpl,
parseEndpointRef,
@@ -29,13 +30,19 @@ const kinds = {
function createAgent1 (tickMgr: TickManagerImpl, networkClient: NetworkClient): AgentImpl {
const agent: AgentImpl = new AgentImpl(agents.agent1, {
[kinds.session]: async (uuid) => {
return [new DummySessionContainer(), containerOnAgentEndpointRef(agent.endpoint as AgentEndpointRef, uuid)]
[kinds.session]: async (options) => {
const uuid = options.uuid ?? containerUuid()
return {
uuid,
container: new DummySessionContainer(),
endpoint: containerOnAgentEndpointRef(agent.endpoint as AgentEndpointRef, uuid)
}
},
[kinds.workspace]: async (uuid) => {
[kinds.workspace]: async (options) => {
const uuid = options.uuid ?? containerUuid()
const container = new DummyWorkspaceContainer(uuid, agent.uuid, networkClient)
const endpoint = await container.start(tickMgr)
return [container, endpoint]
return { uuid, container, endpoint }
}
})
return agent
@@ -59,6 +66,7 @@ describe('check network server is working fine', () => {
await networkClient.close()
await network.close()
tickMgr.stop()
})
it('check routed connection and requests', async () => {
@@ -83,10 +91,10 @@ describe('check network server is working fine', () => {
const _agents = await networkClient.agents()
expect(_agents.length).toEqual(1)
expect(_agents[0].agentId).toEqual(agents.agent1)
expect(_agents[0].containers.length).toEqual(0)
expect(_agents[0].containers).toEqual(0)
// Start a new container and check if messaging works
const containerRef = await networkClient.get(composeCID('session', 'user1'), { kind: kinds.session })
const containerRef = await networkClient.get(kinds.session, { uuid: composeCID('session', 'user1') })
const data = parseEndpointRef(containerRef.endpoint)
expect(data.kind).toEqual(EndpointKind.routed)
@@ -113,8 +121,9 @@ describe('check network server is working fine', () => {
expect(events.length).toEqual(1)
expect(events[0]).toEqual('event')
await agentServer.close()
await networkClient.close()
await agentServer.close()
await network.close()
tickMgr.stop()
})
})
+8 -6
View File
@@ -6,7 +6,7 @@ import {
type ContainerEndpointRef,
type ContainerKind,
type ContainerRecord,
type ContainerRequest,
type GetOptions,
type ContainerUuid,
type Network,
type NetworkAgent,
@@ -25,8 +25,8 @@ class AgentCallbackHandler implements NetworkAgent {
readonly client: ClientUuid
) {}
async get (uuid: ContainerUuid, request: ContainerRequest): Promise<ContainerEndpointRef> {
return await this.rpcServer.request(this.client, opNames.getContainer, [this.uuid, [uuid, request]])
async get (kind: ContainerKind, request: GetOptions): Promise<[ContainerUuid, ContainerEndpointRef]> {
return await this.rpcServer.request(this.client, opNames.getContainer, [this.uuid, [kind, request]])
}
async list (kind?: ContainerKind): Promise<ContainerRecord[]> {
@@ -92,9 +92,9 @@ export class NetworkServer implements BackRPCServerHandler<ClientUuid> {
break
}
case opNames.getContainer: {
const uuid: ContainerUuid = params.uuid
const request: ContainerRequest = params.request
await send(await this.network.get(client, uuid, request))
const kind: ContainerKind = params.kind
const request: GetOptions = params.request
await send(await this.network.get(client, kind, request))
break
}
case opNames.releaseContainer: {
@@ -164,6 +164,7 @@ export class NetworkServer implements BackRPCServerHandler<ClientUuid> {
new AgentCallbackHandler(server, agentUuid, endpoint, kinds, client)
)
this.network.mapAgent(client, agentUuid)
console.log('Client registered agent', agentUuid, 'with containers', containers.length, 'kinds', kinds)
await send(res)
}
@@ -175,6 +176,7 @@ export class NetworkServer implements BackRPCServerHandler<ClientUuid> {
const agentUuid: AgentUuid = params.uuid
await this.network.unregister(agentUuid)
this.network.unmapAgent(client, agentUuid)
console.log('Client unregistered agent', agentUuid)
await send('ok')
}
}