@jush-agent/sdk 0.0.0-stage → 0.2.6247

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 CHANGED
@@ -1,3 +1,492 @@
1
- # Temporary Holding Version
1
+ # @jush-agent/sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Jush's agents in your program. The SDK starts `jush-core`, the process your
4
+ program's cores run in, and gives your program the cores, their
5
+ conversations, providers and devices, and plugins of your own whose commands
6
+ the agents run as `jush <group> <command>`.
7
+
8
+ A core is one Jush: its conversations, provider entries, devices and
9
+ settings, kept in a directory you choose. A team bot, for example, opens one
10
+ core for each team it serves.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm install @jush-agent/sdk zod
16
+ ```
17
+
18
+ The package installs the executables of your machine's platform, macOS,
19
+ Linux or Windows on x86-64 or ARM64, from its optional platform packages,
20
+ such as `@jush-agent/sdk-linux-x64`. It runs on Node.js 22 or later, Bun and
21
+ Deno. `zod` defines your plugins' command arguments.
22
+
23
+ ## Quick start
24
+
25
+ The following program opens a core, gives it an Anthropic API key and a
26
+ device on your machine, and runs one turn:
27
+
28
+ ```ts
29
+ import { Jush } from '@jush-agent/sdk'
30
+
31
+ const jush = await Jush.start()
32
+ const core = await jush.open('./data/team-a')
33
+
34
+ const provider = await core.providers.create({
35
+ source: 'vendor',
36
+ vendorId: 'anthropic',
37
+ apiKey: process.env.ANTHROPIC_API_KEY,
38
+ })
39
+ const device = await core.devices.create()
40
+ await device.spawn()
41
+
42
+ const conversation = await core.conversations.create({
43
+ title: 'Release notes',
44
+ model: { providerId: provider.id, modelId: 'claude-sonnet-4-5' },
45
+ target: { kind: 'device', deviceId: device.id, path: '/srv/work' },
46
+ })
47
+ const turn = await conversation.run('Draft the notes for this week from the git log')
48
+ console.log(turn.outcome, turn.text)
49
+
50
+ await jush.close()
51
+ ```
52
+
53
+ `turn.outcome` is `finished`, `failed` or `stopped`; `turn.text` is the text
54
+ the agent wrote after your message, and `turn.blocks` everything it did.
55
+
56
+ ## Cores
57
+
58
+ `Jush.start()` starts one `jush-core` process for your program, and
59
+ `jush.open(dir)` opens a core in it, creating the core when `dir` is absent
60
+ or empty. A process holds any number of cores. Open the same directory again
61
+ later, and the core has everything it had: conversations, provider entries,
62
+ devices and settings.
63
+
64
+ A core is open in one process at a time; opening it in a second fails with a
65
+ `JushError` whose `code` is `core_open`.
66
+
67
+ `core.close()` closes one core, and `jush.close()` closes every core and ends
68
+ the process. A turn still running when its core closes stops there, and Jush
69
+ takes it up again when the core opens again.
70
+
71
+ `jush-core` and the runners the SDK starts write their logs to your
72
+ program's standard error.
73
+
74
+ ## A process of its own
75
+
76
+ `Jush.start()` ties the process to your program: when your program stops,
77
+ for example to deploy a new version, every core closes, and a running turn
78
+ stops until the core opens again. To keep the cores running while your
79
+ program restarts, or to share them between several services, run
80
+ `jush-core` on its own, as a server:
81
+
82
+ ```sh
83
+ export JUSH_CORE_TOKEN=7f3c9e2a51d84b06
84
+ export JUSH_RELEASE=node_modules/@jush-agent/sdk/release:node_modules/@jush-agent/sdk-linux-x64/release
85
+ node_modules/@jush-agent/sdk-linux-x64/bin/jush-core serve --listen 127.0.0.1:4100
86
+ ```
87
+
88
+ `jush-core` is in your platform's package. `JUSH_RELEASE` lists the
89
+ `release` directories of the SDK and of each platform package, as `PATH`
90
+ does, so the cores' devices run Jush's commands. Each of your services
91
+ connects with the same token:
92
+
93
+ ```ts
94
+ const jush = await Jush.connect({ url: 'http://127.0.0.1:4100', token, plugins: [chat] })
95
+ const core = await jush.open('/srv/bot/data/team-a')
96
+ ```
97
+
98
+ - Opening a directory the process holds already answers the same core, with
99
+ its conversations and turns as they went on while your program was away.
100
+ - `jush.close()` disconnects and closes nothing; `core.close()` closes the
101
+ core for every service.
102
+ - Each connection declares its plugins again, and the last declaration of a
103
+ plugin's id stands: a new version of your program changes the commands
104
+ of every conversation at once.
105
+ - `SIGTERM` or Ctrl-C stops the process, which closes every core. Your
106
+ program connects again by itself once the process is back, declares its
107
+ plugins and opens its cores again, and goes on.
108
+
109
+ Set `JUSH_PUBLIC_URL` to where the process is reached from outside, such as
110
+ `https://bot.example.com/jush`, for schedules' URLs and plugins' sign-ins,
111
+ which the process then answers itself.
112
+
113
+ A runner reaches a process of its own as it reaches a Jush server: start it
114
+ anywhere with `jush-runner run --backend http://bot.example.com:4100`. It
115
+ prints a pairing code and waits; pair it to a core with
116
+ `core.devices.claim(code)`. The runner keeps its token, and connects again
117
+ by itself after it or the process restarts.
118
+
119
+ ## Providers
120
+
121
+ A provider entry is what a core's agents call models with. Create one with
122
+ an API key of a vendor, such as `anthropic`, `openai` or `google`:
123
+
124
+ ```ts
125
+ const openai = await core.providers.create({ source: 'vendor', vendorId: 'openai', apiKey: process.env.OPENAI_API_KEY })
126
+ ```
127
+
128
+ Or with an endpoint that speaks a vendor's protocol, such as a gateway or a
129
+ model server of your own; `providerType` is the protocol, `anthropic` when
130
+ absent:
131
+
132
+ ```ts
133
+ const gateway = await core.providers.create({
134
+ source: 'compatible',
135
+ providerType: 'openai',
136
+ label: 'Team Gateway',
137
+ baseUrl: 'https://gateway.example.com/v1',
138
+ apiKey: process.env.GATEWAY_KEY,
139
+ })
140
+ ```
141
+
142
+ The models each entry offers are in the core's state, as
143
+ `core.state.models.providers`, each with its `providerId` and `models`.
144
+ `core.providers` also lists, updates and removes entries.
145
+
146
+ ### Subscriptions
147
+
148
+ A subscription entry signs in with an account instead of a key: `codex` for
149
+ ChatGPT, `claude-code` for Claude, or `grok-build` for Grok. The user
150
+ confirms the sign-in in a browser. Its stages are in `core.state.logins`:
151
+
152
+ ```ts
153
+ const signIn = await core.providers.signIn({ providerType: 'codex' })
154
+ const stop = core.on('state', (state) => {
155
+ const login = state.logins.find((each) => each.id === signIn.id)?.login
156
+ if (login?.status === 'pending' && login.verificationUrl !== null) {
157
+ showToUser(`Open ${login.verificationUrl} and enter ${login.userCode}`)
158
+ } else if (login !== undefined && login.status !== 'pending') {
159
+ stop()
160
+ }
161
+ })
162
+ ```
163
+
164
+ When a login's `needsCode` is true, the user signs in at the address
165
+ instead, and you hand the code the page shows to `signIn.submitCode(code)`.
166
+ `signIn.cancel()` stops it.
167
+
168
+ An entry can hold several accounts: `core.providers.accounts(id)` signs in
169
+ another one, and lists, activates, orders and removes them. Once an entry
170
+ holds two or more, its `rotation` says which account each agent uses:
171
+ `manual` the active one, `in_order` the first in their order that is not used
172
+ up, and `smart` first the one whose quota resets soonest, since what is left
173
+ at a reset is lost:
174
+
175
+ ```ts
176
+ await core.providers.update(entry.id, { rotation: 'smart' })
177
+ ```
178
+
179
+ ## Devices
180
+
181
+ A device is a machine the agents run commands on, served by a
182
+ `jush-runner`. `device.spawn()` starts the package's own runner on your
183
+ program's machine. Give it a command to start a runner elsewhere, such as in
184
+ a container; each device's runner keeps its state in a directory of its own,
185
+ `--home`:
186
+
187
+ ```ts
188
+ const box = await core.devices.create()
189
+ await box.spawn(['docker', 'exec', '-i', 'box-7', 'jush-runner', 'run', '--stdio', '--home', `/var/lib/jush/${box.id}`])
190
+ ```
191
+
192
+ `spawn()` resolves once the core counts the device online, so a conversation
193
+ can be created on it, and answers the runner's child process. It fails when
194
+ the command exits before, or when no runner comes on its channel within 30
195
+ seconds, and then ends the command. The core counts a device online for a
196
+ short while after its runner drops, and after the core opens, if its runner
197
+ was connected when the core closed, waiting for the runner to come back;
198
+ `spawn()` of such a device resolves at once, so watch the child process for a
199
+ runner that exits.
200
+
201
+ A runner that reaches the core some other way, such as over SSH or a socket,
202
+ connects with `device.connect({ readable, writable })`, the standard output
203
+ and input of a runner already started. It resolves as `spawn()` does, with
204
+ `ended`, which resolves once either end closed.
205
+
206
+ A core keeps its devices, but not their runners: when your program starts
207
+ again, start a runner for each device the core lists:
208
+
209
+ ```ts
210
+ for (const listed of core.state.devices) {
211
+ await core.devices.of(listed).spawn()
212
+ }
213
+ ```
214
+
215
+ `device.revoke()` revokes a device, as the app's Revoke does: its token opens
216
+ nothing from then on, the commands running on it are lost, and the commands of
217
+ a conversation whose target it is fail until `update({ target })` names
218
+ another. A runner connected to it exits and removes its `--home`, or only its
219
+ own files there when `--home` is the home directory, holds it, or is a
220
+ filesystem's root, such as a volume's mount point.
221
+
222
+ ### Sandboxes that expire
223
+
224
+ A sandbox that lives for a while and then goes, such as a container your
225
+ platform keeps for 24 hours, is one device for each sandbox. Make a device
226
+ when the sandbox starts, and revoke it when the sandbox ends; a device is
227
+ never reused for the next sandbox, since its runner's state went with the
228
+ sandbox. One sandbox can serve one conversation, or every conversation of its
229
+ day.
230
+
231
+ When a sandbox ends before a conversation does, start a new sandbox, move the
232
+ conversation onto its device, and revoke the old device:
233
+
234
+ ```ts
235
+ const next = await core.devices.create()
236
+ await next.spawn(['sandbox', 'exec', 'sbx-0412', 'jush-runner', 'run', '--stdio', '--home', `/var/lib/jush/${next.id}`])
237
+ for (const conversation of onOldSandbox) {
238
+ await conversation.update({ target: { kind: 'device', deviceId: next.id, path: '/workspace' } })
239
+ }
240
+ await old.revoke()
241
+ ```
242
+
243
+ The conversation keeps its transcript, and its agent is told on its next turn
244
+ that no file moved: what the old sandbox held, its files and the browser
245
+ profiles on it among them, went with it. What has to outlive a sandbox lives
246
+ on a volume your platform keeps across sandboxes.
247
+
248
+ ## Conversations
249
+
250
+ `core.conversations.create()` takes the conversation's `title`, `model` and
251
+ `target`, the device and directory its commands run in. A conversation
252
+ without a target runs on the Cloud, which only Jush's server runs, so in a
253
+ program it is refused with `cloud_unavailable`, as one on a device that does
254
+ not exist is with `device_not_found`. `core.conversations.open(id)` takes a
255
+ conversation created earlier, and `conversation.update(patch)` changes the
256
+ same fields later.
257
+
258
+ Each conversation's browser has a profile of its own, so a sign-in made in
259
+ one conversation is not seen in another. Conversations that name the same
260
+ `browserProfile` on one device share its browser instead: a site one of them
261
+ signs in to is signed in for the others, and each keeps its own tabs. The
262
+ profile is the directory `~/.jush/browser-profiles/<name>` on the device,
263
+ made at its first use; list, copy or remove it as any directory.
264
+
265
+ ```ts
266
+ const triage = await core.conversations.create({ title: 'Triage', model, target, browserProfile: 'support' })
267
+ const refunds = await core.conversations.create({ title: 'Refunds', model, target, browserProfile: 'support' })
268
+ // Back to a profile of its own, from its next browser command on:
269
+ await refunds.update({ browserProfile: null })
270
+ ```
271
+
272
+ An agent introduces itself as Jush unless its conversation names an
273
+ `identity`, the sentences that open its system prompt instead. Jush's rules
274
+ for running commands and replying still follow them. `plugins` lists which
275
+ of your program's plugins the conversation's agents have, by id; without it
276
+ they have all of them. Jush's own plugins follow the user's switches either
277
+ way. A change made with `update` reaches each agent from its next request.
278
+
279
+ ```ts
280
+ const desk = await core.conversations.create({
281
+ model,
282
+ target,
283
+ identity: "You are the help desk's assistant. You answer the team's questions about their accounts.",
284
+ plugins: ['tickets'],
285
+ })
286
+ // Try the next version of the commands in this conversation alone:
287
+ await desk.update({ plugins: ['tickets-next'] })
288
+ ```
289
+
290
+ `conversation.run(input)` sends a message and resolves once the turn it
291
+ starts ended. It fails when the message leaves the queue without running, as
292
+ an abort that clears the queue makes it, or when the conversation closes
293
+ first. The other actions are the ones the app has:
294
+
295
+ | Method | What it does |
296
+ | --- | --- |
297
+ | `send(input, messageId?)` | Sends a message, queued behind a running turn; resolves once the conversation holds it |
298
+ | `steer(input, steerId?)` | Hands a message to the running turn; resolves once the turn took it |
299
+ | `abort()` | Stops the running turn |
300
+ | `retry()` | Runs the latest turn again from your message |
301
+ | `resume()` | Finishes a turn that did not finish |
302
+ | `compact()` | Summarizes the conversation so far, to free its context |
303
+
304
+ `send` and `steer` take an id after the input. Give one when you may send
305
+ the same message again, such as after a lost answer: while the conversation
306
+ holds input under an id, queued, waiting or in its history, anything sent
307
+ under it again, as a message or a steer, is acknowledged and not taken a
308
+ second time. Input that leaves it, such as a message taken out of the queue,
309
+ frees its id. Make each id your own, such as a ticket's number with a
310
+ prefix: a steer under the id of one of the conversation's blocks is refused.
311
+
312
+ ```ts
313
+ await conversation.steer('Use the staging database instead.', 'steer-ticket-42')
314
+ ```
315
+
316
+ A message is text, or content blocks, such as a file on one of the core's
317
+ devices:
318
+
319
+ ```ts
320
+ await conversation.run([
321
+ { type: 'text', text: 'Summarize this report' },
322
+ { type: 'remote_file', deviceId: device.id, path: '/srv/work/report.md' },
323
+ ])
324
+ ```
325
+
326
+ `conversation.blocks` is what the conversation holds, oldest first.
327
+ `conversation.events()` yields everything that happens in it, as the app
328
+ shows it live; a `transcript_patch` event says the blocks changed, such as
329
+ while the agent writes:
330
+
331
+ ```ts
332
+ for await (const event of conversation.events()) {
333
+ if (event.type === 'transcript_patch') {
334
+ render(conversation.blocks)
335
+ }
336
+ }
337
+ ```
338
+
339
+ The loop ends when you leave it or the conversation closes.
340
+ `conversation.close()` stops following the conversation; its agents go on in
341
+ the core, and the next action follows it again.
342
+
343
+ `conversation.sendToAgent(node, text)` sends a message to one subagent, named
344
+ by its id in the conversation's agents. A subagent at work takes it as a
345
+ steer, an idle one starts a turn with it, and one that ended is resumed with
346
+ it. Give the message an id when you may send it again, such as after a lost
347
+ answer: the subagent takes a message once, however often it arrives.
348
+
349
+ ```ts
350
+ await conversation.sendToAgent(node, 'The build finished.', 'reminder-7')
351
+ ```
352
+
353
+ ## Permission requests
354
+
355
+ A command that acts on Jush itself, such as `jush credentials`, which hands
356
+ the agent a stored secret, or one of your plugin's commands that declares a
357
+ permission, waits for the user's permission in the conversation. `events()` yields a `permission`
358
+ event for each request, and `conversation.permissions` lists those waiting:
359
+
360
+ ```ts
361
+ for await (const event of conversation.events()) {
362
+ if (event.type === 'permission') {
363
+ await event.permission.allow()
364
+ }
365
+ }
366
+ ```
367
+
368
+ `deny()` refuses it. A request for a credential takes the values it asks
369
+ for: `allow({ values: { token: 'xoxb-example' } })`.
370
+
371
+ ## Plugins
372
+
373
+ A plugin gives the agents commands that your program runs. The following
374
+ plugin adds `jush chat send`, which posts to the chat a conversation answers
375
+ in:
376
+
377
+ ```ts
378
+ import { Jush, definePlugin, leaf } from '@jush-agent/sdk'
379
+ import { z } from 'zod'
380
+
381
+ const chat = definePlugin({
382
+ id: 'chat',
383
+ name: 'Chat Commands',
384
+ description: 'Gives the agent `jush chat send`, which posts to the chat.',
385
+ commands: {
386
+ chat: {
387
+ summary: 'The chat this conversation answers in',
388
+ index: 'Posts to the chat this conversation answers in.',
389
+ commands: {
390
+ send: leaf({
391
+ summary: 'Posts a message to the chat',
392
+ input: z.object({ text: z.string().describe('The message, in Markdown') }),
393
+ positionals: ['text'],
394
+ output: 'the id of the message posted',
395
+ async run({ text }, call) {
396
+ const id = await postToChat(call.invocation.context.conversation, text)
397
+ await call.stdout(`${id}\n`)
398
+ },
399
+ }),
400
+ },
401
+ },
402
+ },
403
+ })
404
+
405
+ const jush = await Jush.start({ plugins: [chat] })
406
+ ```
407
+
408
+ Every core of the process gives its agents the plugin's commands. The agent
409
+ learns them as it learns Jush's own: `index` is the group's entry in the
410
+ index of capabilities the agent reads, which says what the group is for, and
411
+ each command's `summary`, input and `output` are its `--help`.
412
+
413
+ - Each field of `input` is an option of the same name, such as `--text`, and
414
+ its `describe` text is the option's help; `positionals` lists the fields
415
+ given by their place, in order. Arguments that do not fit the schema fail
416
+ before `run` is called.
417
+ - `run(input, call)` writes the command's output with `call.stdout()` and
418
+ `call.stderr()`, and reads its standard input with `call.readStdin()` when
419
+ the leaf names a `stdin` field. What it returns is the exit status, 0 when
420
+ it returns nothing; a failure writes why to standard error and returns a
421
+ status other than 0, as a shell command does.
422
+ - `call.signal` aborts when the agent stops the command.
423
+ - `jsonOutput` declares what the command's `--json` writes.
424
+ - A group's `permissions` declares categories, and a leaf's `permission`
425
+ names the one it needs: the conversation then asks before the command
426
+ runs.
427
+
428
+ A plugin can also add a context block before each request (`context`),
429
+ search a conversation's project (`projectSearch`), and follow conversations
430
+ being deleted or forked.
431
+
432
+ A command your program runs outlives your program's restart. If your
433
+ program stops while `run` is under way, the command waits, and once your
434
+ program serves the plugin again, `run` is called again for the same call,
435
+ with `call.resent` true. `call.request` is the call's number, which no other
436
+ call ever takes: use it to do the work once, for example when the command
437
+ posts a message. What the command already wrote stays in its output, and
438
+ what `run` writes now follows it; input the earlier call read is not read
439
+ again. While no program serves the plugin, a new command waits 10 seconds
440
+ for one, and then fails, telling the agent that the plugin's program is
441
+ unreachable. A `context` call does not wait: when your program stops
442
+ during one, the agent goes on without its block.
443
+
444
+ ## Product state and notifications
445
+
446
+ `core.state` is the core's product state: its workspaces, projects, provider
447
+ entries and their models, devices, conversations and sign-ins, as the app
448
+ shows them. `core.on('state', listener)` calls the listener at each change,
449
+ and answers a function that stops it.
450
+
451
+ `core.on('notify', listener)` receives what the app would notify the user
452
+ of: a turn that finished or failed, a permission request, or a notification
453
+ the agent sent with `jush notify send`. A program has one such listener for
454
+ each core, and a new one replaces it; when several services listen to one
455
+ core, the one that listened last receives them. Answer how you delivered
456
+ it; the agent's `jush notify send` reports it:
457
+
458
+ ```ts
459
+ core.on('notify', async (message) => {
460
+ if (message.kind === 'agent') {
461
+ await postToChat(message.conversationId, `${message.title}\n${message.body}`)
462
+ return { type: 'sending', to: 1 }
463
+ }
464
+ return { type: 'nowhere' }
465
+ })
466
+ ```
467
+
468
+ `{ type: 'shown' }` says every place that would show it shows its
469
+ conversation already.
470
+
471
+ ## Showing conversations on your pages
472
+
473
+ [`@jush-agent/web-components`](../web-components/README.md) shows a
474
+ conversation on your own pages with the app's own views: its transcript,
475
+ its composer, and its work in windows with a taskbar.
476
+ Your server forwards the pages' requests with `core.fetch(request, base)`
477
+ and their connection with `core.accept(socket, url)`, which takes a
478
+ WebSocket of the standard interface, as Deno's `upgradeWebSocket` and the
479
+ `ws` package give.
480
+
481
+ Jush can reach your program from outside when you tell it where your server
482
+ forwards its routes: `Jush.start({ publicUrl: 'https://bot.example.com/jush' })`.
483
+ A schedule's URL is then `/hooks/{id}` below it, which your server answers
484
+ with `jush.fetch(request, '/jush')`, and a plugin's sign-in returns there.
485
+
486
+ ## Errors
487
+
488
+ A request the core refuses fails with a `JushError`: `status` is the HTTP
489
+ status, `code` the refusal's code, such as `core_open`, and `message` says
490
+ why in words. `requestId` names the request in the core's log.
491
+
492
+ Part of [Jush](../../README.md). The package is under the Apache License 2.0.