fix(ai): soft LLM transport errors and strip ext/ai json glue

Host/plugin HTTP failures return structured finish|status=error instead of
aborting the VM; agent/llm/openai/智能体 result maps use GFM tables and
table.put. SKILL gains positive copy-paste examples so agents stop mirroring
legacy json.set chains.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-10-16 14:27:45 +08:00
1 parent c44ea38ae2
commit 1908bf1a53
12 files changed
+813 -327

No files matched your search

+395 -270
View File
File diff suppressed because it is too large. Load diff
+29 -5
View File
@@ -5,6 +5,7 @@ description: >-
OpenAI-compatible and Ollama HTTP stay behind the semantic surface.
import sys:lib/sys.mq.md
import json:lib/json.mq.md
import table:lib/table.mq.md
import fs:lib/fs.mq.md
import text:lib/text.mq.md
import openai:ext/ai/llm/openai.mq.md
@@ -283,9 +284,21 @@ Semantic ask → **LLMResult** (`text`, `model`, `finish`, `backend`, `usage`, `
**text = > json.get value=`pack` key="text"**
**usage = > json.get value=`pack` key="usage"**
**finish = > json.get value=`pack` key="finish"**
**err = > json.get value=`pack` key="error"**
**tc = > json.get value=`pack` key="tool_calls"**
1. not `tc`
**tc = > json.parse text=[]**
**tc = > table.empty_list**
2. *
**_ = 1**
1. not `text`
**text = ""**
2. *
**_ = 1**
1. not `finish`
1. `err`
**finish = "error"**
2. *
**finish = "stop"**
2. *
**_ = 1**
@@ -296,13 +309,24 @@ inside the table is unsafe.
**mdl = [model](self)**
**b = [backend](self)**
**nm = [name](self)**
**tt = [total_tokens](usage)**
1. `usage`
**tt = [total_tokens](usage)**
2. *
**tt = 0**
1. not `tt`
**tt = 0**
2. *
**_ = 1**
1. `err`
**calls = 0**
2. *
**calls = 1**
`result` =
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|------|-------|--------|---------|-------|------|-----------|--------|------------|
| `text` | `mdl` | `finish` | `b` | `usage` | `nm` | 1 | `tt` | `tc` |
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls | error |
|------|-------|--------|---------|-------|------|-----------|--------|------------|-------|
| `text` | `mdl` | `finish` | `b` | `usage` | `nm` | `calls` | `tt` | `tc` | `err` |
*result*
+57 -21
View File
@@ -4,6 +4,7 @@ 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.
Result packs are GFM tables / table.put (code-as-documentation).
import net:lib/net.mq.md
import json:lib/json.mq.md
import table:lib/table.mq.md
@@ -53,21 +54,18 @@ Extract OpenAI-shaped `choices[0].message.tool_calls` as a list (or empty list).
**choices = > json.get value=`data` key="choices"**
1. not `choices`
**empty = > json.parse text=[]**
*empty*
*> table.empty_list*
2. *
**c0 = [1](choices)**
**msg = > json.get value=`c0` key="message"**
1. not `msg`
**empty = > json.parse text=[]**
*empty*
*> table.empty_list*
2. *
**tc = > json.get value=`msg` key="tool_calls"**
1. `tc`
*tc*
2. *
**empty = > json.parse text=[]**
*empty*
*> table.empty_list*
## chat_completions
+ `base_url`
@@ -79,7 +77,7 @@ Extract OpenAI-shaped `choices[0].message.tool_calls` as a list (or empty list).
+ `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}`.
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).
**p = > plugin.native_path name="llm"**
1. `p`
@@ -107,13 +105,32 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
**body = > json.stringify value=`req`**
**resp = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo`**
1. [status](resp) == 200
**pack = > json.parse text={"finish":"stop"}**
**pack = > json.set map=`pack` key="events" value=[events](resp)**
**evs = [events](resp)**
`pack` =
| finish | events |
|--------|--------|
| stop | `evs` |
*pack*
2. *
> print text=ext/ai/llm/openai: HTTP error (stream)
> print text=[status](resp)
> sys.exit code=1
Soft transport / HTTP failure — structured pack, never abort the VM.
**st = [status](resp)**
**msg = "HTTP " + `st`**
**evs = [events](resp)**
1. `evs`
**e0 = [1](evs)**
**m2 = > json.get value=`e0` key="message"**
1. `m2`
**msg = `m2`**
`pack` =
| finish | text | error | events |
|--------|------|-------|--------|
| error | "" | `msg` | `evs` |
*pack*
2. *
**body = > json.stringify value=`req`**
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
@@ -132,14 +149,33 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
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` =
| finish | text | usage | tool_calls |
|--------|------|-------|------------|
| `fr` | `text` | `usage` | `tc` |
*pack*
2. *
> print text=ext/ai/llm/openai: HTTP error
> print text=[status](resp)
> print text=[body](resp)
> sys.exit code=1
Soft transport / HTTP failure — structured pack, never abort the VM.
**st = [status](resp)**
**raw = [body](resp)**
**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*
+28 -18
View File
@@ -88,13 +88,14 @@ import json:lib/json.mq.md
> 系统.退出 码=1
**id = > agent_alloc**
**h = > json.parse text={"_type":"智能体"}**
**h = > json.set map=`h` key="id" value=`id`**
**h = > json.set map=`h` key="model" value=`大模型`**
**h = > json.set map=`h` key="tools" value=`工具`**
`h` =
| _type | id | model | tools |
|-------|-----|-------|-------|
| 智能体 | `id` | `大模型` | `工具` |
1. `站立提示`
**h = > json.set map=`h` key="standing" value=`站立提示`**
**h = table.put in=`h` at="standing" value=`站立提示`**
2. *
**_ = 1**
@@ -138,8 +139,12 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
**model = > json.get value=`自` key="model"**
**tools = > json.get value=`自` key="tools"**
**user_turn = > json.parse text={"role":"user"}**
**user_turn = > json.set map=`user_turn` key="content" value=`任务`**
`user_turn` =
| role | content |
|------|---------|
| user | `任务` |
> agent_history_append id=`id` item=`user_turn`
1. `流式`
@@ -151,9 +156,11 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
**reply = > trim value=`reply`**
**decision = reply**
**tool_name = > 提取工具名 回复=`reply`**
**out = > json.parse text={"status":"ok"}**
**out = > json.set map=`out` key="task" value=`任务`**
**out = > json.set map=`out` key="decision" value=`decision`**
`out` =
| status | task | decision |
|--------|------|----------|
| ok | `任务` | `decision` |
1. `tool_name`
**tool_out = > 运行工具 工具=`tools` 名=`tool_name`**
@@ -161,8 +168,8 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
**deny = > split value=`tool_s` sep="Tool not allowed"**
**deny_n = > len value=`deny`**
1. `deny_n` > 1
**out = > json.set map=`out` key="status" value="error"**
**out = > json.set map=`out` key="error" value=`tool_s`**
**out = table.put in=`out` at="status" value="error"**
**out = table.put in=`out` at="error" value=`tool_s`**
**reply = tool_s**
2. *
**task_s = > json.stringify value=`任务`**
@@ -178,14 +185,17 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
**reply = > 大模型.流式结果 事件=`evs2`**
2. *
**reply = > model.运行 提示=`fp`**
**out = > json.set map=`out` key="tool" value=`tool_name`**
**out = > json.set map=`out` key="tool_result" value=`tool_s`**
**out = > json.set map=`out` key="result" value=`reply`**
**out = table.put in=`out` at="tool" value=`tool_name`**
**out = table.put in=`out` at="tool_result" value=`tool_s`**
**out = table.put in=`out` at="result" value=`reply`**
2. *
**out = > json.set map=`out` key="result" value=`reply`**
**out = table.put in=`out` at="result" value=`reply`**
**as_turn = > json.parse text={"role":"assistant"}**
**as_turn = > json.set map=`as_turn` key="content" value=`reply`**
`as_turn` =
| role | content |
|------|---------|
| assistant | `reply` |
> agent_history_append id=`id` item=`as_turn`
1. `沙盒`