@jush-agent/sdk 0.2.6332 → 0.2.6346

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
@@ -629,6 +671,39 @@ for one, and then fails, telling the agent that the plugin's program is
629
671
  unreachable. A `context` call does not wait: when your program stops
630
672
  during one, the agent goes on without its block.
631
673
 
674
+ ### Versions
675
+
676
+ Several builds of your program can serve its plugins at once, such as a
677
+ production node and a canary node connected to one process, each
678
+ conversation running on one of them. A connection with `pluginVersion`
679
+ declares its plugins as that version; one without it, as their current
680
+ versions:
681
+
682
+ ```ts
683
+ // The canary node.
684
+ const jush = await Jush.connect({ url, token, plugins: [acme], pluginVersion: 'canary-1126' })
685
+
686
+ // A conversation moves to the canary, and back to the current versions.
687
+ await conversation.update({ pluginVersions: { acme: 'canary-1126' } })
688
+ await conversation.update({ pluginVersions: {} })
689
+ ```
690
+
691
+ A conversation's agents run the commands of the version it names of the
692
+ plugin, with their `--help`, the index of its system prompt and its
693
+ context; a plugin it names none of runs on its current version. Everything
694
+ else of a plugin is its current version's: what it keeps of a conversation
695
+ as it is forked or deleted, a project's search, and its sign-ins. A core's
696
+ notifications go to a connection of the current versions while one is
697
+ open. A change of `pluginVersions` gives its agents their commands at
698
+ once; a command already running goes on where it is. The core refuses
699
+ versions whose commands take a name another plugin's take, and a version
700
+ of a plugin of its own. A version no connection declares leaves the
701
+ conversation without that plugin's commands, and while no connection of a
702
+ version is open, its commands wait 10 seconds and fail, as above. A
703
+ plugin's store, secrets and sign-ins, and the user's switches of it, are
704
+ the plugin's, whichever version reads them. A process of a release before
705
+ versions refuses a connection with `pluginVersion`.
706
+
632
707
  ## Product state and notifications
633
708
 
634
709
  `core.state` is the core's product state: its workspaces, projects, provider
