Skip to content

fix: migrate Assistant prompts to the current conversation API - #145

Merged
Microck merged 1 commit into
mainfrom
fix/assistant-current-api
Jul 25, 2026
Merged

fix: migrate Assistant prompts to the current conversation API#145
Microck merged 1 commit into
mainfrom
fix/assistant-current-api

Conversation

@Microck

@Microck Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • migrate kagi assistant and kagi ask-page from the removed legacy prompt endpoint to Kagi's current conversation, branch message, upload, and SSE endpoints
  • preserve the existing structured response and streaming contracts
  • add regression coverage for new conversations, thread continuation, attachments, streaming, and page questions
  • update the changelog

Fixes #144

Verification

  • cargo fmt --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test -q
  • live kagi assistant prompt returned the expected completed Markdown response
  • live kagi ask-page prompt returned a completed non-empty Markdown answer

Docs

  • README does not need changes because the command interface is unchanged
  • CHANGELOG updated for the user-facing fix

Auth / Secrets

  • No tokens, cookies, or local config secrets were committed
  • Live verification is documented without exposing credentials

Summary by CodeRabbit

  • Bug Fixes
    • Updated kagi assistant and kagi ask-page to use Kagi’s current Assistant conversation API.
    • Restored assistant prompts and streaming responses after the legacy prompt endpoint was removed.
    • Improved handling of attachments, conversation branches, incremental markdown, and completed responses.
  • Tests
    • Expanded coverage for conversation creation, file uploads, streaming behavior, and delayed final responses.
  • Documentation
    • Added the Assistant API migration to the unreleased changelog.

Greptile Summary

Migrates Assistant prompt handling to Kagi’s current conversation API.

  • Creates or initializes conversations and posts messages to active branches.
  • Uploads attachments separately and references their UUIDs in message payloads.
  • Parses streaming SSE responses while preserving split UTF-8 and supporting LF and CRLF framing.
  • Adds regression coverage and updates the changelog.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failures remain.

Files Needing Attention: No files require additional attention.

T-Rex T-Rex Logs

What T-Rex did

  • The baseline proof from the parent commit b92e881 was captured, with exit code 0.
  • The changed-code proof from commit 800c634 was captured, and every executed command exited with code 0.
  • Existing httpmock assertions exercised the migrated HTTP contracts and completed Markdown/stream output without contacting live Kagi services.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
src/api.rs Replaces the legacy Assistant prompt endpoint with conversation, branch-message, attachment-upload, and SSE-stream handling; the previously reported UTF-8 and CRLF framing defects are addressed.
tests/integration-cli.rs Updates integration mocks and assertions to cover the current Assistant conversation API workflows.
CHANGELOG.md Documents restoration of Assistant and ask-page commands through the current conversation API.

Reviews (3): Last reviewed commit: "fix: migrate Assistant prompts to curren..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@Microck, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 32 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5970141e-9d93-4c9e-b66c-c627c5ec4bfa

📥 Commits

Reviewing files that changed from the base of the PR and between 36e0d30 and 800c634.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/api.rs
  • tests/integration-cli.rs
📝 Walkthrough

Walkthrough

kagi assistant and kagi ask-page now use Kagi’s current Assistant conversation API, including attachment uploads, message creation, branch SSE streaming, and final-frame parsing. Legacy prompt endpoints and parsers were removed, and unit/integration mocks were updated.

Changes

Assistant API migration

