Bläck StudioPlugin SDK v1.0.0 · API 10
Contents

Start

Building

Beyond Bläck Studio

How it runs

Sharing

Look up

The network

A plugin reaches the internet only through this.app.net, and only the hosts that its manifest names (network:api.example.com) and the writer allowed. Requests go over https. Plain http is allowed only to this computer (localhost, 127.0.0.1), so you can try a plugin against a server you run yourself. Redirects aren't followed.

app.net.fetch(url, init?) gets the whole answer, up to 20 MB, within 30 seconds:

const res = await this.app.net.fetch('https://api.example.com/words', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ text }),
})
if (res.ok) console.log(res.json())   // also res.text(), res.bytes(), res.status, res.headers

app.net.stream(url, init, onChunk) passes the answer on as it arrives, giving each piece to onChunk as text. This suits an AI's reply, word by word. When the answer is complete, stream resolves with its status and headers. It can run for up to ten minutes and receive up to 200 MB.

app.net.streamBytes(url, init, onChunk) does the same with each piece as bytes, for what isn't text (API 10).

An address the writer enters, such as a repository's, can't be named in the manifest. List "network:ask" in optionalPermissions, and ask for the exact address when it's needed: await this.app.permissions.request('address:https://git.example.com/me/books'). The writer is asked about that address, in Bläck Studio's own window. The plugin can then reach that address and what's under it, and no other (API 10).

const status = this.addStatusBarItem({ id: 'reply', text: '' })
let reply = ''
const res = await this.app.net.stream(
  'https://api.example.com/v1/chat',
  { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt, stream: true }) },
  (text) => {
    reply += text
    status.setText(reply.slice(-40))
  },
)
if (!res.ok) this.app.ui.notify(`The service answered ${res.status}`)

Each piece is the text as it arrived, not a whole line or message. The data: lines of an event stream can be split across pieces, so collect them before you read them.

There is one catch with a text/plain answer. Unless it has the header X-Content-Type-Options: nosniff, the first 512 bytes may arrive as one piece, because Chromium reads that much to guess the type of the answer. Event streams (text/event-stream, which AI services send) and JSON arrive as they're sent.

Both work the same way in a sandboxed plugin. A plugin can't open a WebSocket.