@xynolog/sdk 0.0.0-stage → 0.8.0
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 +201 -0
- package/README.md +408 -2
- package/dist/chunk-KMB5JOFQ.js +4551 -0
- package/dist/chunk-KMB5JOFQ.js.map +1 -0
- package/dist/index.d.ts +3453 -0
- package/dist/index.js +1815 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.d.ts +104 -0
- package/dist/testing.js +116 -0
- package/dist/testing.js.map +1 -0
- package/package.json +70 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,409 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xynolog/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/xynolog/sdk/actions/workflows/ci.yml?query=branch%3Amain+is%3Asuccess+job%3A%22Test+sdk%22)
|
|
4
|
+
[](https://app.codecov.io/gh/xynolog/sdk/components)
|
|
5
|
+
[](https://www.npmjs.com/package/@xynolog/sdk)
|
|
6
|
+
[](https://www.npmjs.com/package/@xynolog/sdk)
|
|
7
|
+
[](https://github.com/xynolog/sdk/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
The TypeScript SDK for [Xynolog](https://github.com/xynolog/sdk), an open platform for building
|
|
10
|
+
trusted AI knowledge bases: a shared workspace where humans and AI agents
|
|
11
|
+
annotate, connect and govern a corpus of documents.
|
|
12
|
+
|
|
13
|
+
This package is the client. With it a program reads and writes one knowledge
|
|
14
|
+
base: it adds resources, annotates and links them, searches them, gathers
|
|
15
|
+
context for a model, and hears what the other participants do as they do it.
|
|
16
|
+
A person's application and an AI agent use the same client. The SDK does not
|
|
17
|
+
tell them apart.
|
|
18
|
+
|
|
19
|
+
It has the same namespaces, methods and behaviour as the
|
|
20
|
+
[Rust SDK](https://github.com/xynolog/sdk/tree/main/rust/sdk) ([`xynolog`](https://crates.io/crates/xynolog) on crates.io)
|
|
21
|
+
the [Python SDK](https://github.com/xynolog/sdk/tree/main/python/sdk) ([`xynolog`](https://pypi.org/project/xynolog/) on PyPI)
|
|
22
|
+
the [Go SDK](https://github.com/xynolog/sdk/tree/main/go) ([`github.com/xynolog/sdk/go`](https://pkg.go.dev/github.com/xynolog/sdk/go) on pkg.go.dev)
|
|
23
|
+
and the [Kotlin SDK](https://github.com/xynolog/sdk/tree/main/kotlin/sdk), for Kotlin and Java programs,
|
|
24
|
+
and the five are held to the same [conformance suite](https://github.com/xynolog/protocol/tree/main/conformance/sdk).
|
|
25
|
+
New to Xynolog? The [Introduction](https://github.com/xynolog/docs/blob/main/builder/INTRODUCTION.md)
|
|
26
|
+
explains the domain and the ideas the API falls out of.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @xynolog/sdk
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
It runs in a browser and in Node. In a browser it needs Chrome 116, Firefox 124 or Safari 17.4,
|
|
35
|
+
or later: its transport joins abort signals with `AbortSignal.any`, which each of those was the
|
|
36
|
+
first version to have. A client speaks to a running knowledge base.
|
|
37
|
+
The [Quick Start](https://github.com/xynolog/docs/blob/main/builder/QUICK-START.md) sets one up on
|
|
38
|
+
your own machine.
|
|
39
|
+
|
|
40
|
+
## Connect
|
|
41
|
+
|
|
42
|
+
Sign-in happens at the knowledge base's identity provider, never at the gateway. A script uses
|
|
43
|
+
the device grant: it prints a URL, the person approves in any browser, and the sign-in is kept
|
|
44
|
+
where the script says, in a `SignInKept`. `XynologSession.fromKept` then opens a session over
|
|
45
|
+
it, which owns the token lifecycle (proactive refresh at the issuer, each renewal kept where the
|
|
46
|
+
sign-in is, disposal). There is no client-level signIn: the issuer decides how long an access
|
|
47
|
+
token lives and it is short — minutes, not hours — so anything that outlives one token needs a
|
|
48
|
+
session to renew it.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { MemorySignIn, XynologSession, httpKb } from '@xynolog/sdk';
|
|
52
|
+
|
|
53
|
+
const kb = httpKb({ id: 'my-watcher', label: 'My Watcher',
|
|
54
|
+
host: 'localhost', port: 4000, protocol: 'http' });
|
|
55
|
+
const kept = new MemorySignIn();
|
|
56
|
+
await XynologSession.signInDevice({
|
|
57
|
+
kb,
|
|
58
|
+
kept,
|
|
59
|
+
onCode: ({ verificationUri, userCode }) => console.log(`Open ${verificationUri} and enter ${userCode}`),
|
|
60
|
+
});
|
|
61
|
+
const opened = await XynologSession.fromKept({ kb, kept });
|
|
62
|
+
const { resources } = await opened.session.client.browse.resources({ limit: 10 }).fresh();
|
|
63
|
+
opened.dispose(); // the client it was built with, then the session
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`fromKept` gives an `OpenedSession`: the session, and the client it was built with. The session
|
|
67
|
+
only uses its client, so the two end together. The session asks the gateway who its token is,
|
|
68
|
+
and so learns who is signed in, unless it is given `validate: false`. What goes wrong, it says
|
|
69
|
+
on `session.errors$`, its one stream of it: its own errors (a `XynologSessionError`, such as
|
|
70
|
+
`session.refresh-exhausted`, or `session.save-refused` for a write its storage refused) and its
|
|
71
|
+
client's transport failures, each once, until it ends.
|
|
72
|
+
|
|
73
|
+
Where the sign-in is kept decides what a restart does. A `MemorySignIn` lives as long as the
|
|
74
|
+
process. `keptIn(storage, kb.id)` keeps it in a `SessionStorage` under `kb.id`, so distinct
|
|
75
|
+
scripts sharing one storage use distinct ids. An entry of the `SignInStore` is the sign-in
|
|
76
|
+
`xynolog login` keeps (below).
|
|
77
|
+
|
|
78
|
+
Already hold tokens? Keep the access and refresh pair in a `MemorySignIn` and open it with
|
|
79
|
+
`XynologSession.fromKept(...)`. `XynologSession.fromHttp(...)` takes a bare access token and the
|
|
80
|
+
`refresh` that renews it, which is given the token it renews, and gives an
|
|
81
|
+
`OpenedSession` too. A holder of tokens that builds no session over them renews a kept
|
|
82
|
+
sign-in with `renewKept(kept, holding)`.
|
|
83
|
+
`XynologClient.fromHttp({ baseUrl, token })` takes a bare token and never renews it, which suits
|
|
84
|
+
a one-shot script that finishes inside one token's life.
|
|
85
|
+
|
|
86
|
+
### The sign-in `xynolog login` keeps
|
|
87
|
+
|
|
88
|
+
A person who has run `xynolog login` has a sign-in the launcher keeps for each stack, in
|
|
89
|
+
`tokens.json` in the launcher's state home
|
|
90
|
+
([the sign-in store](https://github.com/xynolog/protocol/tree/main/specs/src/sign-in-store)).
|
|
91
|
+
A `SignInStore` is that file, and `entry(key)` is where the sign-in of the stack `key` is kept.
|
|
92
|
+
`XynologSession.fromKept` builds a session over an entry; with nothing kept there, the session is
|
|
93
|
+
signed out. The session renews the sign-in by the contract's rules and keeps each renewal in the
|
|
94
|
+
file, so one sign-in serves the launcher and the script. An entry keeps only what the launcher's
|
|
95
|
+
script client was issued, and records no person, so a session over it keeps no caches.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { XynologSession, SignInStore, httpKb } from '@xynolog/sdk';
|
|
99
|
+
|
|
100
|
+
const opened = await XynologSession.fromKept({
|
|
101
|
+
kb: httpKb({ id: 'local:example.org-my-kb', label: 'My KB',
|
|
102
|
+
host: 'localhost', port: 4000, protocol: 'http' }),
|
|
103
|
+
kept: new SignInStore(signInFiles).entry('local:example.org-my-kb'),
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The SDK reaches no file and takes no lock. The program gives the store a `SignInFiles` over
|
|
108
|
+
the state home's directory. `stateDir` computes that directory from the system and from what
|
|
109
|
+
HOME (USERPROFILE on Windows), XDG_STATE_HOME and LOCALAPPDATA say. The store tells each member
|
|
110
|
+
which file to use, and gives it the mode, the access list and the Windows offset. To share the
|
|
111
|
+
file with the launcher and the other SDKs, each member must do the following:
|
|
112
|
+
|
|
113
|
+
- **`lock`** takes an exclusive lock and waits until it holds it. On Unix that is `flock(2)`,
|
|
114
|
+
never `fcntl` or `lockf`, whose locks do not exclude a `flock` on Linux. On Windows it is
|
|
115
|
+
`LockFileEx` on the one byte at the offset given, or on the whole file. It opens the lock
|
|
116
|
+
file for reading and writing, makes it with the mode given when it is not there, and never
|
|
117
|
+
renames or removes it. It returns the function that lets the lock go.
|
|
118
|
+
- **`keepToOwner`** gives the directory the mode given. On Windows it gives the directory the
|
|
119
|
+
access list for the SID of the user the process runs as.
|
|
120
|
+
- **`read`** returns null for a file that is not there, and throws on any other failure.
|
|
121
|
+
- **`replace`** writes a temporary file beside the document with the mode given, and renames it
|
|
122
|
+
over the document. It removes the temporary file when that fails.
|
|
123
|
+
|
|
124
|
+
Each member is synchronous, as a `SignInKept` is. The store holds the lock from before its read
|
|
125
|
+
until after its rename, and never asks for it while it holds it.
|
|
126
|
+
|
|
127
|
+
To sign out, `signOut(kept)` revokes the refresh token at the issuer, when the issuer says where,
|
|
128
|
+
and forgets the sign-in whatever the issuer answers.
|
|
129
|
+
|
|
130
|
+
Every request these make, of the gateway and of the issuer, is sent with the `fetch` they are
|
|
131
|
+
given, or with the runtime's own when they are given none. `signInDevice`, `fromKept`,
|
|
132
|
+
`fromHttp`, `startAgentSession` and `XynologBrowser` take it in their options as `fetch`;
|
|
133
|
+
`signOut`, `createHttpSessionFactory` and `httpDiscovery` take it as their last argument. The certificates a
|
|
134
|
+
request trusts, and the proxy it goes through, are that fetch's: the SDK reads no setting of its
|
|
135
|
+
own for either. In Node, a gateway whose certificate another authority signed is reached with
|
|
136
|
+
undici's `fetch` sent through an `Agent` that trusts that authority; in a browser page, the
|
|
137
|
+
browser decides.
|
|
138
|
+
[`@xynolog/http-transport`](https://github.com/xynolog/sdk/tree/main/typescript/http-transport#the-fetch-it-sends-with)
|
|
139
|
+
says which requests that covers.
|
|
140
|
+
|
|
141
|
+
## Eight verbs
|
|
142
|
+
|
|
143
|
+
Every operation belongs to one of eight flows: verbs for what a participant
|
|
144
|
+
does with a shared corpus. Four write, three read, and one directs
|
|
145
|
+
attention. Each is a namespace of the client.
|
|
146
|
+
|
|
147
|
+
| | Verb | What it does | Among its methods |
|
|
148
|
+
|---|---|---|---|
|
|
149
|
+
| Writing | `yield` | Introduce a resource, uploaded or generated from gathered context | `yield.resource`, `yield.delegate` |
|
|
150
|
+
| | `mark` | Annotate a resource | `mark.annotation`, `mark.delegate`, `mark.updateEntityTypes`, `mark.archive` |
|
|
151
|
+
| | `bind` | Resolve an ambiguous reference to a specific resource | `bind.body`, `bind.initiate` |
|
|
152
|
+
| | `frame` | Define and grow the schema vocabulary | `frame.addEntityTypes`, `frame.addTagSchema` |
|
|
153
|
+
| Reading | `browse` | Navigate, read and observe, including the knowledge base's engines | `browse.resource`, `browse.annotations`, `browse.engines`, `browse.click` |
|
|
154
|
+
| | `match` | Search the corpus: resources by text, and candidates for a reference | `match.resources`, `match.search` |
|
|
155
|
+
| | `gather` | Assemble grounding context around a resource or an annotation, and list what refers to a resource | `gather.resource`, `gather.annotation`, `gather.referencedBy` |
|
|
156
|
+
| Attention | `beckon` | Direct attention across participants | `beckon.hover`, `beckon.sparkle`, `beckon.openResource` |
|
|
157
|
+
|
|
158
|
+
Beside the eight are `job`, and `auth` and `system` when the client has a
|
|
159
|
+
gateway. What each flow means is in
|
|
160
|
+
[protocol/contract/flows](https://github.com/xynolog/protocol/tree/main/contract/flows).
|
|
161
|
+
|
|
162
|
+
## A first program
|
|
163
|
+
|
|
164
|
+
From a signed-in session, this ingests a paper, has a model mark the concepts
|
|
165
|
+
it mentions, gathers the context around it, and generates a summary grounded
|
|
166
|
+
in that context. Those are the [Quick Start](https://github.com/xynolog/docs/blob/main/builder/QUICK-START.md)'s
|
|
167
|
+
last two steps, from code, and the two that come next. [Connect](#connect) is
|
|
168
|
+
how a session comes to be signed in.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
// Ingest: the paper's bytes become a resource.
|
|
172
|
+
const { resourceId } = await session.client.yield.resource({
|
|
173
|
+
name: 'Attention Is All You Need',
|
|
174
|
+
file, // a browser File or a Node Buffer
|
|
175
|
+
format: 'application/pdf',
|
|
176
|
+
storageUri: 'file://papers/attention-is-all-you-need.pdf',
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Annotate: a model reads it and marks each mention of a concept.
|
|
180
|
+
await session.client.mark.delegate(resourceId, { motivation: 'linking', entityTypes: ['Concept'] });
|
|
181
|
+
|
|
182
|
+
// Gather: the paper, its annotations, and what the knowledge base holds around it.
|
|
183
|
+
const context = await session.client.gather.resource(resourceId);
|
|
184
|
+
|
|
185
|
+
// Generate: a new resource, grounded in that context and linked to its source.
|
|
186
|
+
const done = await session.client.yield.delegate({
|
|
187
|
+
title: 'Attention Is All You Need: a summary',
|
|
188
|
+
storageUri: 'file://generated/attention-summary.md',
|
|
189
|
+
context,
|
|
190
|
+
task: 'summary',
|
|
191
|
+
});
|
|
192
|
+
if (done.result && 'resourceId' in done.result) {
|
|
193
|
+
console.log('The summary is', done.result.resourceId);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
opened.dispose(); // what the sign-in gave (Connect): its client, then the session
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Both resources, and every annotation the model made, are in the knowledge base
|
|
200
|
+
for the next participant, person or agent, to read and build on.
|
|
201
|
+
|
|
202
|
+
Where to go from here:
|
|
203
|
+
|
|
204
|
+
- The [Developer Guide](https://github.com/xynolog/docs/blob/main/builder/DEVELOPER-GUIDE.md) has
|
|
205
|
+
each of these steps as a recipe, and the ones after them: reading, searching,
|
|
206
|
+
annotating by hand, reacting to what others do, and testing.
|
|
207
|
+
- The [agent skills](https://github.com/xynolog/docs/tree/main/builder/skills) are whole scripts on
|
|
208
|
+
this SDK, one per task, for an AI coding assistant to load: ingesting a
|
|
209
|
+
corpus, annotating it, linking it, and the layers built on those.
|
|
210
|
+
|
|
211
|
+
## One call, two ways to consume
|
|
212
|
+
|
|
213
|
+
Every long-lived value is an `Observable` with an explicit one-shot path — from the same
|
|
214
|
+
call, take the value once or keep it live:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
const resource = await client.browse.resource(rId).fresh(); // one-shot fresh read — no rxjs import
|
|
218
|
+
client.browse.resource(rId).subscribe((st) => { // live — same call, typed states
|
|
219
|
+
if (st.status === 'ready') render(st.value); // pending | ready | failed
|
|
220
|
+
});
|
|
221
|
+
const found = await client.match.search(rId, refId, ctx); // bounded streams ARE awaitable
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Methods return one of: `Promise<T>` (atomic gateway ops), `StreamObservable` /
|
|
225
|
+
`UploadObservable` (bounded progress — thenable, `await` resolves the final value),
|
|
226
|
+
`DelegationObservable` (a delegated job — thenable, `await` resolves the job's completion, typed by its verb),
|
|
227
|
+
`CacheObservable` (live queries — `.subscribe(...)` for `CacheState` emissions,
|
|
228
|
+
`.fresh()` for the explicit network read; deliberately NOT thenable, so a cache read can
|
|
229
|
+
never silently become a round trip), `ClaimsObservable` (a worker's claims — below), a count
|
|
230
|
+
(wire drives — below), or `void` (local signals). The
|
|
231
|
+
per-method table and the `.run()` rule for progress-plus-result live in
|
|
232
|
+
[docs' `builder/REACTIVE-MODEL.md`](https://github.com/xynolog/docs/blob/main/builder/REACTIVE-MODEL.md).
|
|
233
|
+
|
|
234
|
+
## Directing attention
|
|
235
|
+
|
|
236
|
+
Directing attention is protocol-level coordination, not browser-app fluff. An agent calls
|
|
237
|
+
`beckon.attention(resourceId, annotationId)` and every connected person's viewer scrolls to
|
|
238
|
+
the passage; `beckon.sparkleAll` lights it up; `beckon.openResource` opens a resource on
|
|
239
|
+
their screens. With no annotation, `beckon.attention(resourceId)` attends to the resource as a
|
|
240
|
+
whole. Each of these wire drives resolves with how many clients it reached.
|
|
241
|
+
|
|
242
|
+
The `void` signals (`beckon.hover`, `bind.initiate`, `mark.request`) are different: they stay
|
|
243
|
+
on one client's own bus, where its interface coordinates itself.
|
|
244
|
+
|
|
245
|
+
## A worker
|
|
246
|
+
|
|
247
|
+
A worker claims jobs from a knowledge base's queue, does each one, and says what became of
|
|
248
|
+
it. `client.job.claim` is its side of the queue. It is given what the worker accepts, each a
|
|
249
|
+
`JobFilter`: the `mark` jobs of one motivation, or the `yield` jobs. The `ClaimsObservable` it
|
|
250
|
+
returns hands out the jobs the worker comes to hold, one at a time, and a held job says its
|
|
251
|
+
own lifecycle.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { annotationOfSpan, reconcile, type Annotation, type QuotedText } from '@xynolog/sdk';
|
|
255
|
+
import type { components } from '@xynolog/core';
|
|
256
|
+
|
|
257
|
+
/** Your model: the passages of a text worth a highlight, each as the words it quotes. */
|
|
258
|
+
declare function passagesOf(text: string): Promise<QuotedText[]>;
|
|
259
|
+
|
|
260
|
+
await client.wire.emit('principal:profile', { name: 'my-highlighter' }); // what it is called, said as itself
|
|
261
|
+
const engine = { provider: 'ollama', model: 'gemma3' }; // the model this worker runs
|
|
262
|
+
const generator: components['schemas']['Agent'] = { // what its annotations say made them
|
|
263
|
+
'@type': 'Software', '@id': agent.did, ...engine,
|
|
264
|
+
};
|
|
265
|
+
const claims = client.job.claim({
|
|
266
|
+
accepts: [{ jobType: 'mark', params: { motivation: 'highlighting' } }],
|
|
267
|
+
});
|
|
268
|
+
claims.refused$.subscribe((refusal) => console.error(`claim refused: ${refusal.message}`));
|
|
269
|
+
claims.subscribe(async (job) => {
|
|
270
|
+
// A completion is its verb's, so the verb is checked before `complete` is called.
|
|
271
|
+
if (job.jobType !== 'mark') return job.fail(`this worker runs no ${job.jobType} job`);
|
|
272
|
+
await job.start();
|
|
273
|
+
const text = await client.browse.resourceContent(job.resourceId);
|
|
274
|
+
const quoted = await passagesOf(text);
|
|
275
|
+
|
|
276
|
+
const annotations: Annotation[] = [];
|
|
277
|
+
for (const words of quoted) {
|
|
278
|
+
const span = reconcile(text, words); // where the text has the words, or null
|
|
279
|
+
if (span) annotations.push(annotationOfSpan({ text, resourceId: job.resourceId, generator, motivation: 'highlighting', span }));
|
|
280
|
+
}
|
|
281
|
+
await job.commit(job.resourceId, annotations); // resolves once the record has them
|
|
282
|
+
await job.complete({ found: quoted.length, persisted: annotations.length }); // settles: the next job is claimed
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
- **A worker is an agent, and an agent is an account.** It signs in with its service account,
|
|
287
|
+
whose token is its bearer, and the gateway names it by that account: `startAgentSession`
|
|
288
|
+
signs it in, learns its DID, and keeps the token fresh. The engine it runs is no part of
|
|
289
|
+
who it is: what it commits names that in its `generator`.
|
|
290
|
+
- **A worker signs in with a key.** Its credential is a `KeyCredential`: its issuer, its
|
|
291
|
+
client id, and the RSA key the launcher hands it as `XYNOLOG_OIDC_CLIENT_KEY`, a PKCS#8
|
|
292
|
+
PEM that `privateKeyFromPem` reads. Each grant carries an assertion signed for it alone,
|
|
293
|
+
which the issuer checks with the public half registered on the client, and no secret is
|
|
294
|
+
sent. A service account that holds a secret signs in with a `SecretCredential` instead.
|
|
295
|
+
- **A service reaches another as itself** with a `ServiceToken` made for its account: its
|
|
296
|
+
`authorization()` is the `Authorization` header's value, the token held until it is due
|
|
297
|
+
and one grant at a time.
|
|
298
|
+
- **It says what it is called**, as itself, on `principal:profile`. Readers name an agent by
|
|
299
|
+
the last name it said, as they name a person, so a `generator` carries no name of its own.
|
|
300
|
+
- **Its stream names `JOB_CLAIM_CHANNELS`**, and the reply channels of whatever else it
|
|
301
|
+
awaits. A client whose stream does not is refused at once, as `bus.unsubscribed`.
|
|
302
|
+
- **The SDK builds what a worker commits.** `reconcile(text, quoted)` finds the words a model
|
|
303
|
+
quoted in a text: a span of the text's own, with its offsets, or `null` when they are
|
|
304
|
+
nowhere in it. `annotationOfSpan` builds the annotation of a span (its selectors, its
|
|
305
|
+
`generator`, its `body` if it has one), with an id derived from what the annotation is, so
|
|
306
|
+
that committing it again after a retry writes nothing new; given a PDF's `anchored` text in
|
|
307
|
+
place of `text`, it anchors the span by where it is on the page. A span that is not the
|
|
308
|
+
text's throws a `SpanRefusedError`, whose `code` names the refusal.
|
|
309
|
+
`annotationOfResource` builds an annotation of a resource as a whole, with no selector.
|
|
310
|
+
`reconcile` is one way to find a model's words in a text, with its own tolerance for a
|
|
311
|
+
misquote: a worker that finds spans another way gives `annotationOfSpan` its own span. The
|
|
312
|
+
builders make nothing a hand-written payload could not, so nothing a knowledge base relies
|
|
313
|
+
on rests on them.
|
|
314
|
+
- **A held job commits for itself**: `job.commit(resourceId, annotations)` sends the batch
|
|
315
|
+
as `mark:commit`, citing the job, and resolves once the record has it. When no
|
|
316
|
+
acknowledgement arrives it asks whether the batch's last annotation is on the resource,
|
|
317
|
+
and a commit that is not established rejects with the failure of its unanswered request.
|
|
318
|
+
The job says how its commits were established when it settles. A worker that commits
|
|
319
|
+
names `JOB_COMMIT_CHANNELS` in its stream as well.
|
|
320
|
+
- **A held job settles once**: `complete`, `fail` or `cancel`. Each says the outcome and
|
|
321
|
+
lets the job go, and the worker claims the next. `claims.stop()` fails a job still held,
|
|
322
|
+
so the queue runs it again at once.
|
|
323
|
+
- **Each job has a trace of its own**, and a worker's author writes nothing for it. A claim
|
|
324
|
+
is made in no trace, whatever span the job before it was settled in, and a job is handed
|
|
325
|
+
over in the trace its reply arrived in, which is its claim's: the span a worker that
|
|
326
|
+
exports telemetry opens around a job, and what the job sends from inside it, continue
|
|
327
|
+
that trace. `job.trace` is the same trace as a value, a W3C carrier (`traceparent`, and
|
|
328
|
+
`tracestate` when there is one) or `undefined` for a reply that arrived in none: for
|
|
329
|
+
what is done for the job where the hand-over does not reach, such as work passed to
|
|
330
|
+
another process. OpenTelemetry's `propagation.extract` takes it as it is. It is the
|
|
331
|
+
trace OpenTelemetry has active when the reply arrives, so in a process that has set up
|
|
332
|
+
no OpenTelemetry it is `undefined`, as every trace is there.
|
|
333
|
+
|
|
334
|
+
The [`xynolog-worker` skill](https://github.com/xynolog/sdk/blob/main/skills/xynolog-worker/SKILL.md)
|
|
335
|
+
is a whole worker, sign-in to shutdown, and
|
|
336
|
+
[`protocol/contract/WORKER-CONTRACT.md`](https://github.com/xynolog/protocol/blob/main/contract/WORKER-CONTRACT.md)
|
|
337
|
+
is what a worker promises the dispatcher.
|
|
338
|
+
|
|
339
|
+
## Any transport
|
|
340
|
+
|
|
341
|
+
`XynologClient` is built against the `ITransport` / `IContentTransport` contracts from
|
|
342
|
+
`@xynolog/core`, not any particular wire. The HTTP adapter is re-exported here for convenience.
|
|
343
|
+
|
|
344
|
+
## What is in the package
|
|
345
|
+
|
|
346
|
+
- **`XynologClient`** — the verb-oriented coordinator: the eight flow namespaces, plus `job`
|
|
347
|
+
(always present) and `auth`/`system` (present when constructed with gateway operations).
|
|
348
|
+
- **Annotation readers** — `getAnnotationExactText`, `getBodySource`, `getTargetSource`,
|
|
349
|
+
`getEntityTypes`, `isStubReference`, `getTagCategory` and their siblings read an
|
|
350
|
+
annotation whose target is an id or an object, whose selector is one or a list, and whose
|
|
351
|
+
body is absent, one item or a list, so a script checks none of that itself. One table,
|
|
352
|
+
`protocol/specs/src/annotations/reader-cases.json`, holds every SDK's readers to the same answers.
|
|
353
|
+
- **Annotation builders** — `reconcile`, `annotationOfSpan` and `annotationOfResource` turn
|
|
354
|
+
the words a model quoted into annotations to commit: where the text has the words, the
|
|
355
|
+
selectors of the span, and an id derived from what the annotation is. Every SDK has the
|
|
356
|
+
three, and `protocol/specs/src/annotations/builder-cases.json` and `reconcile-cases.json` hold them
|
|
357
|
+
to the same answers.
|
|
358
|
+
- **A worker's side of the job queue** — `job.claim`, the held jobs it hands out,
|
|
359
|
+
`startAgentSession` and `privateKeyFromPem` for a worker's sign-in, and
|
|
360
|
+
`JOB_CLAIM_CHANNELS` and `JOB_COMMIT_CHANNELS` for its stream.
|
|
361
|
+
- **Session layer** — `XynologSession` (per-KB auth, proactive token refresh, lifecycle),
|
|
362
|
+
`XynologBrowser` (multi-KB orchestration), `SessionStorage` adapters, `SignInKept` and
|
|
363
|
+
where a sign-in is kept (`MemorySignIn`, `keptIn`), `signOut`, the `httpKb` helper for
|
|
364
|
+
endpoint shapes, and `SignInStore` and `stateDir` for the sign-in `xynolog login` keeps.
|
|
365
|
+
- **Flow state machines** — closure-based factories (`createMarkStateUnit`, `…Gather…`,
|
|
366
|
+
`…Match…`, `…Yield…`, `…Beckon…`) wrapping each long-running flow with `loading$`/`error$`/
|
|
367
|
+
progress observables; UI-shape-agnostic ([docs' `builder/STATE-UNITS.md`](https://github.com/xynolog/docs/blob/main/builder/STATE-UNITS.md)).
|
|
368
|
+
- **KB discovery** — the consumer side of the launcher's published KB view:
|
|
369
|
+
`httpDiscovery` (polls the Browser origin's `DISCOVERY_URL_PATH` with ETag/304),
|
|
370
|
+
`textDiscovery` (bring-your-own IO — the sdk never imports `fs`), and
|
|
371
|
+
`subscribeDiscovery` (a polling diff stream with a typed absent-vs-managed state).
|
|
372
|
+
Descriptors only; auth stays per-KB. Types (`DiscoveredKB`, `DiscoveryDocument`) come
|
|
373
|
+
from `@xynolog/core`'s generated schema.
|
|
374
|
+
- **Helpers & types** — the cache primitive behind live queries
|
|
375
|
+
([`protocol/contract/CACHE-SEMANTICS.md`](https://github.com/xynolog/protocol/blob/main/contract/CACHE-SEMANTICS.md)),
|
|
376
|
+
`createSearchPipeline`, branded ids, and the unified error hierarchy (`XynologError`,
|
|
377
|
+
`BusRequestError`) re-exported so you catch every SDK error from one package. (The
|
|
378
|
+
typed bus over a transport, `Bus`, lives in `@xynolog/core`; a client hands its own out as
|
|
379
|
+
`client.wire`.)
|
|
380
|
+
|
|
381
|
+
This is everything a non-web consumer (TUI, mobile, daemon, agent) needs — nothing
|
|
382
|
+
page-shaped. Page-level state machines and components, including the **embeddable
|
|
383
|
+
`ResourceViewer`**, live in [`@xynolog/react-ui`](https://github.com/xynolog/sdk/tree/main/typescript/react-ui).
|
|
384
|
+
|
|
385
|
+
## Documentation
|
|
386
|
+
|
|
387
|
+
The builder docs start at [docs' `builder/README.md`](https://github.com/xynolog/docs/blob/main/builder/README.md):
|
|
388
|
+
who the SDK is for, where it goes, and the docs in reading order.
|
|
389
|
+
|
|
390
|
+
- **[docs' `builder/DEVELOPER-GUIDE.md`](https://github.com/xynolog/docs/blob/main/builder/DEVELOPER-GUIDE.md) — start here to build.** Task-ordered recipes, connect through teardown.
|
|
391
|
+
- [docs' `builder/Usage.md`](https://github.com/xynolog/docs/blob/main/builder/Usage.md) — per-namespace API tour with concrete examples, plus SSE and error handling.
|
|
392
|
+
- [docs' `builder/REACTIVE-MODEL.md`](https://github.com/xynolog/docs/blob/main/builder/REACTIVE-MODEL.md) — the Promise-shape-over-Observable design.
|
|
393
|
+
- [docs' `builder/STATE-UNITS.md`](https://github.com/xynolog/docs/blob/main/builder/STATE-UNITS.md) — the state-unit pattern and its enforced axioms.
|
|
394
|
+
- [`protocol/contract/CACHE-SEMANTICS.md`](https://github.com/xynolog/protocol/blob/main/contract/CACHE-SEMANTICS.md) — the cache primitive's numbered behavioral contract.
|
|
395
|
+
- [`protocol/contract/TRANSPORT-CONTRACT.md`](https://github.com/xynolog/protocol/blob/main/contract/TRANSPORT-CONTRACT.md) — what every `ITransport` must honor; HTTP specifics in [TRANSPORT-HTTP.md](https://github.com/xynolog/protocol/blob/main/contract/TRANSPORT-HTTP.md). New transports implement the `@xynolog/core` interfaces directly — no inheritance from `HttpTransport`.
|
|
396
|
+
|
|
397
|
+
## Related packages
|
|
398
|
+
|
|
399
|
+
- [`xynolog`](https://github.com/xynolog/sdk/tree/main/rust/sdk) — the Rust SDK, a full peer of this one, on [crates.io](https://crates.io/crates/xynolog)
|
|
400
|
+
- [`xynolog`](https://github.com/xynolog/sdk/tree/main/python/sdk) — the Python SDK, on [PyPI](https://pypi.org/project/xynolog/)
|
|
401
|
+
- [`github.com/xynolog/sdk/go`](https://github.com/xynolog/sdk/tree/main/go) — the Go SDK, on [pkg.go.dev](https://pkg.go.dev/github.com/xynolog/sdk/go), which the `xynolog` launcher is built on
|
|
402
|
+
- [`@xynolog/core`](https://github.com/xynolog/sdk/tree/main/typescript/core) — domain types, `ITransport` contract, `Bus`, OpenAPI-derived schemas
|
|
403
|
+
- [`@xynolog/http-transport`](https://github.com/xynolog/sdk/tree/main/typescript/http-transport) — HTTP transport (`HttpTransport`, `HttpContentTransport`)
|
|
404
|
+
- [`@xynolog/observability`](https://github.com/xynolog/sdk/tree/main/typescript/observability) — OpenTelemetry tracing the SDK propagates across the bus
|
|
405
|
+
- [`@xynolog/react-ui`](https://github.com/xynolog/sdk/tree/main/typescript/react-ui) — the embeddable `ResourceViewer` (bring-your-own-session) plus React hooks (`useResourceLoader`, `useMediaToken`, `useObservable`) and the web `SessionStorage`; its docs cross-link the [Developer Guide](https://github.com/xynolog/docs/blob/main/builder/DEVELOPER-GUIDE.md)
|
|
406
|
+
|
|
407
|
+
## License
|
|
408
|
+
|
|
409
|
+
Apache-2.0 — see [LICENSE](https://github.com/xynolog/sdk/blob/main/LICENSE).
|