Layer / File(s) Summary
Current Assistant request flow
src/api.rs
Assistant prompt execution now prepares conversations, uploads attachments, posts messages, and consumes branch streams through the current Assistant API.
Current Assistant protocol parsing
src/api.rs
New protocol structures and CurrentAssistantPromptParser process SSE updates, markdown deltas, message metadata, references, and final-frame completion.
Migration validation and release notes
src/api.rs, tests/integration-cli.rs, CHANGELOG.md
Tests mock the new conversation flow, uploads, SSE responses, delayed streams, and contract outputs; the changelog records the endpoint migration.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant AssistantAPI
  participant UploadAPI
  participant BranchStream
  CLI->>AssistantAPI: create or prepare conversation
  CLI->>UploadAPI: upload attachments
  CLI->>AssistantAPI: post assistant message
  AssistantAPI->>BranchStream: open branch SSE stream
  BranchStream-->>CLI: markdown update frames
  BranchStream-->>CLI: final frame and DONE marker
Loading

Suggested reviewers: nickclyde, gfs

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Release Metadata Consistency ⚠️ Warning Cargo.toml and Cargo.lock are 0.14.3, but npm/package.json is still 0.14.2; CHANGELOG does have ## [0.14.3]. Bump npm/package.json version to 0.14.3 so all three versions match, then keep the existing CHANGELOG [0.14.3] heading.
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #144 by replacing the legacy prompt flow with the current conversation API and preserving structured output.
Out of Scope Changes check ✅ Passed The modified files stay focused on the API migration, related tests, and changelog update with no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely summarizes the main change: migrating Assistant prompts to the current conversation API.
Description check ✅ Passed The description follows the template well and includes summary, verification, docs, and auth/secrets sections with the required details.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/assistant-current-api

Comment @coderabbitai help to get the list of available commands.

@Microck

Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/api.rs (2)

634-637: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider preparing the conversation before uploading attachments.

If conversation create/init fails, attachments have already been uploaded to Kagi and are orphaned (and the work is wasted). Swapping the two awaits fails fast on the cheaper/likelier-to-fail call.

♻️ Proposed reorder
-    let attachment_uuids =
-        upload_current_assistant_attachments(&request.attachments, token).await?;
-    let (_conversation, branch) =
-        prepare_current_assistant_conversation(thread_id.as_deref(), request, token).await?;
+    let (_conversation, branch) =
+        prepare_current_assistant_conversation(thread_id.as_deref(), request, token).await?;
+    let attachment_uuids =
+        upload_current_assistant_attachments(&request.attachments, token).await?;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/api.rs` around lines 634 - 637, In the request flow around
prepare_current_assistant_conversation and upload_current_assistant_attachments,
await conversation preparation before uploading attachments. Preserve both
existing error-propagation paths and pass the resulting conversation/branch and
attachment UUIDs to their current downstream uses.

6346-6356: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Leftover /assistant/prompt mocks after the migration. The legacy prompt endpoint is gone, but several tests still register mocks for it; they can never match and only obscure which flow is being asserted.

  • src/api.rs#L6346-L6356: drop the _prompt mock that follows the new stream mock in assistant_prompt_uses_multipart_when_attachments_are_present.
  • src/api.rs#L6443-L6454: drop the _prompt mock that follows the delayed stream mock in assistant_prompt_accepts_delayed_stream_response.
  • tests/integration-cli.rs#L2066-L2066: drop the _prompt mocks in assistant_stream_prints_text_deltas_by_default, assistant_stream_can_print_ndjson_updates, and assistant_once_creates_prompts_and_deletes_temporary_profile.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/api.rs` around lines 6346 - 6356, Remove the obsolete /assistant/prompt
