90e86a5e3d
* tweak: use theme tokens for debug bar surface * chore: update nix node_modules hashes * feat(tui): add heap snapshot functionality for TUI and server (#19028) * ci * change model for changelog * release: v1.3.2 * fix(opencode): skip typechecking generated models snapshot (#19018) * Revert "fix(app): more startup efficiency (#18985)" This reverts commit98b3340cee. * Revert "fix(app): startup efficiency (#18854)" This reverts commit546748a461. * effectify Worktree service (#18679) * fix: increase operations-per-run to 1000 and pin stale action to v10.2.0 The stale-issues workflow was hitting the default 30 operations limit, preventing it from processing all 2900+ issues/PRs. Increased to 1000 to handle the full backlog. Also pinned to exact v10.2.0 for reproducibility. * Add close-issues script and GitHub Action - Create script/github/close-issues.ts to close stale issues after 60 days - Add GitHub Action workflow to run daily at 2 AM - Remove old stale-issues workflow to avoid conflicts * Fix close-issues workflow permissions - Add contents: read permission for checkout - Use github.token instead of secrets.GITHUB_TOKEN * Process issues sequentially to avoid rate limits * Change issue close reason from not_planned to completed * fix(opencode): avoid snapshotting files over 2MB (#19043) * fix: provide merge context to beta conflict resolver (#19055) * tweak: only spawn lsp servers for files in current instance (or cwd if instance is global) (#19058) * fix: beta resolver typecheck + build smoke check (#19060) * fix: unblock beta conflict recovery (#19068) * electron: add createDirectory to open directory picker (#19071) * electron: remove file extension from electron-store wrapper (#19082) * app: pre-warm project globalSync state when navigate project via keybind (#19088) * fix(app): move message navigation off cmd+arrow (#18728) * Reapply "fix(app): startup efficiency (#18854)" This reverts commita379eb3867. * Reapply "fix(app): more startup efficiency (#18985)" This reverts commitcbe1337f24. * fix(app): hash inline script for csp * Revert "fix(app): startup efficiency" * Reapply "fix(app): startup efficiency" This reverts commit898456a25c. * fix(app): opencode web server url * chore(app): markdown playground in storyboard * chore(app): markdown playground in storyboard * feat(core): initial implementation of syncing (#17814) * chore: generate * chore: bump modelcontextprotocol/sdk to 1.27.1 (#19064) * chore: storybook tweaks * feat: restore git-backed review modes with effectful git service (#18900) * chore: generate * chore: update nix node_modules hashes * chore: cleanup * chore: remove dead code for todoread tool (#19128) * chore: storybook tweaks * fix(opencode): classify ZlibError from Bun fetch as retryable instead of unknown (#19104) Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> * fix(task): respect agent permission config for todowrite tool (#19125) * fix(app): agent normalization (#19169) * fix: Windows e2e stability (CrossSpawnSpawner, snapshot isolation, session race guards) (#19163) * fix+refactor(mcp): lifecycle tests, cancelPending fix, Effect migration (#19042) * effectify Bus service: migrate to Effect PubSub + InstanceState (#18579) * file: use Effect.cached for scan deduplication (#19164) * ignore: update disavowed list (#19184) * skill: use Effect.cached for load deduplication (#19165) * chore: generate * fix: bump gitlab-ai-provider to 5.3.3 for DWS tool approval support (#19185) * test: restore 5 workers on Windows e2e (#19188) * fix(opencode): image paste on Windows Terminal 1.25+ with kitty keyboard (#17674) * chore: update nix node_modules hashes * wip: zen * wip: zen * go: do not respect disabled zen models * fix: ensure enterprise url is set properly during auth flow (#19212) * revert: roll back git-backed review modes (#19295) * chore: generate * tui: bypass local SSE event streaming in worker (#19183) * feat: embed WebUI in binary with proxy flags (#19299) Co-authored-by: BlankParticle <blankparticle@gmail.com> * release: v1.3.3 * chore: generate * changelog ci tweaks * refactor(lsp): effectify LSP service with InstanceState (#19150) * chore: generate * feat: add gpt prompt so non codex gpt models have their own system prompt modeled after codex cli (#19220) * feat(core): remove workspace server, WorkspaceContext, start work towards better routing (#19316) * effectify Config service (#19139) * chore: generate * refactor(config): use cachedInvalidateWithTTL, bump effect to beta.37 (#19322) * fix(mcp): close transport on failed/timed-out connections (#19200) * fix(app): more startup perf (#19288) * chore: generate * chore: update nix node_modules hashes * fix(app): don't bundle fonts (#19329) * chore: generate * fix(app): default shell tool to collapsed * fix(app): remove fork session button * fix(ui): reduce markdown jank while responses stream (#19304) * fix: web ui bundle build on windows (#19337) * refactor(effect): yield services instead of promise facades (#19325) * chore: generate * refactor(vcs): replace async git() with ChildProcessSpawner (#19361) * fix(opencode): ignore generated models snapshot files (#19362) * fix(ui): keep partial markdown readable while responses stream (#19403) * chore: update nix node_modules hashes * fix(app): persist queued followups across project switches (#19421) * refactor(tool-registry): yield Config/Plugin services, use Effect.forEach (#19363) * chore: generate * tui plugins (#19347) * chore: generate * effectify Skill service internals (#19364) * chore: update nix node_modules hashes * effectify Plugin service internals (#19365) * refactor(core): split out instance and route through workspaces (#19335) * chore(app): more spacing controls * fix(ui): make streamed markdown feel more continuous (#19404) * fix(app): resize layout viewport when mobile keyboard appears (#15841) * fix(desktop-electron): match dev dock icon inset on macOS (#19429) * fix(app): default file tree to closed with minimum width (#19426) * fix flaky plugin tests (no mock.module for bun) (#19445) * tweak: add additional overflow error patterns (#19446) * no theme override in dev (#19456) * feat: AI SDK v6 support (#18433) * refactor(session): effectify Session service (#19449) * refactor(core): move more responsibility to workspace routing (#19455) * chore: update nix node_modules hashes * refactor(format): use ChildProcessSpawner instead of Process.spawn (#19457) * chore: generate * Single target plugin entrypoints (#19467) * refactor(session): effectify SessionCompaction service (#19459) * feat(ci): use Azure Artifact Signing for Windows releases (#15201) * fix(app): more startup efficiency (#19454) * update effect to 4.0.0-beta.42 (#19484) * chore: update nix node_modules hashes * tweak: adjust bash tool description to increase cache hit rates between projects (#19487) * refactor(session): move context into prompt footer (#19486) * refactor(prompt): remove variant cycle display from footer (#19489) * feat: add model variant selection dialog (#19488) * fix: restore subagent footer and fix style guide violations (#19491) * tweak(session): add top spacing and remove obsolete docs prompt * upgrade opentui to 0.1.91 (#19440) * refactor(file): use AppFileSystem instead of raw Filesystem (#19458) * chore: generate * chore: update nix node_modules hashes * kv theme before default fallback (#19523) * feat: open dialog for model variant selection instead of cycling (#19534) * refactor(session): effectify session processor (#19485) * feat: dialog variant menu and subagent improvements (#19537) * use theme color for prompt placeholder (#19535) * fix: update opencode-gitlab-auth to 2.0.1 (#19552) * chore: update nix node_modules hashes * prompt slot (#19563) * fix: respect semver build identifiers for nix (#11915) * fix: nix embedded web-ui support (#19561) * ignore: kill todo (#19566) * chore: update nix node_modules hashes * wip: zen * wip: zen * zen: ZDR policy * ci: cancel stale nix-hashes runs (#19571) * release: v1.3.4 * refactor: kilo compat for v1.3.4 * fix: migration types * refactor: upgrade kilo-gateway to ai sdk v6 * refactor: improve upstream merge script * fix: fix some tests * style(kilo-vscode): adjust indentation and formatting in parts-util and PopupSelector Normalize boolean expression indentation in isCompletionResult to use consistent 4-space alignment and reformat PopupSelectorProps generic interface declaration to split the Omit type across multiple lines. * docs(kilo-docs): update auto-generated source links Remove outdated URLs and add new bug report issue link pointing to anomalyco/opencode repository. Drop references to kilocode bug report template and config precedence order docs, reducing total unique URLs from 262 to 261. * fix(kilo-ui): remove unused NerdFonts story and MONO_NERD_FONTS import Drop the NerdFonts story from font.stories.tsx along with the unused MONO_NERD_FONTS import, as the exported constant is no longer available from the @opencode-ai/ui/font module. * chore: update visual regression baselines * fix(opencode): move Show conditional wrapper outside box in home onboarding Relocate the Show component to wrap the box element instead of being nested inside it, preventing the empty box from rendering when the onboarding tip is not visible. --------- Co-authored-by: Jay V <air@live.ca> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Brendan Allan <brendonovich@outlook.com> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: James Long <longster@gmail.com> Co-authored-by: André Cruz <acruz@cloudflare.com> Co-authored-by: Ariane Emory <97994360+ariane-emory@users.noreply.github.com> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: BlankParticle <blankparticle@gmail.com> Co-authored-by: Sebastian <hasta84@gmail.com> Co-authored-by: Burak Yigit Kaya <byk@sentry.io> Co-authored-by: Caleb Norton <n0603919@outlook.com> Co-authored-by: Imanol Maiztegui <imanol.mzd@gmail.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
593 lines
28 KiB
TypeScript
593 lines
28 KiB
TypeScript
import { OpenAICompatibleChatLanguageModel } from "@/provider/sdk/copilot/chat/openai-compatible-chat-language-model"
|
|
import { describe, test, expect, mock } from "bun:test"
|
|
import type { LanguageModelV3Prompt } from "@ai-sdk/provider"
|
|
|
|
async function convertReadableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]> {
|
|
const reader = stream.getReader()
|
|
const result: T[] = []
|
|
while (true) {
|
|
const { done, value } = await reader.read()
|
|
if (done) break
|
|
result.push(value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
const TEST_PROMPT: LanguageModelV3Prompt = [{ role: "user", content: [{ type: "text", text: "Hello" }] }]
|
|
|
|
// Fixtures from copilot_test.exs
|
|
const FIXTURES = {
|
|
basicText: [
|
|
`data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gemini-2.0-flash-001","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}`,
|
|
`data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gemini-2.0-flash-001","choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}`,
|
|
`data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gemini-2.0-flash-001","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":"stop"}]}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
reasoningWithToolCalls: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Understanding Dayzee's Purpose**\\n\\nI'm starting to get a better handle on \`dayzee\`.\\n\\n"}}],"created":1764940861,"id":"OdwyabKMI9yel7oPlbzgwQM","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Assessing Dayzee's Functionality**\\n\\nI've reviewed the files.\\n\\n"}}],"created":1764940862,"id":"OdwyabKMI9yel7oPlbzgwQM","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{\\"filePath\\":\\"/README.md\\"}","name":"read_file"},"id":"call_abc123","index":0,"type":"function"}],"reasoning_opaque":"4CUQ6696CwSXOdQ5rtvDimqA91tBzfmga4ieRbmZ5P67T2NLW3"}}],"created":1764940862,"id":"OdwyabKMI9yel7oPlbzgwQM","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{\\"filePath\\":\\"/mix.exs\\"}","name":"read_file"},"id":"call_def456","index":1,"type":"function"}]}}],"created":1764940862,"id":"OdwyabKMI9yel7oPlbzgwQM","usage":{"completion_tokens":53,"prompt_tokens":19581,"prompt_tokens_details":{"cached_tokens":17068},"total_tokens":19768,"reasoning_tokens":134},"model":"gemini-3-pro-preview"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
reasoningWithOpaqueAtEnd: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Analyzing the Inquiry's Nature**\\n\\nI'm currently parsing the user's question.\\n\\n"}}],"created":1765201729,"id":"Ptc2afqsCIHqlOoP653UiAI","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Reconciling User's Input**\\n\\nI'm grappling with the context.\\n\\n"}}],"created":1765201730,"id":"Ptc2afqsCIHqlOoP653UiAI","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":"I am Tidewave, a highly skilled AI coding agent.\\n\\n","role":"assistant"}}],"created":1765201730,"id":"Ptc2afqsCIHqlOoP653UiAI","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"finish_reason":"stop","index":0,"delta":{"content":"How can I help you?","role":"assistant","reasoning_opaque":"/PMlTqxqSJZnUBDHgnnJKLVI4eZQ"}}],"created":1765201730,"id":"Ptc2afqsCIHqlOoP653UiAI","usage":{"completion_tokens":59,"prompt_tokens":5778,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":5932,"reasoning_tokens":95},"model":"gemini-3-pro-preview"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
// Case where reasoning_opaque and content come in the SAME chunk
|
|
reasoningWithOpaqueAndContentSameChunk: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Understanding the Query's Nature**\\n\\nI'm currently grappling with the user's philosophical query.\\n\\n"}}],"created":1766062103,"id":"FPhDacixL9zrlOoPqLSuyQ4","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-2.5-pro"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Framing the Response's Core**\\n\\nNow, I'm structuring my response.\\n\\n"}}],"created":1766062103,"id":"FPhDacixL9zrlOoPqLSuyQ4","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-2.5-pro"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":"Of course. I'm thinking right now.","role":"assistant","reasoning_opaque":"ExXaGwW7jBo39OXRe9EPoFGN1rOtLJBx"}}],"created":1766062103,"id":"FPhDacixL9zrlOoPqLSuyQ4","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-2.5-pro"}`,
|
|
`data: {"choices":[{"finish_reason":"stop","index":0,"delta":{"content":" What's on your mind?","role":"assistant"}}],"created":1766062103,"id":"FPhDacixL9zrlOoPqLSuyQ4","usage":{"completion_tokens":78,"prompt_tokens":3767,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":3915,"reasoning_tokens":70},"model":"gemini-2.5-pro"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
// Case where reasoning_opaque and content come in same chunk, followed by tool calls
|
|
reasoningWithOpaqueContentAndToolCalls: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Analyzing the Structure**\\n\\nI'm currently trying to get a handle on the project's layout. My initial focus is on the file structure itself, specifically the directory organization. I'm hoping this will illuminate how different components interact. I'll need to identify the key modules and their dependencies.\\n\\n\\n"}}],"created":1766066995,"id":"MQtEafqbFYTZsbwPwuCVoAg","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-2.5-pro"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":"Okay, I need to check out the project's file structure.","role":"assistant","reasoning_opaque":"WHOd3dYFnxEBOsKUXjbX6c2rJa0fS214FHbsj+A3Q+i63SFo7H/92RsownAzyo0h2qEy3cOcrvAatsMx51eCKiMSqt4dYWZhd5YVSgF0CehkpDbWBP/SoRqLU1dhCmUJV/6b5uYFBOzKLBGNadyhI7T1gWFlXntwc6SNjH6DujnFPeVr+L8DdOoUJGJrw2aOfm9NtkXA6wZh9t7dt+831yIIImjD9MHczuXoXj8K7tyLpIJ9KlVXMhnO4IKSYNdKRtoHlGTmudAp5MgH/vLWb6oSsL+ZJl/OdF3WBOeanGhYNoByCRDSvR7anAR/9m5zf9yUax+u/nFg+gzmhFacnzZGtSmcvJ4/4HWKNtUkRASTKeN94DXB8j1ptB/i6ldaMAz2ZyU+sbjPWI8aI4fKJ2MuO01u3uE87xVwpWiM+0rahIzJsllI5edwOaOFtF4tnlCTQafbxHwCZR62uON2E+IjGzW80MzyfYrbLBJKS5zTeHCgPYQSNaKzPfpzkQvdwo3JUnJYcEHgGeKzkq5sbvS5qitCYI7Xue0V98S6/KnUSPnDQBjNnas2i6BqJV2vuCEU/Y3ucrlKVbuRIFCZXCyLzrsGeRLRKlrf5S/HDAQ04IOPQVQhBPvhX0nDjhZB"}}],"created":1766066995,"id":"MQtEafqbFYTZsbwPwuCVoAg","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-2.5-pro"}`,
|
|
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{}","name":"list_project_files"},"id":"call_MHxqRDd5WVo3NU8wUXRaMmc0MFE","index":0,"type":"function"}]}}],"created":1766066995,"id":"MQtEafqbFYTZsbwPwuCVoAg","usage":{"completion_tokens":19,"prompt_tokens":3767,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":3797,"reasoning_tokens":11},"model":"gemini-2.5-pro"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
// Case where reasoning goes directly to tool_calls with NO content
|
|
// reasoning_opaque and tool_calls come in the same chunk
|
|
reasoningDirectlyToToolCalls: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Executing and Analyzing HTML**\\n\\nI've successfully captured the HTML snapshot using the \`browser_eval\` tool, giving me a solid understanding of the page structure. Now, I'm shifting focus to Elixir code execution with \`project_eval\` to assess my ability to work within the project's environment.\\n\\n\\n"}}],"created":1766068643,"id":"oBFEaafzD9DVlOoPkY3l4Qs","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"**Testing Project Contexts**\\n\\nI've got the HTML body snapshot from \`browser_eval\`, which is a helpful reference. Next, I'm testing my ability to run Elixir code in the project with \`project_eval\`. I'm starting with a simple sum: \`1 + 1\`. This will confirm I'm set up to interact with the project's codebase.\\n\\n\\n"}}],"created":1766068644,"id":"oBFEaafzD9DVlOoPkY3l4Qs","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-pro-preview"}`,
|
|
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{\\"code\\":\\"1 + 1\\"}","name":"project_eval"},"id":"call_MHw3RDhmT1J5Z3B6WlhpVjlveTc","index":0,"type":"function"}],"reasoning_opaque":"ytGNWFf2doK38peANDvm7whkLPKrd+Fv6/k34zEPBF6Qwitj4bTZT0FBXleydLb6"}}],"created":1766068644,"id":"oBFEaafzD9DVlOoPkY3l4Qs","usage":{"completion_tokens":12,"prompt_tokens":8677,"prompt_tokens_details":{"cached_tokens":3692},"total_tokens":8768,"reasoning_tokens":79},"model":"gemini-3-pro-preview"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
|
|
reasoningOpaqueWithToolCallsNoReasoningText: [
|
|
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{}","name":"read_file"},"id":"call_reasoning_only","index":0,"type":"function"}],"reasoning_opaque":"opaque-xyz"}}],"created":1769917420,"id":"opaque-only","usage":{"completion_tokens":0,"prompt_tokens":0,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":0,"reasoning_tokens":0},"model":"gemini-3-flash-preview"}`,
|
|
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{}","name":"read_file"},"id":"call_reasoning_only_2","index":1,"type":"function"}]}}],"created":1769917420,"id":"opaque-only","usage":{"completion_tokens":12,"prompt_tokens":123,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":135,"reasoning_tokens":0},"model":"gemini-3-flash-preview"}`,
|
|
`data: [DONE]`,
|
|
],
|
|
}
|
|
|
|
function createMockFetch(chunks: string[]) {
|
|
return mock(async () => {
|
|
const body = new ReadableStream({
|
|
start(controller) {
|
|
for (const chunk of chunks) {
|
|
controller.enqueue(new TextEncoder().encode(chunk + "\n\n"))
|
|
}
|
|
controller.close()
|
|
},
|
|
})
|
|
|
|
return new Response(body, {
|
|
status: 200,
|
|
headers: { "Content-Type": "text/event-stream" },
|
|
})
|
|
})
|
|
}
|
|
|
|
function createModel(fetchFn: ReturnType<typeof mock>) {
|
|
return new OpenAICompatibleChatLanguageModel("test-model", {
|
|
provider: "copilot.chat",
|
|
url: () => "https://api.test.com/chat/completions",
|
|
headers: () => ({ Authorization: "Bearer test-token" }),
|
|
fetch: fetchFn as any,
|
|
})
|
|
}
|
|
|
|
describe("doStream", () => {
|
|
test("should stream text deltas", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.basicText)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// Filter to just the key events
|
|
const textParts = parts.filter(
|
|
(p) => p.type === "text-start" || p.type === "text-delta" || p.type === "text-end" || p.type === "finish",
|
|
)
|
|
|
|
expect(textParts).toMatchObject([
|
|
{ type: "text-start", id: "txt-0" },
|
|
{ type: "text-delta", id: "txt-0", delta: "Hello" },
|
|
{ type: "text-delta", id: "txt-0", delta: " world" },
|
|
{ type: "text-delta", id: "txt-0", delta: "!" },
|
|
{ type: "text-end", id: "txt-0" },
|
|
{ type: "finish", finishReason: { unified: "stop" } },
|
|
])
|
|
})
|
|
|
|
test("should stream reasoning with tool calls and capture reasoning_opaque", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningWithToolCalls)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// Check reasoning parts
|
|
const reasoningParts = parts.filter(
|
|
(p) => p.type === "reasoning-start" || p.type === "reasoning-delta" || p.type === "reasoning-end",
|
|
)
|
|
|
|
expect(reasoningParts[0]).toEqual({
|
|
type: "reasoning-start",
|
|
id: "reasoning-0",
|
|
})
|
|
|
|
expect(reasoningParts[1]).toMatchObject({
|
|
type: "reasoning-delta",
|
|
id: "reasoning-0",
|
|
})
|
|
expect((reasoningParts[1] as { delta: string }).delta).toContain("**Understanding Dayzee's Purpose**")
|
|
|
|
expect(reasoningParts[2]).toMatchObject({
|
|
type: "reasoning-delta",
|
|
id: "reasoning-0",
|
|
})
|
|
expect((reasoningParts[2] as { delta: string }).delta).toContain("**Assessing Dayzee's Functionality**")
|
|
|
|
// reasoning_opaque should be in reasoning-end providerMetadata
|
|
const reasoningEnd = reasoningParts.find((p) => p.type === "reasoning-end")
|
|
expect(reasoningEnd).toMatchObject({
|
|
type: "reasoning-end",
|
|
id: "reasoning-0",
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: "4CUQ6696CwSXOdQ5rtvDimqA91tBzfmga4ieRbmZ5P67T2NLW3",
|
|
},
|
|
},
|
|
})
|
|
|
|
// Check tool calls
|
|
const toolParts = parts.filter(
|
|
(p) => p.type === "tool-input-start" || p.type === "tool-call" || p.type === "tool-input-end",
|
|
)
|
|
|
|
expect(toolParts).toContainEqual({
|
|
type: "tool-input-start",
|
|
id: "call_abc123",
|
|
toolName: "read_file",
|
|
})
|
|
|
|
expect(toolParts).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "tool-call",
|
|
toolCallId: "call_abc123",
|
|
toolName: "read_file",
|
|
}),
|
|
)
|
|
|
|
expect(toolParts).toContainEqual({
|
|
type: "tool-input-start",
|
|
id: "call_def456",
|
|
toolName: "read_file",
|
|
})
|
|
|
|
// Check finish
|
|
const finish = parts.find((p) => p.type === "finish")
|
|
expect(finish).toMatchObject({
|
|
type: "finish",
|
|
finishReason: { unified: "tool-calls" },
|
|
usage: {
|
|
inputTokens: { total: 19581 },
|
|
outputTokens: { total: 53 },
|
|
},
|
|
})
|
|
})
|
|
|
|
test("should handle reasoning_opaque that comes at end with text in between", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningWithOpaqueAtEnd)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// Check that reasoning comes first
|
|
const reasoningStart = parts.findIndex((p) => p.type === "reasoning-start")
|
|
const textStart = parts.findIndex((p) => p.type === "text-start")
|
|
expect(reasoningStart).toBeLessThan(textStart)
|
|
|
|
// Check reasoning deltas
|
|
const reasoningDeltas = parts.filter((p) => p.type === "reasoning-delta")
|
|
expect(reasoningDeltas).toHaveLength(2)
|
|
expect((reasoningDeltas[0] as { delta: string }).delta).toContain("**Analyzing the Inquiry's Nature**")
|
|
expect((reasoningDeltas[1] as { delta: string }).delta).toContain("**Reconciling User's Input**")
|
|
|
|
// Check text deltas
|
|
const textDeltas = parts.filter((p) => p.type === "text-delta")
|
|
expect(textDeltas).toHaveLength(2)
|
|
expect((textDeltas[0] as { delta: string }).delta).toContain("I am Tidewave")
|
|
expect((textDeltas[1] as { delta: string }).delta).toContain("How can I help you?")
|
|
|
|
// reasoning-end should be emitted before text-start
|
|
const reasoningEndIndex = parts.findIndex((p) => p.type === "reasoning-end")
|
|
const textStartIndex = parts.findIndex((p) => p.type === "text-start")
|
|
expect(reasoningEndIndex).toBeGreaterThan(-1)
|
|
expect(reasoningEndIndex).toBeLessThan(textStartIndex)
|
|
|
|
// In this fixture, reasoning_opaque comes AFTER content has started (in chunk 4)
|
|
// So it arrives too late to be attached to reasoning-end. But it should still
|
|
// be captured and included in the finish event's providerMetadata.
|
|
const reasoningEnd = parts.find((p) => p.type === "reasoning-end")
|
|
expect(reasoningEnd).toMatchObject({
|
|
type: "reasoning-end",
|
|
id: "reasoning-0",
|
|
})
|
|
|
|
// reasoning_opaque should be in the finish event's providerMetadata
|
|
const finish = parts.find((p) => p.type === "finish")
|
|
expect(finish).toMatchObject({
|
|
type: "finish",
|
|
finishReason: { unified: "stop" },
|
|
usage: {
|
|
inputTokens: { total: 5778 },
|
|
outputTokens: { total: 59 },
|
|
},
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: "/PMlTqxqSJZnUBDHgnnJKLVI4eZQ",
|
|
},
|
|
},
|
|
})
|
|
})
|
|
|
|
test("should handle reasoning_opaque and content in the same chunk", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningWithOpaqueAndContentSameChunk)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// The critical test: reasoning-end should come BEFORE text-start
|
|
const reasoningEndIndex = parts.findIndex((p) => p.type === "reasoning-end")
|
|
const textStartIndex = parts.findIndex((p) => p.type === "text-start")
|
|
expect(reasoningEndIndex).toBeGreaterThan(-1)
|
|
expect(textStartIndex).toBeGreaterThan(-1)
|
|
expect(reasoningEndIndex).toBeLessThan(textStartIndex)
|
|
|
|
// Check reasoning deltas
|
|
const reasoningDeltas = parts.filter((p) => p.type === "reasoning-delta")
|
|
expect(reasoningDeltas).toHaveLength(2)
|
|
expect((reasoningDeltas[0] as { delta: string }).delta).toContain("**Understanding the Query's Nature**")
|
|
expect((reasoningDeltas[1] as { delta: string }).delta).toContain("**Framing the Response's Core**")
|
|
|
|
// reasoning_opaque should be in reasoning-end even though it came with content
|
|
const reasoningEnd = parts.find((p) => p.type === "reasoning-end")
|
|
expect(reasoningEnd).toMatchObject({
|
|
type: "reasoning-end",
|
|
id: "reasoning-0",
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: "ExXaGwW7jBo39OXRe9EPoFGN1rOtLJBx",
|
|
},
|
|
},
|
|
})
|
|
|
|
// Check text deltas
|
|
const textDeltas = parts.filter((p) => p.type === "text-delta")
|
|
expect(textDeltas).toHaveLength(2)
|
|
expect((textDeltas[0] as { delta: string }).delta).toContain("Of course. I'm thinking right now.")
|
|
expect((textDeltas[1] as { delta: string }).delta).toContain("What's on your mind?")
|
|
|
|
// Check finish
|
|
const finish = parts.find((p) => p.type === "finish")
|
|
expect(finish).toMatchObject({
|
|
type: "finish",
|
|
finishReason: { unified: "stop" },
|
|
})
|
|
})
|
|
|
|
test("should handle reasoning_opaque and content followed by tool calls", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningWithOpaqueContentAndToolCalls)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// Check that reasoning comes first, then text, then tool calls
|
|
const reasoningEndIndex = parts.findIndex((p) => p.type === "reasoning-end")
|
|
const textStartIndex = parts.findIndex((p) => p.type === "text-start")
|
|
const toolStartIndex = parts.findIndex((p) => p.type === "tool-input-start")
|
|
|
|
expect(reasoningEndIndex).toBeGreaterThan(-1)
|
|
expect(textStartIndex).toBeGreaterThan(-1)
|
|
expect(toolStartIndex).toBeGreaterThan(-1)
|
|
expect(reasoningEndIndex).toBeLessThan(textStartIndex)
|
|
expect(textStartIndex).toBeLessThan(toolStartIndex)
|
|
|
|
// Check reasoning content
|
|
const reasoningDeltas = parts.filter((p) => p.type === "reasoning-delta")
|
|
expect(reasoningDeltas).toHaveLength(1)
|
|
expect((reasoningDeltas[0] as { delta: string }).delta).toContain("**Analyzing the Structure**")
|
|
|
|
// reasoning_opaque should be in reasoning-end (comes with content in same chunk)
|
|
const reasoningEnd = parts.find((p) => p.type === "reasoning-end")
|
|
expect(reasoningEnd).toMatchObject({
|
|
type: "reasoning-end",
|
|
id: "reasoning-0",
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: expect.stringContaining("WHOd3dYFnxEBOsKUXjbX6c2rJa0fS214"),
|
|
},
|
|
},
|
|
})
|
|
|
|
// Check text content
|
|
const textDeltas = parts.filter((p) => p.type === "text-delta")
|
|
expect(textDeltas).toHaveLength(1)
|
|
expect((textDeltas[0] as { delta: string }).delta).toContain(
|
|
"Okay, I need to check out the project's file structure.",
|
|
)
|
|
|
|
// Check tool call
|
|
const toolParts = parts.filter(
|
|
(p) => p.type === "tool-input-start" || p.type === "tool-call" || p.type === "tool-input-end",
|
|
)
|
|
|
|
expect(toolParts).toContainEqual({
|
|
type: "tool-input-start",
|
|
id: "call_MHxqRDd5WVo3NU8wUXRaMmc0MFE",
|
|
toolName: "list_project_files",
|
|
})
|
|
|
|
expect(toolParts).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "tool-call",
|
|
toolCallId: "call_MHxqRDd5WVo3NU8wUXRaMmc0MFE",
|
|
toolName: "list_project_files",
|
|
}),
|
|
)
|
|
|
|
// Check finish
|
|
const finish = parts.find((p) => p.type === "finish")
|
|
expect(finish).toMatchObject({
|
|
type: "finish",
|
|
finishReason: { unified: "tool-calls" },
|
|
usage: {
|
|
inputTokens: { total: 3767 },
|
|
outputTokens: { total: 19 },
|
|
},
|
|
})
|
|
})
|
|
|
|
test("should emit reasoning-end before tool-input-start when reasoning goes directly to tool calls", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningDirectlyToToolCalls)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
// Critical check: reasoning-end MUST come before tool-input-start
|
|
const reasoningEndIndex = parts.findIndex((p) => p.type === "reasoning-end")
|
|
const toolStartIndex = parts.findIndex((p) => p.type === "tool-input-start")
|
|
|
|
expect(reasoningEndIndex).toBeGreaterThan(-1)
|
|
expect(toolStartIndex).toBeGreaterThan(-1)
|
|
expect(reasoningEndIndex).toBeLessThan(toolStartIndex)
|
|
|
|
// Check reasoning parts
|
|
const reasoningDeltas = parts.filter((p) => p.type === "reasoning-delta")
|
|
expect(reasoningDeltas).toHaveLength(2)
|
|
expect((reasoningDeltas[0] as { delta: string }).delta).toContain("**Executing and Analyzing HTML**")
|
|
expect((reasoningDeltas[1] as { delta: string }).delta).toContain("**Testing Project Contexts**")
|
|
|
|
// reasoning_opaque should be in reasoning-end providerMetadata
|
|
const reasoningEnd = parts.find((p) => p.type === "reasoning-end")
|
|
expect(reasoningEnd).toMatchObject({
|
|
type: "reasoning-end",
|
|
id: "reasoning-0",
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: "ytGNWFf2doK38peANDvm7whkLPKrd+Fv6/k34zEPBF6Qwitj4bTZT0FBXleydLb6",
|
|
},
|
|
},
|
|
})
|
|
|
|
// No text parts should exist
|
|
const textParts = parts.filter((p) => p.type === "text-start" || p.type === "text-delta" || p.type === "text-end")
|
|
expect(textParts).toHaveLength(0)
|
|
|
|
// Check tool call
|
|
const toolCall = parts.find((p) => p.type === "tool-call")
|
|
expect(toolCall).toMatchObject({
|
|
type: "tool-call",
|
|
toolCallId: "call_MHw3RDhmT1J5Z3B6WlhpVjlveTc",
|
|
toolName: "project_eval",
|
|
})
|
|
|
|
// Check finish
|
|
const finish = parts.find((p) => p.type === "finish")
|
|
expect(finish).toMatchObject({
|
|
type: "finish",
|
|
finishReason: { unified: "tool-calls" },
|
|
})
|
|
})
|
|
|
|
test("should attach reasoning_opaque to tool calls without reasoning_text", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.reasoningOpaqueWithToolCallsNoReasoningText)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
const reasoningParts = parts.filter(
|
|
(p) => p.type === "reasoning-start" || p.type === "reasoning-delta" || p.type === "reasoning-end",
|
|
)
|
|
|
|
expect(reasoningParts).toHaveLength(0)
|
|
|
|
const toolCall = parts.find((p) => p.type === "tool-call" && p.toolCallId === "call_reasoning_only")
|
|
expect(toolCall).toMatchObject({
|
|
type: "tool-call",
|
|
toolCallId: "call_reasoning_only",
|
|
toolName: "read_file",
|
|
providerMetadata: {
|
|
copilot: {
|
|
reasoningOpaque: "opaque-xyz",
|
|
},
|
|
},
|
|
})
|
|
})
|
|
|
|
test("should include response metadata from first chunk", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.basicText)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
const metadata = parts.find((p) => p.type === "response-metadata")
|
|
expect(metadata).toMatchObject({
|
|
type: "response-metadata",
|
|
id: "chatcmpl-123",
|
|
modelId: "gemini-2.0-flash-001",
|
|
})
|
|
})
|
|
|
|
test("should emit stream-start with warnings", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.basicText)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
const streamStart = parts.find((p) => p.type === "stream-start")
|
|
expect(streamStart).toEqual({
|
|
type: "stream-start",
|
|
warnings: [],
|
|
})
|
|
})
|
|
|
|
test("should include raw chunks when requested", async () => {
|
|
const mockFetch = createMockFetch(FIXTURES.basicText)
|
|
const model = createModel(mockFetch)
|
|
|
|
const { stream } = await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
includeRawChunks: true,
|
|
})
|
|
|
|
const parts = await convertReadableStreamToArray(stream)
|
|
|
|
const rawChunks = parts.filter((p) => p.type === "raw")
|
|
expect(rawChunks.length).toBeGreaterThan(0)
|
|
})
|
|
})
|
|
|
|
describe("request body", () => {
|
|
test("should send tools in OpenAI format", async () => {
|
|
let capturedBody: unknown
|
|
const mockFetch = mock(async (_url: string, init?: RequestInit) => {
|
|
capturedBody = JSON.parse(init?.body as string)
|
|
return new Response(
|
|
new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(new TextEncoder().encode(`data: [DONE]\n\n`))
|
|
controller.close()
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "Content-Type": "text/event-stream" } },
|
|
)
|
|
})
|
|
|
|
const model = createModel(mockFetch)
|
|
|
|
await model.doStream({
|
|
prompt: TEST_PROMPT,
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
name: "get_weather",
|
|
description: "Get the weather for a location",
|
|
inputSchema: {
|
|
type: "object",
|
|
properties: {
|
|
location: { type: "string" },
|
|
},
|
|
required: ["location"],
|
|
},
|
|
},
|
|
],
|
|
includeRawChunks: false,
|
|
})
|
|
|
|
expect((capturedBody as { tools: unknown[] }).tools).toEqual([
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "get_weather",
|
|
description: "Get the weather for a location",
|
|
parameters: {
|
|
type: "object",
|
|
properties: {
|
|
location: { type: "string" },
|
|
},
|
|
required: ["location"],
|
|
},
|
|
},
|
|
},
|
|
])
|
|
})
|
|
})
|