Services
Service Proxies
How to call remote services using service proxies in Kinotic.
Service proxies let you call remote services as if they were local objects. You define a typed interface, create a class that implements it, and delegate calls to the remote service through the Kinotic connection.
Writing a Service Proxy
The pattern is:
- Define an interface describing the service methods
- Create a class that implements the interface
- In the constructor, obtain a proxy via
kinotic.serviceProxy() - Implement methods by calling
invoke()orinvokeStream()on the proxy
Example
import type { IKinotic, IServiceProxy } from '@kinotic-ai/core'
import type { Observable } from 'rxjs'
export interface INotificationService {
sendAlert(message: string): Promise<void>
watchAlerts(severity: string): Observable<Alert>
}
export class NotificationService implements INotificationService {
private readonly serviceProxy: IServiceProxy
constructor(kinotic: IKinotic) {
this.serviceProxy = kinotic.serviceProxy('com.example.NotificationService')
}
sendAlert(message: string): Promise<void> {
return this.serviceProxy.invoke('sendAlert', [message])
}
watchAlerts(severity: string): Observable<Alert> {
return this.serviceProxy.invokeStream('watchAlerts', [severity])
}
}
Usage
import { Kinotic } from '@kinotic-ai/core'
const notifications = new NotificationService(Kinotic)
// Request-response
await notifications.sendAlert('High CPU usage detected')
// Streaming (returns an Observable)
notifications.watchAlerts('critical').subscribe((alert) => {
console.log('Alert:', alert)
})
Invocation Methods
Each proxy provides two ways to call the remote service:
| Method | Returns | Use When |
|---|---|---|
invoke(method, args?) | Promise<any> | The service returns a single result |
invokeStream(method, args?) | Observable<any> | The service pushes multiple values over time |