mock registrations from the listed tests: src/api.rs lines 6346-6356 in
assistant_prompt_uses_multipart_when_attachments_are_present, src/api.rs lines
6443-6454 in assistant_prompt_accepts_delayed_stream_response, and
tests/integration-cli.rs line 2066 in
assistant_stream_prints_text_deltas_by_default,
assistant_stream_can_print_ndjson_updates, and
assistant_once_creates_prompts_and_deletes_temporary_profile. Keep the new
stream mocks and all other test setup unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/api.rs`:
- Around line 802-812: Update the SSE reader around response.bytes_stream() so
it buffers raw bytes across chunks instead of converting each chunk with
String::from_utf8_lossy. Detect complete "\n\n"-delimited frames at the byte
level, decode each complete frame as UTF-8 only after extraction, and retain
incomplete frame bytes for the next chunk so split multi-byte characters are
never replaced or corrupted.
- Around line 711-741: Update both assistant conversation HTTP requests in the
conversation initialization/create flow: the GET request before
read_current_assistant_json_response and the POST request using
KAGI_ASSISTANT_CONVERSATIONS_PATH. Add an Accept header requesting
application/json alongside the existing headers, preserving all other request
behavior.

---

Nitpick comments:
In `@src/api.rs`:
- Around line 634-637: In the request flow around
prepare_current_assistant_conversation and upload_current_assistant_attachments,
await conversation preparation before uploading attachments. Preserve both
existing error-propagation paths and pass the resulting conversation/branch and
attachment UUIDs to their current downstream uses.
- Around line 6346-6356: Remove the obsolete /assistant/prompt mock
registrations from the listed tests: src/api.rs lines 6346-6356 in
assistant_prompt_uses_multipart_when_attachments_are_present, src/api.rs lines
6443-6454 in assistant_prompt_accepts_delayed_stream_response, and
tests/integration-cli.rs line 2066 in
assistant_stream_prints_text_deltas_by_default,
assistant_stream_can_print_ndjson_updates, and
assistant_once_creates_prompts_and_deletes_temporary_profile. Keep the new
stream mocks and all other test setup unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d26f5320-0c62-43cc-af33-9a96f0b3b9d0

📥 Commits

Reviewing files that changed from the base of the PR and between b92e881 and 36e0d30.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/api.rs
  • tests/integration-cli.rs

Comment thread src/api.rs
Comment thread src/api.rs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 36e0d30fb0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/api.rs Outdated
Comment thread src/api.rs Outdated
Comment thread src/api.rs Outdated
Comment thread src/api.rs Outdated
Comment thread src/api.rs Outdated
@Microck
Microck force-pushed the fix/assistant-current-api branch from 36e0d30 to 1364829 Compare July 25, 2026 20:30
@Microck

Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@greptileai review

@Microck

Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1364829735

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/api.rs Outdated
@Microck
Microck force-pushed the fix/assistant-current-api branch from 1364829 to 800c634 Compare July 25, 2026 20:40
@Microck

Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@greptileai review

@Microck

Microck commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@Microck
Microck merged commit bd3fe81 into main Jul 25, 2026
7 checks passed
@Microck
Microck deleted the fix/assistant-current-api branch July 25, 2026 20:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 800c63431f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/api.rs
Comment on lines +727 to +729
.get(http::kagi_assistant_url(&format!(
"/api/conversations/{thread_id}/init"
)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the branch from thread URLs

When --thread-id is a branch-specific URL such as https://assistant.kagi.com/chat/thread-1/branch-1, normalize_assistant_thread_id deliberately reduces it to only thread-1, and this request initializes the conversation without branch_uuid. The subsequent message is therefore posted to whichever branch the API reports as active/default rather than the branch named by the user, so follow-ups can continue the wrong conversation branch. Parse the prompt target with normalize_assistant_thread_ref and pass its branch UUID to the init request.

Useful? React with 👍 / 👎.

Comment thread src/api.rs
Comment on lines +802 to +805
if !response.status().is_success() {
let status = response.status();
let body = http::read_error_body(response, "Assistant prompt stream").await;
if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Detect login HTML before parsing the SSE body

When an expired session is redirected to a successful 200 login HTML response, this status check accepts it as an SSE stream. The HTML is later ignored because it contains no data: lines, and finish() reports a misleading parse error instead of an authentication error; structured callers consequently receive parse_error without the session-auth remediation. Retain the prior logged-out HTML detection (or validate the response content type/body) on the successful stream path.

Useful? React with 👍 / 👎.

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.

[bug] Assistant tools return HTML instead of structured output

1 participant