@jush-agent/sdk 0.2.6332 → 0.2.6342

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
@@ -105,14 +105,16 @@ const core = await jush.open('/srv/bot/data/team-a')
105
105
  core for every service.
106
106
  - Each connection declares its plugins again, and the last declaration of a
107
107
  plugin's id stands: a new version of your program changes the commands
108
- of every conversation at once.
108
+ of every conversation at once. A connection that passes `addresses`
109
+ declares them in place of those before; one without them keeps them.
109
110
  - `SIGTERM` or Ctrl-C stops the process, which closes every core. Your
110
111
  program connects again by itself once the process is back, declares its
111
- plugins and opens its cores again, and goes on.
112
+ addresses and plugins and opens its cores again, and goes on.
112
113
 
113
- Set `JUSH_PUBLIC_URL` to where the process is reached from outside, such as
114
- `https://bot.example.com/jush`, for schedules' URLs and plugins' sign-ins,
115
- which the process then answers itself.
114
+ The process answers only your services and its runners. A call from
115
+ outside, such as one of a schedule's URL, reaches your server at an
116
+ address of yours, which hands it on: see
117
+ [Your server's addresses](#your-servers-addresses).
116
118
 
117
119
  A runner reaches a process of its own as it reaches a Jush server: start it
118
120
  anywhere with `jush-runner run --backend http://bot.example.com:4100`. It
@@ -341,35 +343,59 @@ const refunds = await core.conversations.create({ title: 'Refunds', model, targe
341
343
  await refunds.update({ browserProfile: null })
342
344
  ```
343
345
 
344
- An agent introduces itself as Jush unless its conversation names an
345
- `identity`, the sentences that open its system prompt instead. Jush's rules
346
- for running commands and replying still follow them. `plugins` lists which
347
- of your program's plugins the conversation's agents have, by id; without it
348
- they have all of them. Jush's own plugins follow the user's switches either
349
- way. A change made with `update` reaches each agent from its next request.
346
+ Every agent of a conversation, its subagents included, reads Jush's system
347
+ prompt unless the conversation names a `systemPrompt`, your whole text in
348
+ its place. After it, Jush writes the index of the conversation's commands
349
+ and the line naming the model that serves it, and a subagent's profile's
350
+ instructions after those. A change made with `update` reaches each agent
351
+ from its next request, and `systemPrompt: null` gives Jush's back.
352
+
353
+ `commands` lists the commands the conversation's agents have, Jush's and
354
+ your plugins' alike, each by its path without `jush`: a group, such as
355
+ `browser`, names every command under it, and a leaf, such as
356
+ `conversation list`, names that one. Your plugin's commands are named by
357
+ their group's name, not by the plugin's id. A command the list does not name
358
+ is not in the agent's prompt, and running it answers as for a command that
359
+ does not exist; a plugin the list names no command of gives no context
360
+ either. `jush shell`, which reads what the shell tool cut short, stays
361
+ whatever the list says. Without a list the agents have every command; with
362
+ one, a command Jush adds later reaches the conversation only once its list
363
+ names it. A command still follows the core's own switches. A change of the
364
+ list reaches each agent from its next command.
350
365
 
351
366
  ```ts
352
367
  const desk = await core.conversations.create({
353
368
  model,
354
369
  target,
355
- identity: "You are the help desk's assistant. You answer the team's questions about their accounts.",
356
- plugins: ['tickets'],
370
+ systemPrompt: "You are the help desk's assistant. You answer the team's questions about their accounts, in the team's chat: plain text, no links.",
371
+ commands: ['browser', 'schedule', 'tickets'],
357
372
  })
358
373
  // Try the next version of the commands in this conversation alone:
359
- await desk.update({ plugins: ['tickets-next'] })
374
+ await desk.update({ commands: ['browser', 'schedule', 'tickets-next'] })
360
375
  ```
361
376
 
362
- `conversation.run(input)` sends a message and resolves once the turn it
363
- starts ended, with that turn's blocks and outcome, also when your program's
364
- connection to the process dropped while the turn ended. It fails when the
365
- message leaves the queue without running, as an abort that clears the queue
366
- or gives the message back makes it, or when the conversation closes first.
367
- The other actions are the ones the app has:
377
+ A core an earlier release made kept a list of plugin ids, `plugins`, which
378
+ opens as `commands`: Jush's groups of then and those ids, each taken for its
379
+ plugin's group name. Name again a plugin whose group is named otherwise. A
380
+ conversation's `identity`, which opened Jush's system prompt, opens as the
381
+ `systemPrompt` its agents read then: the identity, and Jush's rules for
382
+ running commands and replying after it.
383
+
384
+ `conversation.run(input, options?)` sends a message and resolves once the
385
+ turn it starts ended, with that turn's blocks and outcome, also when your
386
+ program's connection to the process dropped while the turn ended. It fails
387
+ when the message leaves the queue without running, as an abort that clears
388
+ the queue or gives the message back makes it, or when the conversation
389
+ closes first. `options.senderId` names who sent the message, by your own
390
+ id, such as a person of a team's chat: the message's `user` block keeps it
391
+ as `senderId`, so a page can show who wrote what, the model does not read
392
+ it, and [Usage](#usage) says what it counts. The other actions are the ones
393
+ the app has:
368
394
 
369
395
  | Method | What it does |
370
396
  | --- | --- |
371
- | `send(input, messageId?)` | Sends a message, queued behind a running turn; resolves once the conversation holds it |
372
- | `steer(input, steerId?)` | Hands a message to the running turn; resolves once the turn took it |
397
+ | `send(input, messageId?, options?)` | Sends a message, queued behind a running turn; resolves once the conversation holds it; `options.senderId` as `run` takes it |
398
+ | `steer(input, steerId?, options?)` | Hands a message to the running turn; resolves once the turn took it; `options.senderId` as `run` takes it |
373
399
  | `abort()` | Stops the running turn |
374
400
  | `retry()` | Runs the latest turn again from your message |
375
401
  | `resume()` | Finishes a turn that did not finish |
@@ -387,6 +413,19 @@ prefix: a steer under the id of one of the conversation's blocks is refused.
387
413
  await conversation.steer('Use the staging database instead.', 'steer-ticket-42')
388
414
  ```
389
415
 
416
+ `conversation.fork` starts a new conversation with this one's history,
417
+ under `id` when you name one, which a retry repeats. `before` takes a turn
418
+ by its message's id, `turn.id` as `run` answers it or the id you sent it
419
+ under, and keeps everything before that message, the turn before it whole,
420
+ its commands' output and its reply among it. `through` keeps everything
421
+ through a completed text of the main agent, by its block's id.
422
+
423
+ ```ts
424
+ const turn = await conversation.run('Draft the reply to the customer.')
425
+ const retry = await conversation.fork({ before: turn.id })
426
+ await retry.run('Draft the reply, in a warmer tone.')
427
+ ```
428
+
390
429
  A message is text, or content blocks, such as a file on one of the core's
391
430
  devices:
392
431
 
@@ -467,11 +506,12 @@ the core, and the next action follows it again.
467
506
  the conversation's, and each subagent, with its role, phase and queue. The
468
507
  `agents` and `agent` events say they changed.
469
508
 
470
- `conversation.sendToAgent(node, text)` sends a message to one subagent, named
471
- by its id in `conversation.agents`. A subagent at work takes it as a steer, an
472
- idle one starts a turn with it, and one that ended is resumed with it. Give
473
- the message an id when you may send it again, such as after a lost answer:
474
- the subagent takes a message once, however often it arrives.
509
+ `conversation.sendToAgent(node, text, messageId?, options?)` sends a message
510
+ to one subagent, named by its id in `conversation.agents`. A subagent at work
511
+ takes it as a steer, an idle one starts a turn with it, and one that ended is
512
+ resumed with it. Give the message an id when you may send it again, such as
513
+ after a lost answer: the subagent takes a message once, however often it
514
+ arrives. `options.senderId` names who sent it, as `run` takes it.
475
515
 
476
516
  ```ts
477
517
  const tester = conversation.agents.find((agent) => agent.role.kind === 'subagent' && agent.role.profile === 'tester')
@@ -506,6 +546,8 @@ A new schedule starts from the defaults of `jush schedule create`, every day
506
546
  at 09:00, and is on unless the patch says `paused: true`. `create` and
507
547
  `update` answer the schedule with `secret`, a URL trigger's secret when the
508
548
  call made one, and null otherwise: it is the only time the secret is shown.
549
+ A URL trigger's address is your server's, which hands its calls to
550
+ `jush.schedules.call`: see [Your server's addresses](#your-servers-addresses).
509
551
 
510
552
  `runs(id, before?)` answers 50 runs at most, newest first; pass the last
511
553
  one's id as `before` for the 50 before it. Each run says what fired it
@@ -656,20 +698,239 @@ core.on('notify', async (message) => {
656
698
  `{ type: 'shown' }` says every place that would show it shows its
657
699
  conversation already.
658
700
 
701
+ ## Usage
702
+
703
+ `core.usage.report` sums the calls of a period by hour or by day of a time
704
+ zone, grouped by any of `conversation`, `sender`, `model`, `provider`,
705
+ `account`, `turn` and `purpose`:
706
+
707
+ ```ts
708
+ const report = await core.usage.report({
709
+ from: new Date('2026-11-01T00:00:00+08:00'),
710
+ to: new Date('2026-12-01T00:00:00+08:00'),
711
+ bucket: 'day',
712
+ timeZone: 'Asia/Shanghai',
713
+ groupBy: ['conversation', 'sender', 'model'],
714
+ })
715
+ for (const bucket of report.buckets) {
716
+ for (const group of bucket.groups) {
717
+ console.log(bucket.start, group.sender, group.model, group.calls, group.cost.usd, group.apiPrice)
718
+ }
719
+ }
720
+ ```
721
+
722
+ A group counts its calls and their tokens, `cost` is what the vendors said
723
+ the calls cost, by currency and never converted, and `apiPrice` what their
724
+ tokens would cost on the vendors' APIs in dollars, leaving out the
725
+ `unpricedCalls` whose model has no price. A bucket holds only the groups that
726
+ made calls, and a period without calls has no bucket; the first and the last
727
+ bucket start and end with the period, so two reports of adjoining periods
728
+ never hold the same bucket.
729
+
730
+ A call's sender is that of the latest message that handed its agent work,
731
+ by the `senderId` your program sent it with: a message, a steer, a side
732
+ question, or another agent's message; or that of the work whose end opened
733
+ its turn.
734
+
735
+ - Alice's message starts a turn: the turn's calls are Alice's, and so are
736
+ those of the turn that continues once a command it started ends.
737
+ - A subagent the agent starts while working for Alice works for Alice,
738
+ and its calls and those of the commands it runs are hers. So is the work
739
+ of a subagent or a conversation the agent sends a message to then.
740
+ - Bob steers the running turn: the calls after the agent read his steer
741
+ are Bob's.
742
+ - A turn that work for Alice wakes once it ends is Alice's, even after
743
+ Bob's message: a command started in her turn, a subagent working for
744
+ her, or another conversation her turn sent a message to with
745
+ `--wake-when-done`. What ends while Bob's turn runs joins that turn,
746
+ which stays Bob's.
747
+
748
+ After a message that names no sender, as one from the app or from a
749
+ schedule, the calls have a null sender, and so does a turn that the end of
750
+ work it started opens. So do the calls outside an agent's work, such as a
751
+ title's or the suggested replies after a turn.
752
+
753
+ ## Everything else a core does
754
+
755
+ Every capability of a core has a method, on the object it acts on, and its
756
+ input and answer types are exported from `@jush-agent/sdk`:
757
+
758
+ ```ts
759
+ // The root agent's transcript, whose id is the conversation's.
760
+ const page = await conversation.history.transcript(conversation.id, { latest: true })
761
+ const changed = await conversation.changes.status()
762
+ const token = await core.credentials.create({
763
+ name: 'Deploy Token',
764
+ fields: [{ name: 'token', label: 'Token', kind: 'secret', value: deployToken }],
765
+ keep: 'always',
766
+ })
767
+ ```
768
+
769
+ | Object | Methods |
770
+ | --- | --- |
771
+ | `core.conversations` | `list({ archived })`, `get(id)`, `create`, `open`, `remove(id)`, `updateMany(items)` |
772
+ | `conversation` | `get()`, `update`, `remove()`, `fork`, `reloadPlugins()`, `search(text)`, `cost('hour' \| 'day')`, `workflows()`, `unpin(pin)`, `detach(device)` |
773
+ | `conversation.history` | `transcript(agent, page)`, `block(agent, id)`, `turn(id)`, `commandOrigin(id)`, `commandOutput(id)` |
774
+ | `conversation.sideQuestions` | `ask`, `followUp`, `retry`, `stop`, `fold`, `remove` |
775
+ | `conversation.files` | `list`, `lists`, `read`, `readBytes`, `write`, `makeDirectory`, `remove`, `search` |
776
+ | `conversation.changes` | `status`, `file`, `committed`, `branches`, `repositories`, `log`, `show(commit)` |
777
+ | `conversation.plugins` | `state(plugin)`, `call(plugin, method, input)` |
778
+ | `core` | `search(text)`, `blob(sha256)`, `upload` |
779
+ | `core.usage` | `totals()`, `report(options)` |
780
+ | `core.models` | `search(text)`, `refresh()` |
781
+ | `core.providers` | `list`, `create`, `update`, `remove`, `catalog`, `order`, `status`, `quota`, `test`, `cli`, `installCli`, `accounts(id)`, `signIn` |
782
+ | `core.credentials` | `list`, `create`, `update`, `remove`, `reveal(id, field)` |
783
+ | `core.instructions` | `get()`, `set(text)` |
784
+ | `core.subagents` | `get()`, `update(patch)`, `profiles.create`, `profiles.update`, `profiles.remove` |
785
+ | `core.plugins` | `enable(plugin, on)`, `enableFeature(plugin, feature, on)`, `call(plugin, method, input)` |
786
+ | `core.workspaces` | `list`, `create`, `update`, `remove`, `order`, `move` |
787
+ | `core.projects` | `list`, `create`, `update`, `remove` |
788
+ | `core.devices` | `list`, `create`, `claim`, `of(listed)` |
789
+ | `device` | `connect`, `spawn`, `exec`, `revoke`, `update`, `elevate`, `workflows(directory)` |
790
+ | `device.files` | `list(path)`, `makeDirectory(path)` |
791
+ | `core.portForwards` | `list`, `create`, `update`, `remove`, `retry` |
792
+
659
793
  ## Showing conversations on your pages
660
794
 
661
795
  [`@jush-agent/web-components`](../web-components/README.md) shows a
662
796
  conversation on your own pages with the app's own views: its transcript,
663
- its composer, and its work in windows with a taskbar.
664
- Your server forwards the pages' requests with `core.fetch(request, base)`
665
- and their connection with `core.accept(socket, url)`, which takes a
666
- WebSocket of the standard interface, as Deno's `upgradeWebSocket` and the
667
- `ws` package give.
668
-
669
- Jush can reach your program from outside when you tell it where your server
670
- forwards its routes: `Jush.start({ publicUrl: 'https://bot.example.com/jush' })`.
671
- A schedule's URL is then `/hooks/{id}` below it, which your server answers
672
- with `jush.fetch(request, '/jush')`, and a plugin's sign-in returns there.
797
+ its composer, and its work in windows with a taskbar. The element names an
798
+ address of your server, `<jush-conversation src="/chats/42/jush">`, and your
799
+ server answers it below that address after its own sign-in:
800
+
801
+ ```ts
802
+ async function answer(request: Request): Promise<Response> {
803
+ const chat = await signedInChat(request) // your sign-in, your chat
804
+ const page = core.conversations.open(chat.conversation).page({
805
+ base: `/chats/${chat.id}/jush`,
806
+ allow: ['send', 'steer', 'stop', 'files', 'browser'],
807
+ })
808
+ if (request.headers.get('upgrade') === 'websocket') {
809
+ const { socket, response } = Deno.upgradeWebSocket(request)
810
+ page.accept(socket, request.url)
811
+ return response
812
+ }
813
+ return page.fetch(request)
814
+ }
815
+ ```
816
+
817
+ - The page reaches that conversation alone: its transcript, its agents,
818
+ the media its messages name, and the windows `allow` names. It never
819
+ reaches another conversation, the providers, credentials or settings.
820
+ - `allow` lists what the page may do beyond reading, each a
821
+ `PageOperation`: `send`, `attach`, `steer`, `edit`, `stop`, `retry`,
822
+ `compact`, `clear`, `ask`, `messageAgents`, `decide`, `fork`, `delete`,
823
+ `rename`, `chooseModel`, `chooseDevice`, `chooseWorkflow`, `pins`,
824
+ `schedules`, and the windows `files`, `writeFiles`, `changes`, `browser`,
825
+ `simulators`, `plugins`. The page shows no control of the rest, and the
826
+ core refuses it. An operation a later version adds is on no page until you
827
+ name it.
828
+ - `page.accept` takes a WebSocket of the standard interface, as Deno's
829
+ `upgradeWebSocket` and the `ws` package give. Make a page for each
830
+ request: it holds nothing, so each viewer's can name other operations. A
831
+ connection keeps the operations it opened with.
832
+ - The paths below `base` are the element's and the core's own; pass them on
833
+ as they come.
834
+ - A file the Files window previews loads in a frame that sends no cookie,
835
+ below `{base}/html/`, with its own token in the path: hand those requests
836
+ to the chat's page before your sign-in.
837
+
838
+ ## Your server's addresses
839
+
840
+ Two kinds of call come from outside, and reach your server, not Jush: a
841
+ system that calls a schedule's URL, and a plugin's vendor that sends the
842
+ browser back once the user signed in, such as Exa for the `web` plugin's
843
+ search. Tell Jush where your server takes each, as URI templates whose
844
+ `{name}`s Jush fills:
845
+
846
+ ```ts
847
+ const jush = await Jush.start({
848
+ plugins: [chat],
849
+ addresses: {
850
+ schedule: 'https://bot.example.com/hooks/{id}',
851
+ signInReturn: 'https://bot.example.com/sign-in/{core}/{plugin}',
852
+ },
853
+ })
854
+ ```
855
+
856
+ - `schedule` holds `{id}`, the schedule's id. `jush schedule create --url`
857
+ and `jush schedule show` print the address filled.
858
+ - `signInReturn` may hold `{core}`, the core's id (`core.id`), and
859
+ `{plugin}`, the plugin's: leave out what your server knows from its own
860
+ session. The core registers the address with the vendor itself.
861
+ - A template that holds another name or a fragment (`#…`), or that is no
862
+ `http` or `https` URL once filled, fails `Jush.start` and
863
+ `Jush.connect`. Without `schedule`, a URL schedule has no address;
864
+ without `signInReturn`, a plugin's sign-in is refused.
865
+ - Jush fills a template each time it writes the address: change it, and
866
+ the address of every URL schedule changes with it, its secret kept.
867
+
868
+ Your server answers each call at its route, and hands it to Jush. With
869
+ `Bun.serve`, as with any server that gives a standard `Request`:
870
+
871
+ ```ts
872
+ const text = (body: string) => new Response(body, { headers: { 'content-type': 'text/plain; charset=utf-8' } })
873
+
874
+ Bun.serve({
875
+ routes: {
876
+ '/hooks/:id': {
877
+ POST: async (request) => {
878
+ const called = await jush.schedules.call(request.params.id, request)
879
+ if (called.type === 'taken') {
880
+ return new Response(null, { status: 202 })
881
+ }
882
+ console.info('A schedule call was refused', request.params.id, called.reason)
883
+ const status = { unknown: 404, secret: 404, tooMany: 429, tooLarge: 413 }[called.reason]
884
+ return new Response(null, { status })
885
+ },
886
+ },
887
+ '/sign-in/:core/:plugin': async (request) => {
888
+ const { core, plugin } = request.params
889
+ const query = new URL(request.url).searchParams
890
+ const done = await jush.plugins.completeSignIn({ core, plugin, query })
891
+ return text(done.type === 'signedIn' ? `You're signed in to ${done.vendor}.` : "The sign-in didn't finish. Try again.")
892
+ },
893
+ },
894
+ })
895
+ ```
896
+
897
+ `jush.schedules.call(id, request)` passes the call to the open core that
898
+ holds the schedule as it came, so a GitHub signature over its body
899
+ verifies. It answers `{ type: 'taken' }` or `{ type: 'refused', reason }`:
900
+
901
+ | `reason` | When |
902
+ | --- | --- |
903
+ | `unknown` | No open core holds a URL schedule of the id. |
904
+ | `secret` | The call carries the secret neither as `Authorization: Bearer` nor in GitHub's `X-Hub-Signature-256`. |
905
+ | `tooMany` | The schedule took 30 calls in the last minute. |
906
+ | `tooLarge` | The body is over 1 MiB, or the headers are over 64 KiB. |
907
+
908
+ - `taken` means the schedule keeps the call, not that a run started: a run
909
+ starts 30 seconds after the last call of a burst, or 2 minutes after its
910
+ first; a call whose `X-GitHub-Delivery` or `Idempotency-Key` came in the
911
+ last hour is answered as the first; a paused schedule keeps its calls.
912
+ The runs are in `conversation.schedules.runs(id)`.
913
+ - Answer `unknown` and `secret` alike, as above, so a caller cannot tell
914
+ which addresses exist. Limit how often one caller may call addresses
915
+ that are none, as you limit your other routes: Jush does not see the
916
+ caller's address.
917
+ - Your route decides which methods it takes: Jush checks a call whatever
918
+ its method.
919
+ - A call whose body breaks off, as when its sender goes away, fails with
920
+ the body's error, as reading the request would; one whose body stops
921
+ coming for a minute fails with a `JushError` whose `code` is
922
+ `transfer_stalled`.
923
+
924
+ `jush.plugins.completeSignIn({ core, plugin, query })` takes the query the
925
+ browser came back with, as `URLSearchParams`, a string or a record. It
926
+ answers `{ type: 'signedIn', vendor }` or `{ type: 'refused', reason }`:
927
+
928
+ | `reason` | When |
929
+ | --- | --- |
930
+ | `unknownPlugin` | The core has no plugin of the id that signs in, or has it off. |
931
+ | `expired` | No open core of the id started the sign-in, or it started over 10 minutes ago, or the sign-in ended already. |
932
+ | `declined` | The vendor sent the browser back with an error, such as the user's refusal. |
933
+ | `vendorFailed` | The vendor issued no tokens. |
673
934
 
674
935
  ## Errors
675
936