Skip to content

Commit

Permalink
Merge pull request #1156 from bcgov/chore/may16LibUpdates
Browse files Browse the repository at this point in the history
Library updates
  • Loading branch information
loneil authored May 17, 2024
2 parents c3937b8 + e39e98e commit c78ef94
Show file tree
Hide file tree
Showing 14 changed files with 879 additions and 999 deletions.
12 changes: 6 additions & 6 deletions demo/plugins/rpc/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
},
"dependencies": {
"pinia": "^2.1.7",
"vue": "^3.4.24",
"vuetify": "^3.5.17"
"vue": "^3.4.27",
"vuetify": "^3.6.6"
},
"devDependencies": {
"@mdi/font": "^7.4.47",
"@rushstack/eslint-patch": "^1.10.2",
"@tsconfig/node20": "^20.1.4",
"@types/node": "^20.12.7",
"@types/node": "^20.12.12",
"@vitejs/plugin-vue": "^5.0.3",
"@vue/eslint-config-prettier": "^9.0.0",
"@vue/eslint-config-typescript": "^13.0.0",
"@vue/tsconfig": "^0.5.1",
"eslint": "^8.57.0",
"eslint-plugin-vue": "^9.25.0",
"eslint-plugin-vue": "^9.26.0",
"npm-run-all2": "^6.1.1",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"vite": "^5.2.10",
"vue-tsc": "^2.0.14"
"vite": "^5.2.11",
"vue-tsc": "^2.0.19"
}
}
8 changes: 6 additions & 2 deletions demo/plugins/rpc/app/src/components/Console.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ watch(
() => appStore.tenant,
() => {
if (appStore.tenant) {
const ws = new WebSocket(`ws://localhost:${import.meta.env.VITE_AGENT_PORT}/ws/${appStore.tenant?.walletId}`)
const ws = new WebSocket(
`ws://localhost:${import.meta.env.VITE_AGENT_PORT}/ws/${appStore.tenant?.walletId}`
)
ws.onopen = () => {
appStore.addMessage(`Hello ${appStore.tenant?.tenantName}, you have an active websocket and will receive messages here.`)
appStore.addMessage(
`Hello ${appStore.tenant?.tenantName}, you have an active websocket and will receive messages here.`
)
}
ws.onmessage = async (wsMessage) => {
const { message, topic } = JSON.parse(wsMessage.data)
Expand Down
36 changes: 18 additions & 18 deletions demo/plugins/rpc/app/src/services/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import { useAppStore } from '@/stores/app'
import type AgentService from '@/services/agent'

export default class ConnectionService {
private agentService!: AgentService
private appStore = useAppStore()
private agentService!: AgentService
private appStore = useAppStore()

constructor(agentService: AgentService) {
this.agentService = agentService
}
constructor(agentService: AgentService) {
this.agentService = agentService
}

handleConnectionMessage(message: any): void {
const { state } = message
if (state === 'active') {
const { their_label: label } = message;
this.appStore.addConnection(message)
this.appStore.addMessage(`You are now connected with ${label}`)
}
handleConnectionMessage(message: any): void {
const { state } = message
if (state === 'active') {
const { their_label: label } = message
this.appStore.addConnection(message)
this.appStore.addMessage(`You are now connected with ${label}`)
}
}

async createInvitation(): Promise<any> {
return this.agentService.fetchInvitation()
}
async createInvitation(): Promise<any> {
return this.agentService.fetchInvitation()
}

async acceptInvitation(invitation: any): Promise<void> {
return this.agentService.createConnection(invitation)
}
async acceptInvitation(invitation: any): Promise<void> {
return this.agentService.createConnection(invitation)
}
}
182 changes: 92 additions & 90 deletions demo/plugins/rpc/app/src/services/drpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,111 @@ import { useAppStore } from '@/stores/app'
import AgentService from '@/services/agent'

export default class DrpcService {
private agentService!: AgentService
private agentService!: AgentService

private appStore = useAppStore()
private appStore = useAppStore()

constructor(agentService: AgentService) {
this.agentService = agentService
}
constructor(agentService: AgentService) {
this.agentService = agentService
}

async handleDrpcMessage(topic: string, message: any): Promise<void> {
// Deep copy the message so we don't modify it when processing
const messageCopy = JSON.parse(JSON.stringify(message))
async handleDrpcMessage(topic: string, message: any): Promise<void> {
// Deep copy the message so we don't modify it when processing
const messageCopy = JSON.parse(JSON.stringify(message))

switch (topic) {
case 'drpc_request':
this.appStore.addMessage(message)
switch (topic) {
case 'drpc_request':
this.appStore.addMessage(message)

// eslint-disable-next-line no-case-declarations
const {
request: dRequest,
connection_id: connectionId,
thread_id: rpcRequestId
} = messageCopy
// eslint-disable-next-line no-case-declarations
const { request } = dRequest
// eslint-disable-next-line no-case-declarations
const aRequest = request && !Array.isArray(request) ? [request] : request
// eslint-disable-next-line no-case-declarations
const {
request: dRequest,
connection_id: connectionId,
thread_id: rpcRequestId
} = messageCopy
// eslint-disable-next-line no-case-declarations
const { request } = dRequest
// eslint-disable-next-line no-case-declarations
const aRequest = request && !Array.isArray(request) ? [request] : request

if (aRequest?.length) {
const aResult = aRequest.map(this.rpcMethodHandler).filter((result: any) => !!result)
const response = request && !Array.isArray(request) ? aResult?.[0] ?? {} : aResult
await this.agentService.sendDrpcResponse(connectionId, rpcRequestId, response)
this.appStore.addMessage(JSON.stringify({
connection_id: connectionId,
thread_id: rpcRequestId,
response: { state: "completed", response }
}))
}
break
case 'drpc_response':
// eslint-disable-next-line no-case-declarations
const { response: dResponse } = messageCopy
// eslint-disable-next-line no-case-declarations
const { response } = dResponse
// eslint-disable-next-line no-case-declarations
const aResponse = response && !Array.isArray(response) ? [response] : response
if (aRequest?.length) {
const aResult = aRequest.map(this.rpcMethodHandler).filter((result: any) => !!result)
const response = request && !Array.isArray(request) ? aResult?.[0] ?? {} : aResult
await this.agentService.sendDrpcResponse(connectionId, rpcRequestId, response)
this.appStore.addMessage(
JSON.stringify({
connection_id: connectionId,
thread_id: rpcRequestId,
response: { state: 'completed', response }
})
)
}
break
case 'drpc_response':
// eslint-disable-next-line no-case-declarations
const { response: dResponse } = messageCopy
// eslint-disable-next-line no-case-declarations
const { response } = dResponse
// eslint-disable-next-line no-case-declarations
const aResponse = response && !Array.isArray(response) ? [response] : response

if (aResponse.length) {
for (const response of aResponse) {
if (response.result) {
this.appStore.addMessage(response.result)
} else if (response.error) {
this.appStore.addMessage(response.error.message)
}
}
}
break
default:
break
if (aResponse.length) {
for (const response of aResponse) {
if (response.result) {
this.appStore.addMessage(response.result)
} else if (response.error) {
this.appStore.addMessage(response.error.message)
}
}
}
break
default:
break
}
}

private rpcMethodHandler(request: any): any {
const { method, params } = request
const aParams = params && !Array.isArray(params) ? Object.values(params) : params
private rpcMethodHandler(request: any): any {
const { method, params } = request
const aParams = params && !Array.isArray(params) ? Object.values(params) : params

switch (method) {
case 'sum':
return {
id: request.id,
jsonrpc: '2.0',
result: aParams.reduce((acc: number, val: number) => (acc += val), 0)
}
case 'subtract':
// eslint-disable-next-line no-case-declarations
const first = aParams.shift()
return {
id: request.id,
jsonrpc: '2.0',
result: aParams.reduce((acc: number, val: number) => (acc -= val), first)
}
default:
// eslint-disable-next-line no-case-declarations
const { id } = request
if (!id) {
// Assume its a notification and do nothing
return
}
return {
id: request.id || null,
jsonrpc: '2.0',
error: {
code: '-32601',
message: 'Method not found'
}
}
switch (method) {
case 'sum':
return {
id: request.id,
jsonrpc: '2.0',
result: aParams.reduce((acc: number, val: number) => (acc += val), 0)
}
case 'subtract':
// eslint-disable-next-line no-case-declarations
const first = aParams.shift()
return {
id: request.id,
jsonrpc: '2.0',
result: aParams.reduce((acc: number, val: number) => (acc -= val), first)
}
default:
// eslint-disable-next-line no-case-declarations
const { id } = request
if (!id) {
// Assume its a notification and do nothing
return
}
return {
id: request.id || null,
jsonrpc: '2.0',
error: {
code: '-32601',
message: 'Method not found'
}
}
}
}

async sendRequest(connectionId: string, request: any): Promise<void> {
return this.agentService.sendDrpcRequest(connectionId, request)
}
async sendRequest(connectionId: string, request: any): Promise<void> {
return this.agentService.sendDrpcRequest(connectionId, request)
}

async sendResponse(connectionId: string, threadId: string, response: any): Promise<void> {
return this.agentService.sendDrpcResponse(connectionId, threadId, response)
}
async sendResponse(connectionId: string, threadId: string, response: any): Promise<void> {
return this.agentService.sendDrpcResponse(connectionId, threadId, response)
}
}
Loading

0 comments on commit c78ef94

Please sign in to comment.