@otonoma/paranet-client 2.11.0-rc.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/client.d.ts +168 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +497 -0
- package/dist/graphql.d.ts +8 -0
- package/dist/graphql.d.ts.map +1 -0
- package/dist/graphql.js +113 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/pncp.d.ts +129 -0
- package/dist/pncp.d.ts.map +1 -0
- package/dist/pncp.js +100 -0
- package/dist/proto/broker.grpc.client.d.ts +154 -0
- package/dist/proto/broker.grpc.client.d.ts.map +1 -0
- package/dist/proto/broker.grpc.client.js +102 -0
- package/dist/proto/broker.grpc.d.ts +6 -0
- package/dist/proto/broker.grpc.d.ts.map +1 -0
- package/dist/proto/broker.grpc.js +32 -0
- package/dist/proto/broker_api.d.ts +324 -0
- package/dist/proto/broker_api.d.ts.map +1 -0
- package/dist/proto/broker_api.js +566 -0
- package/dist/proto/google/protobuf/descriptor.d.ts +2492 -0
- package/dist/proto/google/protobuf/descriptor.d.ts.map +1 -0
- package/dist/proto/google/protobuf/descriptor.js +3250 -0
- package/dist/proto/google/protobuf/timestamp.d.ts +157 -0
- package/dist/proto/google/protobuf/timestamp.d.ts.map +1 -0
- package/dist/proto/google/protobuf/timestamp.js +132 -0
- package/dist/proto/grpc/health/v1/health.client.d.ts +79 -0
- package/dist/proto/grpc/health/v1/health.client.d.ts.map +1 -0
- package/dist/proto/grpc/health/v1/health.client.js +46 -0
- package/dist/proto/grpc/health/v1/health.d.ts +74 -0
- package/dist/proto/grpc/health/v1/health.d.ts.map +1 -0
- package/dist/proto/grpc/health/v1/health.js +152 -0
- package/dist/proto/identifiers.d.ts +82 -0
- package/dist/proto/identifiers.d.ts.map +1 -0
- package/dist/proto/identifiers.js +132 -0
- package/dist/proto/mediums.d.ts +71 -0
- package/dist/proto/mediums.d.ts.map +1 -0
- package/dist/proto/mediums.js +120 -0
- package/dist/proto/observation.d.ts +287 -0
- package/dist/proto/observation.d.ts.map +1 -0
- package/dist/proto/observation.js +443 -0
- package/dist/proto/otonoma/common/value.d.ts +127 -0
- package/dist/proto/otonoma/common/value.d.ts.map +1 -0
- package/dist/proto/otonoma/common/value.js +248 -0
- package/dist/proto/pncp.d.ts +607 -0
- package/dist/proto/pncp.d.ts.map +1 -0
- package/dist/proto/pncp.js +936 -0
- package/dist/schema/paranet.d.ts +245 -0
- package/dist/schema/paranet.d.ts.map +1 -0
- package/dist/schema/paranet.js +7 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +120 -0
- package/package.json +39 -0
- package/src/client.ts +677 -0
- package/src/graphql.ts +103 -0
- package/src/index.ts +14 -0
- package/src/pncp.ts +236 -0
- package/src/proto/broker.grpc.client.ts +193 -0
- package/src/proto/broker.grpc.ts +32 -0
- package/src/proto/broker_api.ts +778 -0
- package/src/proto/google/protobuf/descriptor.ts +4860 -0
- package/src/proto/google/protobuf/timestamp.ts +288 -0
- package/src/proto/grpc/health/v1/health.client.ts +106 -0
- package/src/proto/grpc/health/v1/health.ts +174 -0
- package/src/proto/identifiers.ts +176 -0
- package/src/proto/mediums.ts +168 -0
- package/src/proto/observation.ts +636 -0
- package/src/proto/otonoma/common/value.ts +334 -0
- package/src/proto/pncp.ts +1333 -0
- package/src/schema/paranet.ts +257 -0
- package/src/util.ts +129 -0
- package/tsconfig.json +27 -0
package/README.md
ADDED
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { BrokerClient } from "./proto/broker.grpc.client";
|
|
2
|
+
import { PncpRequest, PncpMessage, PncpCallback } from "./proto/pncp";
|
|
3
|
+
import { MessageResponse, PncpCallbackRequest } from "./proto/broker_api";
|
|
4
|
+
import { ObservationCallback, ObservationMessageWrapper, ObservationRequest } from "./proto/observation";
|
|
5
|
+
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
|
|
6
|
+
import { RpcMetadata, RpcOptions, ServerStreamingCall, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
7
|
+
import { MessageRequest, PncpMessageObject, SkillRequest } from "./pncp";
|
|
8
|
+
import { HealthClient } from "./proto/grpc/health/v1/health.client";
|
|
9
|
+
export interface ParanetClientInit {
|
|
10
|
+
endpoint: string;
|
|
11
|
+
actorId: string;
|
|
12
|
+
}
|
|
13
|
+
export interface PncpClientInit {
|
|
14
|
+
endpoint: string;
|
|
15
|
+
actorId: string;
|
|
16
|
+
actorVersion?: string;
|
|
17
|
+
token?: string;
|
|
18
|
+
refreshToken?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare class PncpClient {
|
|
21
|
+
transport: GrpcWebFetchTransport;
|
|
22
|
+
inner: BrokerClient;
|
|
23
|
+
health: HealthClient;
|
|
24
|
+
endpoint: string;
|
|
25
|
+
actorId: string;
|
|
26
|
+
actorVersion?: string;
|
|
27
|
+
token?: string;
|
|
28
|
+
refreshToken?: string;
|
|
29
|
+
tokenExp?: Date;
|
|
30
|
+
tokenCb?: (token: string, refresh: string) => void;
|
|
31
|
+
constructor(init: PncpClientInit);
|
|
32
|
+
healthCheck(service?: string): Promise<boolean>;
|
|
33
|
+
pncpRequest(req: SkillRequest): Promise<MessageResponse>;
|
|
34
|
+
directPncpRequest(req: PncpRequest): Promise<MessageResponse>;
|
|
35
|
+
pncpMessage(data: MessageRequest): Promise<MessageResponse>;
|
|
36
|
+
pncpMessageObject(data: PncpMessageObject): Promise<MessageResponse>;
|
|
37
|
+
directPncpMessage(data: PncpMessage): Promise<MessageResponse>;
|
|
38
|
+
pncpListener(): PncpListener;
|
|
39
|
+
observerListener(init: ObservationRequest): ParanetObserverListener;
|
|
40
|
+
login(creds: LoginCredentials): Promise<TokenResponse>;
|
|
41
|
+
refreshAuth(): Promise<TokenResponse>;
|
|
42
|
+
logout(): void;
|
|
43
|
+
setTokens(token?: string, refresh?: string): void;
|
|
44
|
+
setTokenCb(cb: (token: string, refresh: string) => void): void;
|
|
45
|
+
hasTokens(): boolean;
|
|
46
|
+
getTokens(): {
|
|
47
|
+
token?: string;
|
|
48
|
+
refresh?: string;
|
|
49
|
+
};
|
|
50
|
+
isTokenValid(): boolean;
|
|
51
|
+
getMetadata(): RpcMetadata;
|
|
52
|
+
issue<R extends object, T extends object>(req: R, f: (this: BrokerClient, req: R, metadata: RpcOptions) => UnaryCall<R, T>): Promise<T>;
|
|
53
|
+
}
|
|
54
|
+
export declare abstract class ParanetListener<Treq extends object, Tres extends object, TresIn extends object = Tres> {
|
|
55
|
+
client: PncpClient;
|
|
56
|
+
start?: (client: PncpClient) => void;
|
|
57
|
+
msg?: (msg: Tres, client: PncpClient) => void;
|
|
58
|
+
error?: (msg: string, fatal: boolean) => void;
|
|
59
|
+
disconnected?: () => void;
|
|
60
|
+
reconnected?: () => void;
|
|
61
|
+
shouldAutoReconnect: boolean;
|
|
62
|
+
streamState?: {
|
|
63
|
+
stream: ServerStreamingCall<Treq, TresIn>;
|
|
64
|
+
abort: AbortController;
|
|
65
|
+
};
|
|
66
|
+
protected constructor(client: PncpClient);
|
|
67
|
+
protected abstract getRequest(): RpcStreamRequest<Treq, TresIn>;
|
|
68
|
+
protected abstract getRes(t: TresIn): Tres | null;
|
|
69
|
+
onmessage(f: (msg: Tres, client: PncpClient) => void): this;
|
|
70
|
+
onstart(f: (client: PncpClient) => void): this;
|
|
71
|
+
onerror(f: (msg: string, isFatal: boolean) => void): this;
|
|
72
|
+
ondisconnect(f: () => void): this;
|
|
73
|
+
onreconnect(f: () => void): this;
|
|
74
|
+
autoReconnect(v: boolean): this;
|
|
75
|
+
connect(): void;
|
|
76
|
+
disconnect(): void;
|
|
77
|
+
makeDecoratedStream(isReconnect: boolean, timeout?: number): void;
|
|
78
|
+
}
|
|
79
|
+
export declare class PncpListener extends ParanetListener<PncpCallbackRequest, PncpCallback> {
|
|
80
|
+
constructor(client: PncpClient);
|
|
81
|
+
protected getRequest(): RpcStreamRequest<PncpCallbackRequest, PncpCallback>;
|
|
82
|
+
protected getRes(t: PncpCallback): PncpCallback | null;
|
|
83
|
+
}
|
|
84
|
+
interface RpcStreamRequest<TReq extends object, TRes extends object> {
|
|
85
|
+
method: (this: BrokerClient, body: TReq, meta: RpcOptions) => ServerStreamingCall<TReq, TRes>;
|
|
86
|
+
body: TReq;
|
|
87
|
+
}
|
|
88
|
+
export declare class ParanetObserverListener extends ParanetListener<ObservationRequest, ObservationCallback, ObservationMessageWrapper> {
|
|
89
|
+
init: ObservationRequest;
|
|
90
|
+
constructor(client: PncpClient, init: ObservationRequest);
|
|
91
|
+
protected getRequest(): RpcStreamRequest<ObservationRequest, ObservationMessageWrapper>;
|
|
92
|
+
protected getRes(t: ObservationMessageWrapper): ObservationCallback | null;
|
|
93
|
+
}
|
|
94
|
+
export declare abstract class BaseClient {
|
|
95
|
+
actorId: string;
|
|
96
|
+
endpoint: string;
|
|
97
|
+
protected token?: string;
|
|
98
|
+
protected refreshToken?: string;
|
|
99
|
+
protected tokenExp?: Date;
|
|
100
|
+
protected tokenCb?: (token: string, refresh: string) => void;
|
|
101
|
+
constructor(init: ParanetClientInit);
|
|
102
|
+
getEndpoint(): string;
|
|
103
|
+
login(secret: ActorSecret): Promise<TokenResponse>;
|
|
104
|
+
refreshAuth(): Promise<TokenResponse>;
|
|
105
|
+
logout(): void;
|
|
106
|
+
setTokens(token?: string, refresh?: string): void;
|
|
107
|
+
setTokenCb(cb: (token: string, refresh: string) => void): void;
|
|
108
|
+
hasTokens(): boolean;
|
|
109
|
+
getTokens(): {
|
|
110
|
+
token?: string;
|
|
111
|
+
refresh?: string;
|
|
112
|
+
};
|
|
113
|
+
isTokenValid(): boolean;
|
|
114
|
+
protected postJson<T>(path: string, body: any): Promise<T>;
|
|
115
|
+
protected post_(path: string, body: any, headers?: Record<string, string>): Promise<Response>;
|
|
116
|
+
protected get<T>(path: string): Promise<T>;
|
|
117
|
+
protected fetch(path: string, method: string, body?: any, headers?: Record<string, string>): Promise<Response>;
|
|
118
|
+
getHeaders(): any;
|
|
119
|
+
}
|
|
120
|
+
export declare class ParanetServiceClient extends BaseClient {
|
|
121
|
+
constructor(init: ParanetClientInit);
|
|
122
|
+
serviceEndpoint(): string;
|
|
123
|
+
schemaFetch(path: string): Promise<any>;
|
|
124
|
+
documentFetch({ name, id }: DocumentArgs): Promise<Response>;
|
|
125
|
+
documentUpload(data: File | any, args?: DocumentArgs): Promise<DocumentInfo>;
|
|
126
|
+
graphql(query: string, inputs?: Record<string, any>): Promise<any>;
|
|
127
|
+
}
|
|
128
|
+
export declare class SysClient extends BaseClient {
|
|
129
|
+
constructor(init: ParanetClientInit);
|
|
130
|
+
nodeInfo(): Promise<NodeInfo>;
|
|
131
|
+
}
|
|
132
|
+
export interface LoginCredentials {
|
|
133
|
+
secret: string;
|
|
134
|
+
kind: "password" | "token" | "jwt";
|
|
135
|
+
}
|
|
136
|
+
export type ActorSecret = {
|
|
137
|
+
password: string;
|
|
138
|
+
} | {
|
|
139
|
+
token: string;
|
|
140
|
+
} | {
|
|
141
|
+
jwt: string;
|
|
142
|
+
};
|
|
143
|
+
export type TokenResponse = {
|
|
144
|
+
access_token: string;
|
|
145
|
+
refresh_token: string;
|
|
146
|
+
};
|
|
147
|
+
export interface DocumentArgs {
|
|
148
|
+
name?: string;
|
|
149
|
+
id?: string;
|
|
150
|
+
}
|
|
151
|
+
export interface DocumentInfo {
|
|
152
|
+
id: string;
|
|
153
|
+
contentType: string;
|
|
154
|
+
}
|
|
155
|
+
export interface NodeInfo {
|
|
156
|
+
id: string;
|
|
157
|
+
name: string;
|
|
158
|
+
versions: {
|
|
159
|
+
broker: string;
|
|
160
|
+
expected_paraflow: string;
|
|
161
|
+
expected_paracord: string;
|
|
162
|
+
para_version: string;
|
|
163
|
+
platform_version: string;
|
|
164
|
+
platform_channel: string;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export {};
|
|
168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M,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE;QACR,MAAM,EAAE,MAAM,CAAC;QACf,iBAAiB,EAAE,MAAM,CAAC;QAC1B,iBAAiB,EAAE,MAAM,CAAC;QAC1B,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;QACzB,gBAAgB,EAAE,MAAM,CAAC;KAC1B,CAAA;CACF"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
// Wrapper client around the paranet broker and data service.
|
|
2
|
+
import { BrokerClient } from "./proto/broker.grpc.client";
|
|
3
|
+
import { ConversationMembership } from "./proto/identifiers";
|
|
4
|
+
import { SkillMatchStrategy, PncpMessageKind, } from "./proto/pncp";
|
|
5
|
+
import { jwtDecode } from "jwt-decode";
|
|
6
|
+
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
|
|
7
|
+
import { fromJsonValue } from "./util";
|
|
8
|
+
import { Value } from "./proto/otonoma/common/value";
|
|
9
|
+
import { HealthClient } from "./proto/grpc/health/v1/health.client";
|
|
10
|
+
import { HealthCheckResponse_ServingStatus } from "./proto/grpc/health/v1/health";
|
|
11
|
+
export class PncpClient {
|
|
12
|
+
constructor(init) {
|
|
13
|
+
this.actorId = init.actorId;
|
|
14
|
+
this.endpoint = init.endpoint;
|
|
15
|
+
this.actorVersion = init.actorVersion;
|
|
16
|
+
// TODO: Consider passing in transport config.
|
|
17
|
+
this.transport = new GrpcWebFetchTransport({
|
|
18
|
+
baseUrl: init.endpoint,
|
|
19
|
+
format: "binary",
|
|
20
|
+
// TODO: Consider other methods here.
|
|
21
|
+
});
|
|
22
|
+
this.inner = new BrokerClient(this.transport);
|
|
23
|
+
this.health = new HealthClient(this.transport);
|
|
24
|
+
}
|
|
25
|
+
async healthCheck(service = "") {
|
|
26
|
+
const req = { service };
|
|
27
|
+
const response = this.health.check(req);
|
|
28
|
+
await response.status;
|
|
29
|
+
const data = await response.response;
|
|
30
|
+
return data.status === HealthCheckResponse_ServingStatus.SERVING;
|
|
31
|
+
}
|
|
32
|
+
async pncpRequest(req) {
|
|
33
|
+
let version = req.actorVersion || this.actorVersion;
|
|
34
|
+
if (!version) {
|
|
35
|
+
throw new Error("Invalid request, must know the actor version");
|
|
36
|
+
}
|
|
37
|
+
let pncpReq = {
|
|
38
|
+
body: {
|
|
39
|
+
subject: req.subject,
|
|
40
|
+
action: req.action,
|
|
41
|
+
body: Value.fromJson(req.body),
|
|
42
|
+
},
|
|
43
|
+
author: {
|
|
44
|
+
id: this.actorId,
|
|
45
|
+
version: version,
|
|
46
|
+
},
|
|
47
|
+
callback: req.callback ? fromJsonValue(req.callback) : undefined,
|
|
48
|
+
parentId: req.parentConversationId,
|
|
49
|
+
versionReq: req.versionReq,
|
|
50
|
+
targetActorId: req.targetId,
|
|
51
|
+
matchStrategy: req.matchStrategy || SkillMatchStrategy.BEST,
|
|
52
|
+
mustMatch: req.mustMatch || true,
|
|
53
|
+
useLock: req.useLock || false,
|
|
54
|
+
allowOpenMatch: req.allowOpenMatch || false,
|
|
55
|
+
};
|
|
56
|
+
return await this.directPncpRequest(pncpReq);
|
|
57
|
+
}
|
|
58
|
+
async directPncpRequest(req) {
|
|
59
|
+
let response = await this.issue(req, BrokerClient.prototype.skillRequest);
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
async pncpMessage(data) {
|
|
63
|
+
const [id, tag] = data.conversation.split("@");
|
|
64
|
+
const membership = tag === "requester" ? ConversationMembership.REQUESTER : tag === "fulfiller" ? ConversationMembership.FULFILLER : ConversationMembership.OBSERVER;
|
|
65
|
+
let pncpId = { id, membership };
|
|
66
|
+
return await this.pncpMessageObject({ id: pncpId, body: data.message });
|
|
67
|
+
}
|
|
68
|
+
async pncpMessageObject(data) {
|
|
69
|
+
let extra;
|
|
70
|
+
switch (data.body.type) {
|
|
71
|
+
case PncpMessageKind.PNCP_QUESTION: {
|
|
72
|
+
extra = {
|
|
73
|
+
oneofKind: "question",
|
|
74
|
+
question: {
|
|
75
|
+
id: data.body.value.id,
|
|
76
|
+
callback: data.body.value.callback ? fromJsonValue(data.body.value.callback) : undefined,
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case PncpMessageKind.PNCP_ANSWER: {
|
|
82
|
+
extra = {
|
|
83
|
+
oneofKind: "answer",
|
|
84
|
+
answer: {
|
|
85
|
+
replyTo: data.body.value.reply_to,
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
default: extra = { oneofKind: undefined };
|
|
91
|
+
}
|
|
92
|
+
let pncpMsg = {
|
|
93
|
+
id: data.id,
|
|
94
|
+
kind: data.body.type,
|
|
95
|
+
body: fromJsonValue(data.body.value.data),
|
|
96
|
+
extra,
|
|
97
|
+
};
|
|
98
|
+
return await this.directPncpMessage(pncpMsg);
|
|
99
|
+
}
|
|
100
|
+
async directPncpMessage(data) {
|
|
101
|
+
let response = await this.issue(data, BrokerClient.prototype.pncpMessageRequest);
|
|
102
|
+
return response;
|
|
103
|
+
}
|
|
104
|
+
// Creates a new pncp callback listener.
|
|
105
|
+
pncpListener() {
|
|
106
|
+
return new PncpListener(this);
|
|
107
|
+
}
|
|
108
|
+
observerListener(init) {
|
|
109
|
+
return new ParanetObserverListener(this, init);
|
|
110
|
+
}
|
|
111
|
+
async login(creds) {
|
|
112
|
+
let secret;
|
|
113
|
+
switch (creds.kind) {
|
|
114
|
+
case "token":
|
|
115
|
+
secret = { oneofKind: "token", token: creds.secret };
|
|
116
|
+
break;
|
|
117
|
+
case "password":
|
|
118
|
+
secret = { oneofKind: "password", password: creds.secret };
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
throw new Error("Invalid creds type");
|
|
122
|
+
}
|
|
123
|
+
let req = {
|
|
124
|
+
actorId: this.actorId,
|
|
125
|
+
secret,
|
|
126
|
+
};
|
|
127
|
+
let response = await this.issue(req, BrokerClient.prototype.login);
|
|
128
|
+
let token = response.token;
|
|
129
|
+
let refresh_token = response.refreshToken;
|
|
130
|
+
this.setTokens(token, refresh_token);
|
|
131
|
+
return { refresh_token, access_token: token };
|
|
132
|
+
}
|
|
133
|
+
async refreshAuth() {
|
|
134
|
+
if (!this.token || !this.refreshToken) {
|
|
135
|
+
throw new Error("Cannot refresh auth, no tokens set.");
|
|
136
|
+
}
|
|
137
|
+
let req = {
|
|
138
|
+
token: this.token,
|
|
139
|
+
refreshToken: this.refreshToken,
|
|
140
|
+
};
|
|
141
|
+
let response = await this.issue(req, BrokerClient.prototype.refreshToken);
|
|
142
|
+
let token = response.token;
|
|
143
|
+
let refreshToken = response.refreshToken;
|
|
144
|
+
this.setTokens(token, refreshToken);
|
|
145
|
+
return { refresh_token: refreshToken, access_token: token };
|
|
146
|
+
}
|
|
147
|
+
logout() {
|
|
148
|
+
this.setTokens();
|
|
149
|
+
}
|
|
150
|
+
setTokens(token, refresh) {
|
|
151
|
+
this.token = token;
|
|
152
|
+
this.refreshToken = refresh;
|
|
153
|
+
if (token && refresh) {
|
|
154
|
+
let decoded = jwtDecode(token);
|
|
155
|
+
this.tokenExp = decoded.exp ? new Date(decoded.exp * 1000) : undefined;
|
|
156
|
+
if (this.tokenCb) {
|
|
157
|
+
this.tokenCb(token, refresh);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
setTokenCb(cb) {
|
|
162
|
+
this.tokenCb = cb;
|
|
163
|
+
}
|
|
164
|
+
hasTokens() {
|
|
165
|
+
return this.token != undefined;
|
|
166
|
+
}
|
|
167
|
+
getTokens() {
|
|
168
|
+
return { token: this.token, refresh: this.refreshToken };
|
|
169
|
+
}
|
|
170
|
+
isTokenValid() {
|
|
171
|
+
if (!this.tokenExp) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
let now = new Date();
|
|
175
|
+
// Add one minute to now. This will make it so the token "expires" sooner than it really
|
|
176
|
+
// does, but allow a more clear indication that it is time to refresh.
|
|
177
|
+
now.setTime(now.getTime() + 1000 * 60);
|
|
178
|
+
return now < this.tokenExp;
|
|
179
|
+
}
|
|
180
|
+
getMetadata() {
|
|
181
|
+
return {
|
|
182
|
+
'x-actor-id': this.actorId,
|
|
183
|
+
...(this.token && { 'authorization': `Bearer ${this.token}` })
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
async issue(req, f) {
|
|
187
|
+
let md = this.getMetadata();
|
|
188
|
+
let response = f.bind(this.inner)(req, {
|
|
189
|
+
meta: md,
|
|
190
|
+
});
|
|
191
|
+
// Rejects if it failed.
|
|
192
|
+
await response.status;
|
|
193
|
+
let data = await response.response;
|
|
194
|
+
return data;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
export class ParanetListener {
|
|
198
|
+
constructor(client) {
|
|
199
|
+
this.shouldAutoReconnect = true;
|
|
200
|
+
this.client = client;
|
|
201
|
+
}
|
|
202
|
+
onmessage(f) {
|
|
203
|
+
this.msg = f;
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
onstart(f) {
|
|
207
|
+
this.start = f;
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
onerror(f) {
|
|
211
|
+
this.error = f;
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
ondisconnect(f) {
|
|
215
|
+
this.disconnected = f;
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
onreconnect(f) {
|
|
219
|
+
this.reconnected = f;
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
autoReconnect(v) {
|
|
223
|
+
this.shouldAutoReconnect = v;
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
connect() {
|
|
227
|
+
this.makeDecoratedStream(false);
|
|
228
|
+
}
|
|
229
|
+
disconnect() {
|
|
230
|
+
if (this.streamState) {
|
|
231
|
+
this.streamState.abort.abort("disconnect called.");
|
|
232
|
+
this.streamState = undefined;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
makeDecoratedStream(isReconnect, timeout = 100) {
|
|
236
|
+
if (this.streamState) {
|
|
237
|
+
this.disconnect();
|
|
238
|
+
}
|
|
239
|
+
// Set it up as a new one when we create a new stream.
|
|
240
|
+
let abort = new AbortController();
|
|
241
|
+
let { method, body } = this.getRequest();
|
|
242
|
+
let stream = method.bind(this.client.inner)(body, {
|
|
243
|
+
meta: this.client.getMetadata(),
|
|
244
|
+
abort: abort.signal
|
|
245
|
+
});
|
|
246
|
+
this.streamState = { stream, abort };
|
|
247
|
+
// Wait for the headers, indicating success.
|
|
248
|
+
// Note: This doesn't seem to work on firefox for some reason.
|
|
249
|
+
// Seems to be working fine in chrome though.
|
|
250
|
+
stream.headers.then((_headers) => {
|
|
251
|
+
this.start?.(this.client);
|
|
252
|
+
if (isReconnect) {
|
|
253
|
+
this.reconnected?.();
|
|
254
|
+
}
|
|
255
|
+
}).catch((err) => {
|
|
256
|
+
// We need this catch here because of retry looping.
|
|
257
|
+
// We don't need to call this.error on this because it will come through on the onError still.
|
|
258
|
+
});
|
|
259
|
+
// Handle the message stream finally.
|
|
260
|
+
stream.responses.onMessage((msg) => {
|
|
261
|
+
let unwrapped = this.getRes(msg);
|
|
262
|
+
if (unwrapped) {
|
|
263
|
+
this.msg?.(unwrapped, this.client);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
stream.responses.onError(async (err) => {
|
|
267
|
+
// Check if we aborted to know if we should retry or not.
|
|
268
|
+
this.disconnected?.();
|
|
269
|
+
if (!abort.signal.aborted) {
|
|
270
|
+
if (err.name == "RpcError") {
|
|
271
|
+
let rpcErr = err;
|
|
272
|
+
if (rpcErr.code == "UNAUTHENTICATED") {
|
|
273
|
+
try {
|
|
274
|
+
await this.client.refreshAuth();
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
this.error?.(err.message, true);
|
|
278
|
+
abort.abort("Unauthenticated");
|
|
279
|
+
// Exit early in this case.
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
this.error?.(err.message, false);
|
|
285
|
+
if (this.shouldAutoReconnect) {
|
|
286
|
+
// Increase timeout, saturated at 10000ms.
|
|
287
|
+
timeout = 2 * timeout + 1;
|
|
288
|
+
if (timeout > 10000)
|
|
289
|
+
timeout = 10000;
|
|
290
|
+
setTimeout(() => this.makeDecoratedStream(true, timeout), timeout);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
// Semantically, this should never happen for a paranet connection, however, it might
|
|
295
|
+
// at some point in the future, so we should handle this case appropriately.
|
|
296
|
+
stream.responses.onComplete(() => {
|
|
297
|
+
this.disconnected?.();
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
export class PncpListener extends ParanetListener {
|
|
302
|
+
constructor(client) {
|
|
303
|
+
super(client);
|
|
304
|
+
}
|
|
305
|
+
getRequest() {
|
|
306
|
+
return {
|
|
307
|
+
method: BrokerClient.prototype.pncpCallbackStream,
|
|
308
|
+
body: {
|
|
309
|
+
disableSkillForwarding: false,
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
getRes(t) {
|
|
314
|
+
if (t.body.oneofKind == undefined)
|
|
315
|
+
return null;
|
|
316
|
+
return t;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
export class ParanetObserverListener extends ParanetListener {
|
|
320
|
+
constructor(client, init) {
|
|
321
|
+
super(client);
|
|
322
|
+
this.init = init;
|
|
323
|
+
}
|
|
324
|
+
getRequest() {
|
|
325
|
+
return {
|
|
326
|
+
method: BrokerClient.prototype.createObserverStream,
|
|
327
|
+
body: this.init,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
getRes(t) {
|
|
331
|
+
if (t.body)
|
|
332
|
+
return t.body;
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
export class BaseClient {
|
|
337
|
+
constructor(init) {
|
|
338
|
+
this.endpoint = cleanUrl(init.endpoint);
|
|
339
|
+
this.actorId = init.actorId;
|
|
340
|
+
}
|
|
341
|
+
getEndpoint() {
|
|
342
|
+
return this.endpoint;
|
|
343
|
+
}
|
|
344
|
+
// Logs in this client and stores the token on the client.
|
|
345
|
+
async login(secret) {
|
|
346
|
+
let id = this.actorId.split('@')[0];
|
|
347
|
+
console.log(secret);
|
|
348
|
+
let response = await this.postJson("login", {
|
|
349
|
+
id,
|
|
350
|
+
...secret,
|
|
351
|
+
});
|
|
352
|
+
this.setTokens(response.access_token, response.refresh_token);
|
|
353
|
+
return response;
|
|
354
|
+
}
|
|
355
|
+
async refreshAuth() {
|
|
356
|
+
let tokens = await this.postJson("token/refresh", {
|
|
357
|
+
token: this.token,
|
|
358
|
+
refresh_token: this.refreshToken,
|
|
359
|
+
});
|
|
360
|
+
this.setTokens(tokens.access_token, tokens.refresh_token);
|
|
361
|
+
return tokens;
|
|
362
|
+
}
|
|
363
|
+
logout() {
|
|
364
|
+
this.setTokens();
|
|
365
|
+
}
|
|
366
|
+
setTokens(token, refresh) {
|
|
367
|
+
this.token = token;
|
|
368
|
+
this.refreshToken = refresh;
|
|
369
|
+
if (token && refresh) {
|
|
370
|
+
let decoded = jwtDecode(token);
|
|
371
|
+
this.tokenExp = decoded.exp ? new Date(decoded.exp * 1000) : undefined;
|
|
372
|
+
if (this.tokenCb) {
|
|
373
|
+
this.tokenCb(token, refresh);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
setTokenCb(cb) {
|
|
378
|
+
this.tokenCb = cb;
|
|
379
|
+
}
|
|
380
|
+
hasTokens() {
|
|
381
|
+
return this.token != undefined;
|
|
382
|
+
}
|
|
383
|
+
getTokens() {
|
|
384
|
+
return { token: this.token, refresh: this.refreshToken };
|
|
385
|
+
}
|
|
386
|
+
isTokenValid() {
|
|
387
|
+
if (!this.tokenExp) {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
let now = new Date();
|
|
391
|
+
// Add one minute to now. This will make it so the token "expires" sooner than it really
|
|
392
|
+
// does, but allow a more clear indication that it is time to refresh.
|
|
393
|
+
now.setTime(now.getTime() + 1000 * 60);
|
|
394
|
+
return now < this.tokenExp;
|
|
395
|
+
}
|
|
396
|
+
async postJson(path, body) {
|
|
397
|
+
let response = await this.post_(path, JSON.stringify(body), { "content-type": "application/json" });
|
|
398
|
+
return await response.json();
|
|
399
|
+
}
|
|
400
|
+
async post_(path, body, headers) {
|
|
401
|
+
let response = await this.fetch(path, "POST", body, headers);
|
|
402
|
+
return response;
|
|
403
|
+
}
|
|
404
|
+
async get(path) {
|
|
405
|
+
let response = await this.fetch(path, "GET");
|
|
406
|
+
return await response.json();
|
|
407
|
+
}
|
|
408
|
+
async fetch(path, method, body, headers) {
|
|
409
|
+
let url = `${this.endpoint}/${path}`;
|
|
410
|
+
const mkFetch = () => fetch(url, {
|
|
411
|
+
method,
|
|
412
|
+
body,
|
|
413
|
+
headers: {
|
|
414
|
+
...(headers || {}),
|
|
415
|
+
...this.getHeaders(),
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
let response = await mkFetch();
|
|
419
|
+
if (response.status == 401) {
|
|
420
|
+
await this.refreshAuth();
|
|
421
|
+
response = await mkFetch();
|
|
422
|
+
}
|
|
423
|
+
if (response.status != 200) {
|
|
424
|
+
let text = await response.text();
|
|
425
|
+
throw new Error(`Failed to read from endpoint "${url}" (${response.status}): ${text} `);
|
|
426
|
+
}
|
|
427
|
+
return response;
|
|
428
|
+
}
|
|
429
|
+
getHeaders() {
|
|
430
|
+
return {
|
|
431
|
+
"X-ACTOR-ID": this.actorId,
|
|
432
|
+
...(this.token && { Authorization: `Bearer ${this.token}` }),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
export class ParanetServiceClient extends BaseClient {
|
|
437
|
+
constructor(init) {
|
|
438
|
+
super(init);
|
|
439
|
+
}
|
|
440
|
+
serviceEndpoint() {
|
|
441
|
+
return this.endpoint;
|
|
442
|
+
}
|
|
443
|
+
// TODO fill in a specific type.
|
|
444
|
+
// TODO: At a higher level than this we need a schema manager which properly parses
|
|
445
|
+
// schemas and caches them.
|
|
446
|
+
async schemaFetch(path) {
|
|
447
|
+
return await this.get(`schema/${path}`);
|
|
448
|
+
}
|
|
449
|
+
async documentFetch({ name, id }) {
|
|
450
|
+
if (name) {
|
|
451
|
+
return await this.fetch(`document/fetch/name/${name}`, "GET");
|
|
452
|
+
}
|
|
453
|
+
if (id) {
|
|
454
|
+
return await this.fetch(`document/fetch/id/${id}`, "GET");
|
|
455
|
+
}
|
|
456
|
+
throw new Error("Must provide either a name or id to fetch a document.");
|
|
457
|
+
}
|
|
458
|
+
async documentUpload(data, args) {
|
|
459
|
+
const formData = new FormData();
|
|
460
|
+
formData.append("file", data);
|
|
461
|
+
let response = null;
|
|
462
|
+
if (args?.name) {
|
|
463
|
+
const url = `document/upload/name/${args.name}`;
|
|
464
|
+
response = await this.post_(url, formData);
|
|
465
|
+
}
|
|
466
|
+
else if (args?.id) {
|
|
467
|
+
const url = `document/upload/id/${args.id}`;
|
|
468
|
+
response = await this.post_(url, formData);
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
response = await this.post_("document/upload", formData);
|
|
472
|
+
}
|
|
473
|
+
return await response.json();
|
|
474
|
+
}
|
|
475
|
+
async graphql(query, inputs) {
|
|
476
|
+
let response = await this.postJson("graphql", {
|
|
477
|
+
query,
|
|
478
|
+
variables: inputs,
|
|
479
|
+
});
|
|
480
|
+
return response;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
export class SysClient extends BaseClient {
|
|
484
|
+
constructor(init) {
|
|
485
|
+
super(init);
|
|
486
|
+
}
|
|
487
|
+
async nodeInfo() {
|
|
488
|
+
return await this.get('node-info');
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// Some util functions.
|
|
492
|
+
function cleanUrl(url) {
|
|
493
|
+
if (url.endsWith("/")) {
|
|
494
|
+
url = url.slice(0, url.length - 1);
|
|
495
|
+
}
|
|
496
|
+
return url;
|
|
497
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PncpMessageObject, PncpRequestObject, PncpPacketObject } from "./pncp";
|
|
2
|
+
import * as schema from "./schema/paranet";
|
|
3
|
+
import { ConversationId } from "./proto/identifiers";
|
|
4
|
+
export declare function gqlSimplifyPncpPacket(req: schema.PncpPacket): PncpPacketObject;
|
|
5
|
+
export declare function gqlSimplifyPncpRequest(req: schema.PncpRequest): PncpRequestObject;
|
|
6
|
+
export declare function gqlSimplifyPncpMessage(req: schema.PncpMessage): PncpMessageObject;
|
|
7
|
+
export declare function splitConversationId(convId: string): ConversationId;
|
|
8
|
+
//# sourceMappingURL=graphql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,iBAAiB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AACjG,OAAO,KAAK,MAAM,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EAAE,cAAc,EAA0B,MAAM,qBAAqB,CAAC;AAE7E,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,gBAAgB,CAK9E;AAGD,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,CAAC,WAAW,GAAG,iBAAiB,CAejF;AAUD,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,CAAC,WAAW,GAAG,iBAAiB,CAejF;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAWlE"}
|