Long sse-live chats held INVOKE_LOCK indefinitely and starved short routes (UI freeze). Timed try-lock returns interpreter busy → HTTP 503; ADR 0008 records that HTTP is multi-goroutine while Marqdo evaluation stays single-flight. Also lands plan/delta streaming and soft map guards used by qdagent chat. Co-authored-by: Cursor <cursoragent@cursor.com>
5.6 KiB
markup, title, description, import
| markup | title | description | import | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| occlusion | ext/ai/llm/openai | OpenAI-compatible chat completions transport (HTTP + SSE). Prefers optional plugins/llm ABI when present; falls back to lib/net. Used by ext/ai/llm; authors should prefer llm.ask / llm.stream. Result packs are GFM tables / table.put (code-as-documentation). |
|
usage_from
+ `data`
Extract {prompt_tokens,completion_tokens,total_tokens} from an OpenAI-shaped response map.
u = > json.get value=data key="usage"
upt = > json.get value=ukey="prompt_tokens" ct = > json.get value=ukey="completion_tokens" tt = > json.get value=ukey="total_tokens"- not
ptpt = 0 -
**_ = 1**
- not
ctct = 0 -
**_ = 1**
- not
tttt = pt + ct -
**_ = 1**
pt = 0 ct = 0 tt = 0
usage =
| prompt_tokens | completion_tokens | total_tokens |
|---|---|---|
pt |
ct |
tt |
usage
tool_calls_from
+ `data`
Extract OpenAI-shaped choices[0].message.tool_calls as a list (or empty list).
choices = > json.get value=data key="choices"
- not
choices> table.empty_list -
c0 = 1)
msg = > json.get value=c0 key="message"
- not
msg> table.empty_list -
**tc = > json.get value=`msg` key="tool_calls"**
1. `tc`
*tc*
2. *
*> table.empty_list*
chat_completions
+ `base_url`
+ `api_key`
+ `model`
+ `prompt`=None
+ `messages`=None
+ `selection`=None
+ `strict_selection`=False
+ `auth_mode`=None
+ `stream`=False
+ `echo`=False
+ `stream_kind`="delta"
+ `suffix`=/chat/completions
+ `bearer`="Bearer "
POST /chat/completions (or stream SSE). Prefers plugins/llm ABI (llm_chat_completions) when the native plugin is resolvable; otherwise lib/net HTTP/SSE. Non-stream → {text,usage,finish,tool_calls}; stream → {events,finish}. Soft transport / HTTP failures return finish=error packs (never abort the VM).
Pass either messages= (role/content list) or legacy prompt= (single user message). Optional selection= is a ModelSelectionV1 snapshot; strict_selection=True refuses env fallbacks inside the plugin.
stream_kind tags live EventBus frames: delta (answer tokens) or plan (parent protocol — UI process trail, not the bubble).
p = > plugin.native_path name="llm"
p
plugin.load path=
p> llm_chat_completions base_url=base_urlapi_key=api_keymodel=modelprompt=promptmessages=messagesselection=selectionstrict_selection=strict_selectionauth_mode=auth_modestream=streamecho=echostream_kind=stream_kindsuffix=suffixbearer=bearer
url = base_url + suffix
auth = bearer + api_key
headers = > table.put in=None at="Authorization" value=auth
- not
messagesmessages=
| @ | role | content |
|---|------|---------|
| 1 | user | `prompt` |
**_ = 1**
req =
| model | messages |
|---|---|
model |
messages |
-
streamreq = > table.put in=reqat="stream" value=True body = > json.stringify value=reqresp = > net.http_post_sse url=urlbody=bodyheaders=headersecho=echost = > json.get value=respkey="status" 1.st== 200 evs = > json.get value=respkey="events"pack=finish events stop evspack 2. * Soft transport / HTTP failure — structured pack, never abort the VM. msg = "HTTP " +
stevs = > json.get value=respkey="events"evse0 = > at value=evsindex=0 m2 = > json.get value=e0key="message"m2msg =m2pack=
finish text error events error "" msgevspack
-
**body = > json.stringify value=`req`**
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
**st = > json.get value=`resp` key="status"**
1. `st` == 200
**raw = > json.get value=`resp` key="body"**
**data = > json.parse text=`raw`**
**choices = > json.get value=`data` key="choices"**
**choice0 = > at value=`choices` index=0**
**msg = > json.get value=`choice0` key="message"**
**text = > json.get value=`msg` key="content"**
1. not `text`
**text = ""**
2. *
**_ = 1**
**usage = > usage_from data=`data`**
**fr = > json.get value=`choice0` key="finish_reason"**
1. not `fr`
**fr = "stop"**
2. *
**_ = 1**
**tc = > tool_calls_from data=`data`**
`pack` =
| finish | text | usage | tool_calls |
|--------|------|-------|------------|
| `fr` | `text` | `usage` | `tc` |
*pack*
2. *
Soft transport / HTTP failure — structured pack, never abort the VM.
**raw = > json.get value=`resp` key="body"**
**msg = "HTTP " + `st`**
1. `raw`
**msg = `msg` + ": " + `raw`**
`usage0` =
| prompt_tokens | completion_tokens | total_tokens |
|---------------|-------------------|--------------|
| 0 | 0 | 0 |
**empty_tc = > table.empty_list**
`pack` =
| finish | text | error | usage | tool_calls |
|--------|------|-------|-------|------------|
| error | "" | `msg` | `usage0` | `empty_tc` |
*pack*