feat(llm): LLMResult.tool_calls from OpenAI-shaped responses

Parse choices[0].message.tool_calls into the ask Result; empty list when
absent. Offline gold covers tool_calls_from.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-24 20:07:59 +08:00
1 parent 46d78c6e15
commit 2d412e3bba
7 files changed
+83 -16

No files matched your search

+9 -4
View File
@@ -265,7 +265,7 @@ Prefer declaring `model` / `api_key` / `base_url` in frontmatter Binding, then u
+ `prompt`=None
+ `path`=None
Semantic ask → **LLMResult** (`text`, `model`, `finish`, `backend`, `usage`, `name`). Prefer `[text](result)` for the answer string. Omit `prompt=` / `path=` to use the entry document (`sys.module_source` + `prompt_body`).
Semantic ask → **LLMResult** (`text`, `model`, `finish`, `backend`, `usage`, `name`, `tool_calls`). Prefer `[text](result)` for the answer string. Omit `prompt=` / `path=` to use the entry document (`sys.module_source` + `prompt_body`).
1. `path`
**p = > prompt_load path=`path`**
@@ -283,12 +283,17 @@ 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"**
**tc = > json.get value=`pack` key="tool_calls"**
1. not `tc`
**tc = > json.parse text=[]**
2. *
**_ = 1**
`result` =
| text | model | finish | backend | usage | name | llm_calls | tokens |
|------|-------|--------|---------|-------|------|-----------|--------|
| `text` | [model](self) | `finish` | [backend](self) | `usage` | [name](self) | 1 | [total_tokens](usage) |
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|------|-------|--------|---------|-------|------|-----------|--------|------------|
| `text` | [model](self) | `finish` | [backend](self) | `usage` | [name](self) | 1 | [total_tokens](usage) | `tc` |
*result*
+32 -2
View File
@@ -44,6 +44,29 @@ Extract `{prompt_tokens,completion_tokens,total_tokens}` from an OpenAI-shaped r
*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"**
1. not `choices`
**empty = > json.parse text=[]**
*empty*
2. *
**c0 = [1](choices)**
**msg = > json.get value=`c0` key="message"**
1. not `msg`
**empty = > json.parse text=[]**
*empty*
2. *
**tc = > json.get value=`msg` key="tool_calls"**
1. `tc`
*tc*
2. *
**empty = > json.parse text=[]**
*empty*
## chat_completions
+ `base_url`
+ `api_key`
@@ -54,7 +77,7 @@ Extract `{prompt_tokens,completion_tokens,total_tokens}` from an OpenAI-shaped r
+ `suffix`=/chat/completions
+ `bearer`="Bearer "
POST `/chat/completions` (or stream SSE). Non-stream → `{text,usage,finish}`; stream → `{events,finish}`.
POST `/chat/completions` (or stream SSE). Non-stream → `{text,usage,finish,tool_calls}`; stream → `{events,finish}`.
**url = base_url + suffix**
**auth = bearer + api_key**
@@ -89,17 +112,24 @@ POST `/chat/completions` (or stream SSE). Non-stream → `{text,usage,finish}`;
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
1. [status](resp) == 200
**data = > json.parse text=[body](resp)**
**text = [content]([message]([1]([choices](data))))**
**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
+8 -3
View File
@@ -234,12 +234,17 @@ import openai:ext/ai/llm/openai.mq.md
**text = > json.get value=`pack` key="text"**
**usage = > json.get value=`pack` key="usage"**
**finish = > json.get value=`pack` key="finish"**
**tc = > json.get value=`pack` key="tool_calls"**
1. not `tc`
**tc = > json.parse text=[]**
2. *
**_ = 1**
`result` =
| text | model | finish | backend | usage | name | llm_calls | tokens |
|------|-------|--------|---------|-------|------|-----------|--------|
| `text` | [model](自) | `finish` | [backend](自) | `usage` | [name](自) | 1 | [total_tokens](usage) |
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|------|-------|--------|---------|-------|------|-----------|--------|------------|
| `text` | [model](自) | `finish` | [backend](自) | `usage` | [name](自) | 1 | [total_tokens](usage) | `tc` |
*result*