@ignite-ember/extension-bridge 0.0.0-stage → 0.2.1
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +364 -2
- package/dist/bridge.d.ts +37 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +96 -0
- package/dist/bridge.js.map +1 -0
- package/dist/channel.d.ts +26 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +36 -0
- package/dist/channel.js.map +1 -0
- package/dist/contract.d.ts +137 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +9 -0
- package/dist/contract.js.map +1 -0
- package/dist/correlator.d.ts +37 -0
- package/dist/correlator.d.ts.map +1 -0
- package/dist/correlator.js +75 -0
- package/dist/correlator.js.map +1 -0
- package/dist/guestBridge.d.ts +63 -0
- package/dist/guestBridge.d.ts.map +1 -0
- package/dist/guestBridge.js +156 -0
- package/dist/guestBridge.js.map +1 -0
- package/dist/hostModel.d.ts +159 -0
- package/dist/hostModel.d.ts.map +1 -0
- package/dist/hostModel.generated.d.ts +2373 -0
- package/dist/hostModel.generated.d.ts.map +1 -0
- package/dist/hostModel.generated.js +1746 -0
- package/dist/hostModel.generated.js.map +1 -0
- package/dist/hostModel.js +172 -0
- package/dist/hostModel.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.d.ts +348 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +145 -0
- package/dist/protocol.js.map +1 -0
- package/dist/service.d.ts +168 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +196 -0
- package/dist/service.js.map +1 -0
- package/dist/signals.d.ts +21 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +52 -0
- package/dist/signals.js.map +1 -0
- package/dist/tokens.d.ts +101 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +164 -0
- package/dist/tokens.js.map +1 -0
- package/dist/watchers.d.ts +12 -0
- package/dist/watchers.d.ts.map +1 -0
- package/dist/watchers.js +18 -0
- package/dist/watchers.js.map +1 -0
- package/package.json +53 -4
- package/src/bridge.ts +116 -0
- package/src/channel.ts +48 -0
- package/src/contract.ts +157 -0
- package/src/correlator.ts +88 -0
- package/src/guestBridge.ts +261 -0
- package/src/hostModel.generated.ts +3155 -0
- package/src/hostModel.ts +213 -0
- package/src/index.ts +11 -0
- package/src/protocol.ts +485 -0
- package/src/service.ts +278 -0
- package/src/signals.ts +65 -0
- package/src/tokens.ts +171 -0
- package/src/watchers.ts +18 -0
package/src/hostModel.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pieces `bridge.host` is built from: objects, properties, signals,
|
|
3
|
+
* calls. `hostModel.generated.ts` is generated from igni's host schema
|
|
4
|
+
* and is made of these.
|
|
5
|
+
*
|
|
6
|
+
* await bridge.host.chat.processing.get(); // read a property
|
|
7
|
+
* await bridge.host.sessions.current.set(id); // write one
|
|
8
|
+
* await bridge.host.chat.post("Done."); // call a method
|
|
9
|
+
* bridge.on(bridge.host.chat.run_completed, (run) => { … });
|
|
10
|
+
*
|
|
11
|
+
* How a request reaches igni is the `HostLink`'s business — the bridge
|
|
12
|
+
* forwards it to the window this page is framed in, which forwards it to
|
|
13
|
+
* the host. Names are the schema's, snake_case, in both SDKs.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Any JSON value: the host's report of something not yet typed. */
|
|
17
|
+
export type JsonValue = unknown;
|
|
18
|
+
|
|
19
|
+
/** Why the host refused or failed — the same codes the Python SDK raises. */
|
|
20
|
+
export type HostErrorCode =
|
|
21
|
+
| "not_granted"
|
|
22
|
+
| "not_found"
|
|
23
|
+
| "invalid"
|
|
24
|
+
| "read_only"
|
|
25
|
+
| "stage_2"
|
|
26
|
+
| "refused"
|
|
27
|
+
| "unavailable"
|
|
28
|
+
| "failed";
|
|
29
|
+
|
|
30
|
+
/** The host said no, or it went wrong there. `code` is for code; the message, for people. */
|
|
31
|
+
export class HostError extends Error {
|
|
32
|
+
/**
|
|
33
|
+
* The failure, as the host reported it.
|
|
34
|
+
*
|
|
35
|
+
* @param code — why it failed: one of the {@link HostErrorCode}s, or
|
|
36
|
+
* whatever the host said.
|
|
37
|
+
* @param message — a sentence for a person to read.
|
|
38
|
+
*/
|
|
39
|
+
constructor(
|
|
40
|
+
/** Why it failed: one of the {@link HostErrorCode}s, or the host's own. */
|
|
41
|
+
readonly code: HostErrorCode | string,
|
|
42
|
+
message: string,
|
|
43
|
+
) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "HostError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How requests reach the igni host. */
|
|
50
|
+
export interface HostLink {
|
|
51
|
+
/**
|
|
52
|
+
* Send one request to the host.
|
|
53
|
+
*
|
|
54
|
+
* @param op — what to do: read a property, write one, or call a method.
|
|
55
|
+
* @param path — where in the model, as `chat.processing`: the path without the leading `host.`.
|
|
56
|
+
* @param options — the window or session it is about, the arguments
|
|
57
|
+
* of a call, and the value of a write.
|
|
58
|
+
* @returns — whatever the host answered with.
|
|
59
|
+
*/
|
|
60
|
+
request(
|
|
61
|
+
op: "get" | "set" | "call",
|
|
62
|
+
path: string,
|
|
63
|
+
options: {
|
|
64
|
+
/** The window to ask about, when it is not this page's own. */
|
|
65
|
+
window?: string | null;
|
|
66
|
+
/** The session to ask about, when it is not the one on screen. */
|
|
67
|
+
session?: string | null;
|
|
68
|
+
/** The arguments of a call, by name. */
|
|
69
|
+
params?: Record<string, unknown>;
|
|
70
|
+
/** The value of a write. */
|
|
71
|
+
value?: unknown;
|
|
72
|
+
},
|
|
73
|
+
): Promise<unknown>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** One object of the host model: `host.chat`, `host.windows.ui(id)`, `host.sessions.chat(id)`. */
|
|
77
|
+
export class HostObject {
|
|
78
|
+
/**
|
|
79
|
+
* One object of the model, bound to a host.
|
|
80
|
+
*
|
|
81
|
+
* A page never builds one: `bridge.host` is already the right one,
|
|
82
|
+
* and a second would be a second conversation.
|
|
83
|
+
*
|
|
84
|
+
* @param link — how requests reach the host.
|
|
85
|
+
* @param path — where in the model this object sits.
|
|
86
|
+
* @param window — the window it belongs to, when it belongs to one.
|
|
87
|
+
* @param session — the session it belongs to, when it belongs to one.
|
|
88
|
+
*/
|
|
89
|
+
constructor(
|
|
90
|
+
/** How requests reach the host. */
|
|
91
|
+
protected readonly link: HostLink,
|
|
92
|
+
/** Where in the model this object sits: `chat`, without the leading `host.`. */
|
|
93
|
+
readonly path: string,
|
|
94
|
+
/** The window it belongs to, when it belongs to one. */
|
|
95
|
+
protected readonly window: string | null = null,
|
|
96
|
+
/** The session it belongs to, when it belongs to one. */
|
|
97
|
+
protected readonly session: string | null = null,
|
|
98
|
+
) {}
|
|
99
|
+
|
|
100
|
+
/** @internal */
|
|
101
|
+
memberPath(name: string): string {
|
|
102
|
+
return this.path ? `${this.path}.${name}` : name;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @internal — for `Property`. */
|
|
106
|
+
hostLink(): HostLink {
|
|
107
|
+
return this.link;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @internal — for `Property`. */
|
|
111
|
+
windowId(): string | null {
|
|
112
|
+
return this.window;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @internal — for `Property`. */
|
|
116
|
+
sessionId(): string | null {
|
|
117
|
+
return this.session;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Call a method on the host.
|
|
122
|
+
*
|
|
123
|
+
* @param name — the method's name, as the schema spells it.
|
|
124
|
+
* @param params — its arguments, by name.
|
|
125
|
+
* @returns — whatever the host answered with.
|
|
126
|
+
*/
|
|
127
|
+
protected async call<T>(
|
|
128
|
+
name: string,
|
|
129
|
+
params: Record<string, unknown>,
|
|
130
|
+
): Promise<T> {
|
|
131
|
+
const sent: Record<string, unknown> = {};
|
|
132
|
+
for (const [key, value] of Object.entries(params)) {
|
|
133
|
+
if (value !== undefined && value !== null) sent[key] = value;
|
|
134
|
+
}
|
|
135
|
+
return (await this.link.request("call", this.memberPath(name), {
|
|
136
|
+
window: this.window,
|
|
137
|
+
session: this.session,
|
|
138
|
+
params: sent,
|
|
139
|
+
})) as T;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** A value of a host object: `get()` reads it, `set()` writes it. */
|
|
144
|
+
export class Property<T> {
|
|
145
|
+
/**
|
|
146
|
+
* One value of one host object.
|
|
147
|
+
*
|
|
148
|
+
* @param owner — the object it belongs to.
|
|
149
|
+
* @param name — its name, as the schema spells it.
|
|
150
|
+
* @param writable — whether it may be written.
|
|
151
|
+
*/
|
|
152
|
+
constructor(
|
|
153
|
+
/** The object it belongs to. */
|
|
154
|
+
private readonly owner: HostObject,
|
|
155
|
+
/** Its name, as the schema spells it. */
|
|
156
|
+
readonly name: string,
|
|
157
|
+
/** Whether it may be written. */
|
|
158
|
+
readonly writable: boolean,
|
|
159
|
+
) {}
|
|
160
|
+
|
|
161
|
+
/** Where in the model this value sits: `chat.processing`, without the leading `host.`. */
|
|
162
|
+
get path(): string {
|
|
163
|
+
return this.owner.memberPath(this.name);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Read it.
|
|
168
|
+
*
|
|
169
|
+
* @returns — the value the host holds right now.
|
|
170
|
+
*/
|
|
171
|
+
async get(): Promise<T> {
|
|
172
|
+
return (await this.owner.hostLink().request("get", this.path, {
|
|
173
|
+
window: this.owner.windowId(),
|
|
174
|
+
session: this.owner.sessionId(),
|
|
175
|
+
})) as T;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Write it, as the person's UI would. Refused where theirs would be.
|
|
180
|
+
*
|
|
181
|
+
* @param value — what the host should hold for it from now on.
|
|
182
|
+
*/
|
|
183
|
+
async set(value: T): Promise<void> {
|
|
184
|
+
if (!this.writable) {
|
|
185
|
+
throw new HostError("read_only", `host.${this.path} cannot be set`);
|
|
186
|
+
}
|
|
187
|
+
await this.owner.hostLink().request("set", this.path, {
|
|
188
|
+
window: this.owner.windowId(),
|
|
189
|
+
session: this.owner.sessionId(),
|
|
190
|
+
value,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* One signal of one host object; `T` is what it carries. `name` is its
|
|
197
|
+
* wire name — `chat.run_completed`, or for the four events that predate
|
|
198
|
+
* the model their topic strings (`session.switched`, …).
|
|
199
|
+
*/
|
|
200
|
+
export class Signal<T> {
|
|
201
|
+
/** Only for the type parameter; never set. */
|
|
202
|
+
declare readonly carries?: T;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* One signal of one host object.
|
|
206
|
+
*
|
|
207
|
+
* @param name — its wire name, as the host sends it.
|
|
208
|
+
*/
|
|
209
|
+
constructor(
|
|
210
|
+
/** Its wire name, as the host sends it: `chat.run_completed`. */
|
|
211
|
+
readonly name: string,
|
|
212
|
+
) {}
|
|
213
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an extension's page imports to reach igni: `connect()`, and the
|
|
3
|
+
* types of what it hands back. The wire format (`protocol`) and the
|
|
4
|
+
* design tokens (`tokens`) are subpaths, because the host imports them
|
|
5
|
+
* too and an extension rarely needs them directly.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./bridge.js";
|
|
8
|
+
export * from "./contract.js";
|
|
9
|
+
export * from "./hostModel.js";
|
|
10
|
+
export * from "./hostModel.generated.js";
|
|
11
|
+
export * from "./service.js";
|