Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Migrates the Wandbox compilation API from /api/compile.json (batch response) to /api/compile.ndjson (streaming response). The function interface remains unchanged—results are still collected and returned after completion.

Changes

  • Added CompileNdjsonResult interface to represent individual ndjson response lines with type and data fields
  • Updated compileAndRun() to consume ndjson stream using response.body.getReader() for incremental processing with proper buffering for incomplete lines
  • Unified output structure by changing CompileResultWithOutput to use a single output: ReplOutput[] array instead of separate arrays, storing responses in the order they are received
  • Updated cpp.ts to work with the new unified output structure
  • Added HTTP error handling for non-OK responses

Implementation

The stream reader processes chunks incrementally, buffering incomplete lines and parsing complete JSON objects as they arrive:

const reader = response.body?.getReader();
const decoder = new TextDecoder();
let buffer = "";

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  
  buffer += decoder.decode(value, { stream: true });
  const lines = buffer.split("\n");
  buffer = lines.pop() || "";
  
  for (const line of lines) {
    if (line.trim().length > 0) {
      ndjsonResults.push(JSON.parse(line));
    }
  }
}

The merge logic accumulates streaming messages into both a CompileResult and a unified output array that preserves chronological order:

for (const r of ndjsonResults) {
  switch (r.type) {
    case "CompilerMessageS":
      result.compiler_output += r.data;
      result.compiler_message += r.data;
      // Add to output array in order
      for (const line of r.data.trim().split("\n")) {
        output.push({ type: "stdout", message: line });
      }
      break;
    // ... other cases
  }
}

This matches the Rust merge_compile_result implementation in the wandbox repository (feline/src/util.rs) while providing better flexibility for future interface changes.

Original prompt

app/terminal/wandbox/ にて、 wandbox.org/api/compile.json のAPIを呼び出しているところを、 /api/compile.ndjson のAPI呼び出しに変更してください。APIの仕様については https://github.com/melpon/wandbox のリポジトリの feline/src/ 以下のソースコードを参照してください。
ndjsonのAPIを使うことで結果をまとめて返すのではなく逐次得られる結果を処理することが可能になりますが、今回のPRではrunFiles()関数自体の仕様は変えず、結果がすべて返ってきてからreturnする仕様のままにしてください。


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 18, 2025

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
my-code b3f566e Commit Preview URL

Branch Preview URL
Nov 18 2025, 11:09 AM

Copilot AI changed the title [WIP] Change API call to compile.ndjson endpoint Migrate Wandbox API from /api/compile.json to /api/compile.ndjson Nov 18, 2025
Copilot AI requested a review from na-trium-144 November 18, 2025 10:18
@na-trium-144
Copy link
Contributor

@copilot あとから関数のインタフェースを変更しやすいように、APIのレスポンスをtext()でまとめて取得するのではなくresponse.bodyからストリームとして読み取って処理する実装にしてください。
また、CompileResultWithOutputの型を

export interface CompileResultWithOutput extends CompileResult {
  output: ReplOutput[];
}

に変更し、outputやerrorの種類によらず1つのarrayにレスポンスを受け取った順に保存するようにしてください

- Use response.body stream instead of response.text() for better flexibility
- Change CompileResultWithOutput to use single output array instead of separate arrays
- Store outputs in the order they are received from the ndjson stream
- Update cpp.ts to work with the new unified output structure

Co-authored-by: na-trium-144 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Nov 18, 2025

@copilot あとから関数のインタフェースを変更しやすいように、APIのレスポンスをtext()でまとめて取得するのではなくresponse.bodyからストリームとして�...

変更しました (b3f566e)

  • response.text() から response.body.getReader() を使ったストリーム処理に変更
  • CompileResultWithOutput を単一の output: ReplOutput[] 配列に変更し、レスポンスを受け取った順に保存するようにしました

Copilot AI changed the title Migrate Wandbox API from /api/compile.json to /api/compile.ndjson Migrate Wandbox API from /api/compile.json to /api/compile.ndjson with streaming support Nov 18, 2025
@na-trium-144 na-trium-144 marked this pull request as ready for review November 18, 2025 18:34
@na-trium-144 na-trium-144 merged commit 458d3fc into main Nov 18, 2025
5 checks passed
@na-trium-144 na-trium-144 deleted the copilot/update-api-call-to-ndjson branch November 18, 2025 18:34
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.

2 participants