Commit Graph

18 Commits

Author SHA1 Message Date
Kit Langton 76140b3da6 refactor(llm): move tool execution onto client 2026-05-06 20:51:11 -04:00
Kit Langton 73326f5125 feat(llm): preserve provider diagnostics 2026-05-06 15:54:30 -04:00
Kit Langton 9c93840a38 fix(llm): keep local refactor coherent after merge 2026-05-06 13:51:38 -04:00
Kit Langton 8b8a09c59f Merge remote-tracking branch 'origin/llm-core-patch-api' into llm-core-patch-api
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
#
# Conflicts:
#	packages/llm/AGENTS.md
#	packages/llm/package.json
#	packages/llm/script/recording-cost-report.ts
#	packages/llm/script/setup-recording-env.ts
#	packages/llm/src/adapter.ts
#	packages/llm/src/index.ts
#	packages/llm/src/llm.ts
#	packages/llm/src/patch.ts
#	packages/llm/src/protocols/anthropic-messages.ts
#	packages/llm/src/protocols/openai-responses.ts
#	packages/llm/src/provider/bedrock-converse.ts
#	packages/llm/src/provider/openai-compatible-chat.ts
#	packages/llm/src/provider/openai-compatible-family.ts
#	packages/llm/src/provider/xai.ts
#	packages/llm/src/schema.ts
#	packages/llm/src/tool-runtime.ts
#	packages/llm/src/tool.ts
#	packages/llm/test/adapter.test.ts
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/groq-llama-3-3-70b-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/groq-streams-text.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/groq-streams-tool-call.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/openrouter-claude-opus-4-7-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-4o-mini-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-5-5-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-text.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-tool-call.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/xai-grok-4-3-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/xai-streams-text.json
#	packages/llm/test/fixtures/recordings/openai-compatible-chat/xai-streams-tool-call.json
#	packages/llm/test/fixtures/recordings/openai-responses/gpt-5-5-drives-a-tool-loop.json
#	packages/llm/test/fixtures/recordings/openai-responses/gpt-5-5-streams-text.json
#	packages/llm/test/fixtures/recordings/openai-responses/gpt-5-5-streams-tool-call.json
#	packages/llm/test/provider-resolver.test.ts
#	packages/llm/test/provider/anthropic-messages.recorded.test.ts
#	packages/llm/test/provider/bedrock-converse.test.ts
#	packages/llm/test/provider/gemini.recorded.test.ts
#	packages/llm/test/provider/gemini.test.ts
#	packages/llm/test/provider/openai-chat-tool-loop.recorded.test.ts
#	packages/llm/test/provider/openai-compatible-chat.recorded.test.ts
#	packages/llm/test/provider/openai-responses.recorded.test.ts
#	packages/llm/test/provider/openai-responses.test.ts
#	packages/llm/test/recorded-scenarios.ts
#	packages/llm/test/recorded-test.ts
#	packages/opencode/src/provider/llm-bridge.ts
#	packages/opencode/src/session/llm-native-tools.ts
#	packages/opencode/src/session/llm.ts
#	packages/opencode/test/provider/llm-bridge.test.ts
#	packages/opencode/test/session/llm-native-stream.test.ts
2026-05-06 13:48:44 -04:00
Kit Langton c99e278e28 refactor(llm): tighten helper and diagnostic surfaces 2026-05-06 13:28:49 -04:00
Kit Langton 9fc1d154c4 refactor(llm): tighten runtime service boundaries 2026-05-06 11:47:23 -04:00
Kit Langton 7b4f436fc2 refactor(llm): collapse client adapter injection 2026-05-06 11:08:31 -04:00
Kit Langton 1808fe8628 refactor(llm): clarify public module surface 2026-05-06 09:30:01 -04:00
Kit Langton 89c65949c2 refactor(llm): remove transform pipeline 2026-05-05 20:18:15 -04:00
Kit Langton 172c382f00 refactor(llm): clarify adapter payload flow 2026-05-05 17:50:28 -04:00
Kit Langton 4daac79cf3 refactor(llm): split providers and protocols 2026-05-05 16:58:20 -04:00
Kit Langton 9065d79a9a fix(llm): preserve native protocol state 2026-05-03 00:20:50 -04:00
Kit Langton f4de3e801e feat(llm): add LLMEvent.is.* camelCase narrowing helpers
Schema.toTaggedUnion('type') already provides LLMEvent.guards but uses
kebab-case bracket access (LLMEvent.guards['tool-call']). Adds an LLMEvent.is
namespace with camelCase aliases that delegate to the same guards, so
consumers can write events.filter(LLMEvent.is.toolCall) instead of
events.filter(LLMEvent.guards['tool-call']).

Migrated all callsites in src/llm.ts and the two test files for consistency.
LLMEvent.guards / .match / .cases / .isAnyOf remain available for callers
who want the Effect-canonical API.
2026-05-01 08:12:37 -04:00
Kit Langton 3cd13c87c4 refactor(llm): standardize native request APIs 2026-05-01 08:12:34 -04:00
Kit Langton ecd73f26fc refactor(llm): simplify adapter shared logic 2026-05-01 08:12:34 -04:00
Kit Langton e1c6bf92fb feat(llm): provider-executed tool pass-through
Add a `providerExecuted: boolean` flag to `tool-call` and `tool-result`
events plus the persisted `ToolResultPart`. When set, the tool runtime
skips client dispatch (the provider already executed the tool) and folds
both events into the assistant message so the next round's history
carries the call + result for context.

