Repository navigation
feat(ai): carry optional fields on a first-line options comment in freeform tools - #65602
rekram1-node wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
I'd approve this. I checked it with the new and changed tests (22 in packages/ai, 42 in packages/core tool-execute.test.ts, all passing) and tsc --noEmit on packages/ai and packages/core (clean). I also called the parsing and streaming functions directly. A valid options line becomes the extra fields. A non-object line, or one with no newline after it, stays in the code. CRLF and options lines split across chunks are handled, and replaying a call gives back the same text. I couldn't call a real OpenAI endpoint.
Public API: @opencode/schema Tool.Freeform text gains an optional optionsPrefix. @opencode/ai ToolDefinition.freeformField gains an optional options argument, and FreeformOption accepts { type: "text", optionsPrefix } for inputs with optional fields. All of these are additions, so existing tools and plugins are unaffected.
One non-blocking gap, noted inline: the capability check looks only at text, but a tool with an options line sends a Lark grammar.
| if (tool.freeform === undefined || tool.native !== undefined || field === undefined) return undefined | ||
| return { field, format: tool.freeform } | ||
| if (tool.freeform === undefined || tool.native !== undefined) return undefined | ||
| const prefix = tool.freeform.type === "text" ? tool.freeform.optionsPrefix : undefined |
There was a problem hiding this comment.
With optionsPrefix, the request sends a lark grammar, but the tool still declares type: "text". Core's capability check (packages/core/src/session/model-request.ts, model.capabilities.freeform?.includes(t.freeform.type)) therefore lets it through for a model that lists only text. The models.dev snapshot always lists all three formats today, so nothing breaks yet. A config or provider with freeform: ["text"] would still get a grammar it may reject, though. It would be safer to require lark support before using the options line, and otherwise send JSON or plain text.
There was a problem hiding this comment.
Good catch. Fixed in the latest commit: core now keeps optionsPrefix only when the model lists lark. A model that lists only text gets plain { type: "text" }, so single-field execute still goes out as raw text, and full-Code-Mode execute (which has optional fields) falls back to JSON. Covered in session-model-request-hooks.test.ts.
There was a problem hiding this comment.
I checked f002ca7 and the fix is correct.
supportedFreeform(packages/core/src/session/model-request.ts:102-107) behaves as you described:- With
textandlarklisted,optionsPrefixstays. - With only
textlisted, the tool gets plain{ type: "text" }. - With only
larklisted, the tool gets no freeform at all.
- With
- In
packages/ai,resolvehandles{ type: "text" }correctly:- With optional fields and no prefix,
freeformField(schema, false)returnsundefined, so full-Code-Modeexecutegoes out as JSON. - Single-field
executestill resolves to a raw-text custom tool.
- With optional fields and no prefix,
- Both cases are covered by
openai-freeform.test.ts:72-81. The core path below that runsnew ToolDefinitionwithout themakecheck intool.ts, so removing the prefix can't make it throw.
Tests run: session-model-request-hooks.test.ts and tool-execute.test.ts in packages/core, 47 passing.
Not covered: no core test sends the real execute schema to a model that lists only text. The new test uses inputSchema: {}, so the JSON fallback is only checked by combining the core test with the ai unit test. This doesn't block merging.
I didn't change anything. bun.lock was already modified in the checkout before I started, and I left it in place.
There was a problem hiding this comment.
The new commit addresses my earlier point: the options line is now used only for models that list lark. Without it, the tool falls back to plain text, or to a JSON function when it has optional fields. I checked it with session-model-request-hooks.test.ts and tool-execute.test.ts in packages/core (48 pass) and a clean tsc --noEmit on packages/core.
Public API: unchanged from my first review. The new commit only adds internal core logic. I have no new concerns.
There was a problem hiding this comment.
The new commit drops undeclared keys from the options line, so options meant for another harness (e.g. yield_time_ms) no longer fail validation. I checked it with openai-freeform.test.ts and provider/freeform-tools.test.ts in packages/ai (22 pass) and a clean tsc --noEmit on packages/ai. The earlier point about the Lark capability was fixed in the previous commit.
Public API: unchanged from my first review. The new options list is internal to the OpenAI adapter. I have no new concerns.
4db4eb7 to
fba4160
Compare
There was a problem hiding this comment.
The new commit only rewords three comments, and the branch was rebased onto the latest dev. The code is otherwise unchanged since my last review. After the rebase, the freeform tests in packages/ai (22) and the model-request and execute tests in packages/core (48) still pass, and tsc --noEmit is clean on both packages. The Lark capability point was fixed in an earlier commit.
Public API: unchanged from my first review. I have no new concerns.
|
Closing in favor of #65709, which handles the options line inside the execute tool in core instead of in the OpenAI adapter. |
Issue for this PR
Closes #
Type of change
What does this PR do?
Since #65266, full Code Mode
executehas optionaltimeoutandbackgroundfields, sopackages/aistopped sending it as an OpenAI custom tool and GPT got a JSON function instead. In our Terminal-Bench audits, JSON-escaped code makes gpt-6.2-sol fall into endless whitespace after an escaped\"in about 23% of trials, and cut programs short at unescaped quotes. Raw-textexecutehad none of these.A
textfreeform tool can declareoptionsPrefix. That allows optional fields next to its one required string. On OpenAI Responses and Chat, such a tool goes out as a custom tool with a Lark grammar: an optional first line<prefix> <JSON object>, then the source. One generated line is added to the description, naming the syntax and the fields.{ ...options, code: rest }. Otherwise the whole text is the code; a bad line is just a JS comment. Core validates the result as usual.full_codemode,executestill goes out as plaintext.executedeclaresoptionsPrefix: "// @exec:". The capability check keeps the options line only for models that listlark; otherwise the tool falls back to plain text (single field) or JSON (with optional fields).How did you verify your code works?
bun testinpackages/aipasses. Added tests for parsing with and without options, malformed lines, an options line followed by code, CRLF, streaming on Responses and Chat, identical history replay, and unchanged behavior for Anthropic.packages/core,tool-execute.test.tspasses. It checks that full-Code-Modeexecutegoes out as a custom tool with a grammar for GPT and as JSON for Claude.bun typecheckandbun run lintpass.gpt-6.2-sol: the grammar is accepted, the model wrote// @exec: {"background":true}and it parsed to{ background: true, code }, and the replayed next turn was accepted.Screenshots / recordings
N/A
Checklist
Added by the opencode bot (AI-generated).
Public API: adds optional
optionsPrefixto thetextvariant ofTool.Freeformand an optionaloptionsargument toToolDefinition.freeformField(backwards compatible)