@@ -656,20 +731,326 @@ core.on('notify', async (message) => {
656
731
  `{ type: 'shown' }` says every place that would show it shows its
657
732
  conversation already.
658
733
 
734
+ ## Usage
735
+
736
+ `core.usage.report` sums the calls of a period by minute, hour or day of a
737
+ time zone, grouped by any of `conversation`, `agent`, `sender`, `model`,
738
+ `provider`, `account`, `turn`, `assignment` and `purpose`:
739
+
740
+ ```ts
741
+ const report = await core.usage.report({
742
+ from: new Date('2026-11-01T00:00:00+08:00'),
743
+ to: new Date('2026-12-01T00:00:00+08:00'),
744
+ bucket: 'day',
745
+ timeZone: 'Asia/Shanghai',
746
+ groupBy: ['conversation', 'sender', 'model'],
747
+ })
748
+ for (const bucket of report.buckets) {
749
+ for (const group of bucket.groups) {
750
+ console.log(bucket.start, group.sender, group.model, group.calls, group.cost.usd, group.apiPrice)
751
+ }
752
+ }
753
+ ```
754
+
755
+ A group counts its calls and their tokens, `cost` is what the vendors said
756
+ the calls cost, by currency and never converted, and `apiPrice` what their
757
+ tokens would cost on the vendors' APIs in dollars, leaving out the
758
+ `unpricedCalls` whose model has no price. A bucket holds only the groups that
759
+ made calls, and a period without calls has no bucket; the first and the last
760
+ bucket start and end with the period, so two reports of adjoining periods
761
+ never hold the same bucket.
762
+
763
+ `conversations` keeps the calls of the conversations it names, and
764
+ `agents` those of the agents of them it names; an empty list keeps none.
765
+ An agent is the number its conversation knows it by: 0 for the main agent,
766
+ and a subagent's `number` in `conversation.agents`, whose `parent` names
767
+ the agent that started it, once the conversation is open
768
+ (`await conversation.open()`). To count what a subagent did, name it and
769
+ the subagents below it; a call in a minute bucket falls in the minute it
770
+ ended:
771
+
772
+ ```ts
773
+ // Agent 3 and every subagent below it, by number.
774
+ const agents = conversation.agents
775
+ const below = (parent: string): number[] => agents.flatMap((each) =>
776
+ each.role.kind === 'subagent' && each.role.parent === parent ? [each.role.number, ...below(each.id)] : [])
777
+ const reviewer = agents.find((each) => each.role.kind === 'subagent' && each.role.number === 3)!
778
+ const review = await core.usage.report({
779
+ from: startedAt,
780
+ to: new Date(),
781
+ bucket: 'minute',
782
+ conversations: [conversation.id],
783
+ agents: [3, ...below(reviewer.id)],
784
+ groupBy: ['agent', 'model'],
785
+ })
786
+ ```
787
+
788
+ `agent` is null for a side question's call and for one no agent made, such
789
+ as a title's, and `agents` keeps neither.
790
+
791
+ A call's sender is that of the latest message that handed its agent work,
792
+ by the `senderId` your program sent it with: a message, a steer, a side
793
+ question, or another agent's message; or that of the work whose end opened
794
+ its turn.
795
+
796
+ - Alice's message starts a turn: the turn's calls are Alice's, and so are
797
+ those of the turn that continues once a command it started ends.
798
+ - A subagent the agent starts while working for Alice works for Alice,
799
+ and its calls and those of the commands it runs are hers. So is the work
800
+ of a subagent or a conversation the agent sends a message to then.
801
+ - Bob steers the running turn: the calls after the agent read his steer
802
+ are Bob's.
803
+ - A turn that work for Alice wakes once it ends is Alice's, even after
804
+ Bob's message: a command started in her turn, a subagent working for
805
+ her, or another conversation her turn sent a message to with
806
+ `--wake-when-done`. What ends while Bob's turn runs joins that turn,
807
+ which stays Bob's.
808
+ - A schedule the agent makes while working for Alice runs for Alice.
809
+
810
+ After a message that names no sender, as one from the app or from a
811
+ schedule your program or the user made, the calls have a null sender, and
812
+ so does a turn that the end of work it started opens. So do the calls
813
+ outside an agent's work, such as a title's or the suggested replies after
814
+ a turn.
815
+
816
+ A call's `assignment` is the id of the message that handed its agent the
817
+ work: the id you sent the message under, a side question's or its
818
+ follow-up's (`sideQuestions.followUp(side, input, messageId)`), or a
819
+ schedule run's `turn`. The turns that continue that work share it, as they
820
+ share its sender: those a background command's end, a subagent's end or
821
+ another conversation's end opens, a retry, a resume or a compaction, and
822
+ the runs of a schedule the agent made then. A steer joins the running
823
+ turn's assignment. What continues work that names no assignment, such as
824
+ work begun before assignments were kept, names none either. So a report
825
+ grouped by `assignment` sums each piece of work, and a program that names
826
+ its messages by what started them, such as `listener:ops-alerts:8812`,
827
+ sums the cost of each kind:
828
+
829
+ ```ts
830
+ await conversation.send('Summarize the incident', 'listener:ops-alerts:8812')
831
+ const report = await core.usage.report({ from, to, bucket: 'day', timeZone: 'Asia/Shanghai', groupBy: ['conversation', 'assignment', 'model'] })
832
+ ```
833
+
834
+ ## Commands your agents ran
835
+
836
+ `core.commands.history` reads the core's command history, in the order its
837
+ rows were written, which is the order their commands ended. Each command
838
+ an agent's `shell` call started has a row once it ends, however long after
839
+ the call: it exited with its code, was stopped, or was lost. A call that
840
+ returned an error before its command started, such as one that named a
841
+ device that is offline, has a row too, `not_started` with the error.
842
+
843
+ ```ts
844
+ let page = await core.commands.history({ from: new Date(Date.now() - 30 * 86_400_000), limit: 500 })
845
+ for (const command of page.commands) {
846
+ // command.conversation, command.agent (0 for the main agent, null for a
847
+ // side question), command.command, command.device, command.turn,
848
+ // command.sender, command.assignment, command.title, command.script,
849
+ // command.startedAt (a wait for its device included), command.endedAt,
850
+ // and command.end:
851
+ // { kind: 'exited', exitCode } | { kind: 'stopped' } | { kind: 'lost', reason } | { kind: 'not_started', reason }
852
+ }
853
+ // Later, from where the last page ended:
854
+ page = await core.commands.history({ after: page.next })
855
+ ```
856
+
857
+ `after` reads on after a page's `next`; `from` starts at the first row
858
+ written of those that ended at or after a moment; with neither, the first
859
+ row. A page holds `limit` rows, 1 to 1,000 and 100 by default, and `more`
860
+ says rows wait after it. `next` is always where to read on from, also for
861
+ an empty page, so a reader that keeps it and follows it reads each row
862
+ once, also rows written while it reads. It is opaque: keep it as it is. A
863
+ cursor past every row this core's history wrote, such as one of another
864
+ core, is refused.
865
+
866
+ A row can be read a moment before `conversation.history.commandOutput` has
867
+ its command's output. A conversation's deletion removes its rows, which
868
+ hold its scripts. Commands that ended before the core kept the history
869
+ have none, and so do `device.exec` and a schedule's watch command, which
870
+ no agent ran.
871
+
872
+ ## Everything else a core does
873
+
874
+ Every capability of a core has a method, on the object it acts on, and its
875
+ input and answer types are exported from `@jush-agent/sdk`:
876
+
877
+ ```ts
878
+ // The root agent's transcript, whose id is the conversation's.
879
+ const page = await conversation.history.transcript(conversation.id, { latest: true })
880
+ const changed = await conversation.changes.status()
881
+ const token = await core.credentials.create({
882
+ name: 'Deploy Token',
883
+ fields: [{ name: 'token', label: 'Token', kind: 'secret', value: deployToken }],
884
+ keep: 'always',
885
+ })
886
+ ```
887
+
888
+ | Object | Methods |
889
+ | --- | --- |
890
+ | `core.conversations` | `list({ archived })`, `get(id)`, `create`, `open`, `remove(id)`, `updateMany(items)` |
891
+ | `conversation` | `get()`, `update`, `remove()`, `fork`, `reloadPlugins()`, `search(text)`, `cost('hour' \| 'day')`, `workflows()`, `unpin(pin)`, `detach(device)` |
892
+ | `conversation.history` | `transcript(agent, page)`, `block(agent, id)`, `turn(id)`, `commandOrigin(id)`, `commandOutput(id)` |
893
+ | `conversation.sideQuestions` | `ask`, `followUp`, `retry`, `stop`, `fold`, `remove` |
894
+ | `conversation.files` | `list`, `lists`, `read`, `readBytes`, `write`, `makeDirectory`, `remove`, `search` |
895
+ | `conversation.changes` | `status`, `file`, `committed`, `branches`, `repositories`, `log`, `show(commit)` |
896
+ | `conversation.plugins` | `state(plugin)`, `call(plugin, method, input)` |
897
+ | `core` | `search(text)`, `blob(sha256)`, `upload` |
898
+ | `core.usage` | `totals()`, `report(options)` |
899
+ | `core.commands` | `history(options)` |
900
+ | `core.models` | `search(text)`, `refresh()` |
901
+ | `core.providers` | `list`, `create`, `update`, `remove`, `catalog`, `order`, `status`, `quota`, `test`, `cli`, `installCli`, `accounts(id)`, `signIn` |
902
+ | `core.credentials` | `list`, `create`, `update`, `remove`, `reveal(id, field)` |
903
+ | `core.instructions` | `get()`, `set(text)` |
904
+ | `core.subagents` | `get()`, `update(patch)`, `profiles.create`, `profiles.update`, `profiles.remove` |
905
+ | `core.plugins` | `enable(plugin, on)`, `enableFeature(plugin, feature, on)`, `call(plugin, method, input)` |
906
+ | `core.workspaces` | `list`, `create`, `update`, `remove`, `order`, `move` |
907
+ | `core.projects` | `list`, `create`, `update`, `remove` |
908
+ | `core.devices` | `list`, `create`, `claim`, `of(listed)` |
909
+ | `device` | `connect`, `spawn`, `exec`, `revoke`, `update`, `elevate`, `workflows(directory)` |
910
+ | `device.files` | `list(path)`, `makeDirectory(path)` |
911
+ | `core.portForwards` | `list`, `create`, `update`, `remove`, `retry` |
912
+
659
913
  ## Showing conversations on your pages
660
914
 
661
915
  [`@jush-agent/web-components`](../web-components/README.md) shows a
662
916
  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.
917
+ its composer, and its work in windows with a taskbar. The element names an
918
+ address of your server, `<jush-conversation src="/chats/42/jush">`, and your
919
+ server answers it below that address after its own sign-in:
920
+
921
+ ```ts
922
+ async function answer(request: Request): Promise<Response> {
923
+ const chat = await signedInChat(request) // your sign-in, your chat
924
+ const page = core.conversations.open(chat.conversation).page({
925
+ base: `/chats/${chat.id}/jush`,
926
+ allow: ['send', 'steer', 'stop', 'files', 'browser'],
927
+ })
928
+ if (request.headers.get('upgrade') === 'websocket') {
929
+ const { socket, response } = Deno.upgradeWebSocket(request)
930
+ page.accept(socket, request.url)
931
+ return response
932
+ }
933
+ return page.fetch(request)
934
+ }
935
+ ```
936
+
937
+ - The page reaches that conversation alone: its transcript, its agents,
938
+ the media its messages name, and the windows `allow` names. It never
939
+ reaches another conversation, the providers, credentials or settings.
940
+ - `allow` lists what the page may do beyond reading, each a
941
+ `PageOperation`: `send`, `attach`, `steer`, `edit`, `stop`, `retry`,
942
+ `compact`, `clear`, `ask`, `messageAgents`, `decide`, `fork`, `delete`,
943
+ `rename`, `chooseModel`, `chooseDevice`, `chooseWorkflow`, `pins`,
944
+ `schedules`, and the windows `files`, `writeFiles`, `changes`, `browser`,
945
+ `simulators`, `plugins`. The page shows no control of the rest, and the
946
+ core refuses it. An operation a later version adds is on no page until you
947
+ name it.
948
+ - `page.accept` takes a WebSocket of the standard interface, as Deno's
949
+ `upgradeWebSocket` and the `ws` package give. Make a page for each
950
+ request: it holds nothing, so each viewer's can name other operations. A
951
+ connection keeps the operations it opened with.
952
+ - The paths below `base` are the element's and the core's own; pass them on
953
+ as they come.
954
+ - A file the Files window previews loads in a frame that sends no cookie,
955
+ below `{base}/html/`, with its own token in the path: hand those requests
956
+ to the chat's page before your sign-in.
957
+
958
+ ## Your server's addresses
959
+
960
+ Two kinds of call come from outside, and reach your server, not Jush: a
961
+ system that calls a schedule's URL, and a plugin's vendor that sends the
962
+ browser back once the user signed in, such as Exa for the `web` plugin's
963
+ search. Tell Jush where your server takes each, as URI templates whose
964
+ `{name}`s Jush fills:
965
+
966
+ ```ts
967
+ const jush = await Jush.start({
968
+ plugins: [chat],
969
+ addresses: {
970
+ schedule: 'https://bot.example.com/hooks/{id}',
971
+ signInReturn: 'https://bot.example.com/sign-in/{core}/{plugin}',
972
+ },
973
+ })
974
+ ```
975
+
976
+ - `schedule` holds `{id}`, the schedule's id. `jush schedule create --url`
977
+ and `jush schedule show` print the address filled.
978
+ - `signInReturn` may hold `{core}`, the core's id (`core.id`), and
979
+ `{plugin}`, the plugin's: leave out what your server knows from its own
980
+ session. The core registers the address with the vendor itself.
981
+ - A template that holds another name or a fragment (`#…`), or that is no
982
+ `http` or `https` URL once filled, fails `Jush.start` and
983
+ `Jush.connect`. Without `schedule`, a URL schedule has no address;
984
+ without `signInReturn`, a plugin's sign-in is refused.
985
+ - Jush fills a template each time it writes the address: change it, and
986
+ the address of every URL schedule changes with it, its secret kept.
987
+
988
+ Your server answers each call at its route, and hands it to Jush. With
989
+ `Bun.serve`, as with any server that gives a standard `Request`:
990
+
991
+ ```ts
992
+ const text = (body: string) => new Response(body, { headers: { 'content-type': 'text/plain; charset=utf-8' } })
993
+
994
+ Bun.serve({
995
+ routes: {
996
+ '/hooks/:id': {
997
+ POST: async (request) => {
998
+ const called = await jush.schedules.call(request.params.id, request)
999
+ if (called.type === 'taken') {
1000
+ return new Response(null, { status: 202 })
1001
+ }
1002
+ console.info('A schedule call was refused', request.params.id, called.reason)
1003
+ const status = { unknown: 404, secret: 404, tooMany: 429, tooLarge: 413 }[called.reason]
1004
+ return new Response(null, { status })
1005
+ },
1006
+ },
1007
+ '/sign-in/:core/:plugin': async (request) => {
1008
+ const { core, plugin } = request.params
1009
+ const query = new URL(request.url).searchParams
1010
+ const done = await jush.plugins.completeSignIn({ core, plugin, query })
1011
+ return text(done.type === 'signedIn' ? `You're signed in to ${done.vendor}.` : "The sign-in didn't finish. Try again.")
1012
+ },
1013
+ },
1014
+ })
1015
+ ```
1016
+
1017
+ `jush.schedules.call(id, request)` passes the call to the open core that
1018
+ holds the schedule as it came, so a GitHub signature over its body
1019
+ verifies. It answers `{ type: 'taken' }` or `{ type: 'refused', reason }`:
1020
+
1021
+ | `reason` | When |
1022
+ | --- | --- |
1023
+ | `unknown` | No open core holds a URL schedule of the id. |
1024
+ | `secret` | The call carries the secret neither as `Authorization: Bearer` nor in GitHub's `X-Hub-Signature-256`. |
1025
+ | `tooMany` | The schedule took 30 calls in the last minute. |
1026
+ | `tooLarge` | The body is over 1 MiB, or the headers are over 64 KiB. |
1027
+
1028
+ - `taken` means the schedule keeps the call, not that a run started: a run
1029
+ starts 30 seconds after the last call of a burst, or 2 minutes after its
1030
+ first; a call whose `X-GitHub-Delivery` or `Idempotency-Key` came in the
1031
+ last hour is answered as the first; a paused schedule keeps its calls.
1032
+ The runs are in `conversation.schedules.runs(id)`.
1033
+ - Answer `unknown` and `secret` alike, as above, so a caller cannot tell
1034
+ which addresses exist. Limit how often one caller may call addresses
1035
+ that are none, as you limit your other routes: Jush does not see the
1036
+ caller's address.
1037
+ - Your route decides which methods it takes: Jush checks a call whatever
1038
+ its method.
1039
+ - A call whose body breaks off, as when its sender goes away, fails with
1040
+ the body's error, as reading the request would; one whose body stops
1041
+ coming for a minute fails with a `JushError` whose `code` is
1042
+ `transfer_stalled`.
1043
+
1044
+ `jush.plugins.completeSignIn({ core, plugin, query })` takes the query the
1045
+ browser came back with, as `URLSearchParams`, a string or a record. It
1046
+ answers `{ type: 'signedIn', vendor }` or `{ type: 'refused', reason }`:
1047
+
1048
+ | `reason` | When |
1049
+ | --- | --- |
1050
+ | `unknownPlugin` | The core has no plugin of the id that signs in, or has it off. |
1051
+ | `expired` | No open core of the id started the sign-in, or it started over 10 minutes ago, or the sign-in ended already. |
1052
+ | `declined` | The vendor sent the browser back with an error, such as the user's refusal. |
1053
+ | `vendorFailed` | The vendor issued no tokens. |
673
1054
 
674
1055
  ## Errors
675
1056