Anthropic: decode `server_tool_use` blocks and the three server tool
result block types (`web_search_tool_result`, `code_execution_tool_result`,
`web_fetch_tool_result`) into `tool-call` / `tool-result` events with
`providerExecuted: true`. Round-trip the same parts back into the
provider when the assistant message is replayed in subsequent requests.
Result block error payloads (`*_tool_result_error`) surface as
`result.type === "error"`.

OpenAI Responses: decode hosted tool items emitted via
`response.output_item.done` (`web_search_call`, `file_search_call`,
`code_interpreter_call`, `computer_use_call`, `image_generation_call`,
`mcp_call`, `local_shell_call`) as `tool-call` + `tool-result` pairs
with `providerExecuted: true`. Each tool's input fields are pulled out
explicitly; the full item is passed through as the result payload so
consumers can read outputs / sources / status without re-decoding.

Tool runtime: extend the dispatch decision so provider-executed
tool-calls bypass the handler lookup, and tool-result events with
`providerExecuted: true` are appended to the assistant content for
round-trip rather than being treated as a separate tool message.

Tests: 7 new deterministic fixtures cover Anthropic decode (success +
error result + round-trip + unknown server tool name), OpenAI Responses
decode (web_search_call, code_interpreter_call), and tool-runtime
skip-dispatch.

AGENTS.md updates the runtime section to describe pass-through behavior
and notes the transport-agnostic design that keeps a future WebSocket
adapter (e.g. OpenAI Codex backend) as a sibling rather than a core
rewrite.
2026-05-01 08:11:29 -04:00
Kit Langton ca198f739e refactor(llm): cache tool codecs and tighten ToolRuntime types
Simplify pass after the typed ToolRuntime initial drop. Findings from a
parallel review (code reuse + quality + perf):

src/tool.ts
- Tool now carries memoized decode/encode codecs and a precomputed
  ToolDefinition, derived once at tool() construction time. The runtime no
  longer rebuilds Schema closures or JSON Schema docs per call/per run.
- Constrains parameters/success to Schema.Codec<T, any, never, never> so
  the codecs have no service requirements. Drops the 'as unknown as' casts
  the runtime needed previously.
- Fixes a latent bug: schemas with $ref now correctly emit $defs on
  ToolDefinition.inputSchema (toJsonSchemaDocument's definitions were
  silently dropped before).

src/tool-runtime.ts
- Uses LLMRequest constructor instead of 'as LLMRequest' casts.
- Default tool dispatch concurrency is 10 (was 'unbounded'); exposed via
  RunOptions.concurrency. Unbounded is still available for handlers that
  do not share a saturable resource.
- Drops dead 'usage' state, the single-use Dispatched interface, and the
  DEFAULT_MAX_STEPS constant per the inline-when-used style rule.
- accumulate() now factors text-delta and reasoning-delta into one helper.

test/lib/openai-chunks.ts (new)
- Shared deltaChunk / usageChunk / toolCallChunk / finishChunk helpers.

test/lib/http.ts
- scriptedResponses moved here from tool-runtime.test.ts so future
  multi-step adapter tests can reuse it. Also picks up parallel work that
  swapped HandlerInput to a 'respond' callback for cleaner Response
  construction.

test/tool-runtime.test.ts
- Uses LLMEvent.guards for typed event filtering instead of cast-and-check.
- Concurrent test now uses sseEvents + deltaChunk instead of a hand-rolled
  body string.

Includes parallel callsite updates in test/adapter.test.ts and
test/provider/openai-compatible-chat.test.ts that adopt the 'respond' API
in lib/http.ts.
2026-05-01 08:11:29 -04:00
Kit Langton 3a2cb7f8ac feat(llm): add typed ToolRuntime
Schema-first, Effect-first tool loop:

- 'tool({ description, parameters, success, execute })' constructs a fully
  typed Tool. parameters and success are Effect Schemas; execute is typed
  against them and returns Effect<Success, ToolFailure>. Handler dependencies
  are closed over at construction time so the runtime never sees per-tool
  services.
- 'ToolRuntime.run(client, { request, tools, maxSteps?, stopWhen? })' streams
  the model, decodes tool-call inputs against parameters, dispatches to the
  matching handler, encodes results against success, emits tool-result events,
  appends assistant + tool messages, and re-streams. Stops on non-tool-calls
  finish, maxSteps, or stopWhen.
- Three recoverable error paths emit tool-error events so the model can
  self-correct: unknown tool name, input fails parameters Schema, handler
  returns ToolFailure. Defects fail the stream.
- 'ToolFailure' added to the schema and exported as the single forced error
  channel for handlers.
- Tool definitions on the LLMRequest are derived via toJsonSchemaDocument so
  consumers don't write JSON Schema by hand.

8 deterministic fixture tests cover the loop, errors, maxSteps, stopWhen, and
parallel tool calls in one step.
2026-05-01 08:11:29 -04:00