OpenAI transport prefers llm_chat_completions when the native plugin is resolvable, and falls back to lib/net HTTP/SSE so CI stays green without the .so. Co-authored-by: Cursor <cursoragent@cursor.com>
4.0 KiB
title: ext/ai/llm/openai description: >- 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. import net:lib/net.mq.md import json:lib/json.mq.md import table:lib/table.mq.md import sys:lib/sys.mq.md import plugin:lib/plugin.mq.md
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
choicesempty = > json.parse text=[] empty -
c0 = 1
msg = > json.get value=c0 key="message"
- not
msgempty = > json.parse text=[] empty -
**tc = > json.get value=`msg` key="tool_calls"**
1. `tc`
*tc*
2. *
**empty = > json.parse text=[]**
*empty*
chat_completions
+ `base_url`
+ `api_key`
+ `model`
+ `prompt`
+ `stream`=False
+ `echo`=False
+ `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}.
p = > plugin.native_path name="llm"
p
plugin.load path=
p> llm_chat_completions base_url=base_urlapi_key=api_keymodel=modelprompt=promptstream=streamecho=echosuffix=suffixbearer=bearer
url = base_url + suffix
auth = bearer + api_key
headers = > table.put in=None at="Authorization" value=auth
messages =
| @ | role | content |
|---|---|---|
| 1 | user | prompt |
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=echo1. status == 200 pack = > json.parse text={"finish":"stop"} pack = > json.set map=packkey="events" value=events pack 2. *print text=ext/ai/llm/openai: HTTP error (stream) print text=status sys.exit code=1
-
**body = > json.stringify value=`req`**
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
1. [status](resp) == 200
**data = > json.parse text=[body](resp)**
**msg = [message]([1]([choices](data)))**
**text = > json.get value=`msg` key="content"**
1. not `text`
**text = ""**
2. *
**_ = 1**
**usage = > usage_from data=`data`**
**fr = > json.get value=[1]([choices](data)) key="finish_reason"**
1. not `fr`
**fr = "stop"**
2. *
**_ = 1**
**tc = > tool_calls_from data=`data`**
**pack = > json.parse text={"finish":"stop"}**
**pack = > json.set map=`pack` key="text" value=`text`**
**pack = > json.set map=`pack` key="usage" value=`usage`**
**pack = > json.set map=`pack` key="finish" value=`fr`**
**pack = > json.set map=`pack` key="tool_calls" value=`tc`**
*pack*
2. *
> print text=ext/ai/llm/openai: HTTP error
> print text=[status](resp)
> print text=[body](resp)
> sys.exit code=1