Skip to content

feat(ai): carry optional fields on a first-line options comment in freeform tools - #65602

Closed
rekram1-node wants to merge 4 commits into
devfrom
freeform-exec-options
Closed

rekram1-node wants to merge 4 commits into
devfrom
freeform-exec-options

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Since #65266, full Code Mode execute has optional timeout and background fields, so packages/ai stopped 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-text execute had none of these.

A text freeform tool can declare optionsPrefix. 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.

  • Parsing: if the first line matches the prefix and holds a JSON object, the input is { ...options, code: rest }. Otherwise the whole text is the code; a bad line is just a JS comment. Core validates the result as usual.
  • Replay: a call with options is written back as the options line plus the code, and parses back to the same input.
  • Streaming: text that could still be an options line is held until the first newline (or the end), then sent as JSON fields.
  • Other providers, non-freeform tools, and freeform tools without extra fields send the same requests as before. Without full_codemode, execute still goes out as plain text.
  • Core changes: execute declares optionsPrefix: "// @exec:". The capability check keeps the options line only for models that list lark; otherwise the tool falls back to plain text (single field) or JSON (with optional fields).

How did you verify your code works?

  • bun test in packages/ai passes. 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.
  • In packages/core, tool-execute.test.ts passes. It checks that full-Code-Mode execute goes out as a custom tool with a grammar for GPT and as JSON for Claude.
  • bun typecheck and bun run lint pass.
  • Live test through Zen with 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: adds optional optionsPrefix to the text variant of Tool.Freeform and an optional options argument to ToolDefinition.freeformField (backwards compatible)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked f002ca7 and the fix is correct.

  • supportedFreeform (packages/core/src/session/model-request.ts:102-107) behaves as you described:
    • With text and lark listed, optionsPrefix stays.
    • With only text listed, the tool gets plain { type: "text" }.
    • With only lark listed, the tool gets no freeform at all.
  • In packages/ai, resolve handles { type: "text" } correctly:
    • With optional fields and no prefix, freeformField(schema, false) returns undefined, so full-Code-Mode execute goes out as JSON.
    • Single-field execute still resolves to a raw-text custom tool.
  • Both cases are covered by openai-freeform.test.ts:72-81. The core path below that runs new ToolDefinition without the make check in tool.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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rekram1-node
rekram1-node enabled auto-merge (squash) November 23, 2026 04:31

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rekram1-node

Copy link
Copy Markdown
Member Author

Closing in favor of #65709, which handles the options line inside the execute tool in core instead of in the OpenAI adapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant