Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: decode messages in AI logs (#42705)

Sam8272d3c484da
diff --git a/frontend/discourse/app/ui-kit/d-copy-button.gjs b/frontend/discourse/app/ui-kit/d-copy-button.gjs
index cabf74bb07a..c7e709f7138 100644
--- a/frontend/discourse/app/ui-kit/d-copy-button.gjs
+++ b/frontend/discourse/app/ui-kit/d-copy-button.gjs
@@ -54,14 +54,27 @@ export default class DCopyButton extends Component {
@action
async copy() {
- const target = document.querySelector(this.args.selector);
+ let value = this.args.value;
- if (!target) {
+ if (value === undefined) {
+ if (!this.args.selector) {
+ return;
+ }
+
+ const target = document.querySelector(this.args.selector);
+ if (!target) {
+ return;
+ }
+
+ value = target.value ?? target.textContent;
+ }
+
+ if (value == null) {
return;
}
try {
- await clipboardCopy(target.value ?? target.textContent);
+ await clipboardCopy(value);
this.args.copied?.();
this._showCopied();
} catch {}
diff --git a/frontend/discourse/tests/integration/components/d-copy-button-test.gjs b/frontend/discourse/tests/integration/components/d-copy-button-test.gjs
index 06099ec1dc4..081af964649 100644
--- a/frontend/discourse/tests/integration/components/d-copy-button-test.gjs
+++ b/frontend/discourse/tests/integration/components/d-copy-button-test.gjs
@@ -1,6 +1,7 @@
import { tracked } from "@glimmer/tracking";
-import { render, rerender } from "@ember/test-helpers";
+import { click, render, rerender } from "@ember/test-helpers";
import { module, test } from "qunit";
+import sinon from "sinon";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import DCopyButton from "discourse/ui-kit/d-copy-button";
@@ -26,6 +27,29 @@ module("Integration | Component | DCopyButton", function (hooks) {
);
});
+ test("copies a direct value without a DOM source", async function (assert) {
+ const writeText = sinon.stub().resolves();
+ sinon.stub(window.navigator, "clipboard").get(() => ({ writeText }));
+
+ await render(
+ <template>
+ <DCopyButton
+ @value="direct payload"
+ @selector=".missing-copy-source"
+ @translatedLabel="Copy"
+ @translatedLabelAfterCopy="Copied!"
+ />
+ </template>
+ );
+
+ await click(".copy-button");
+
+ assert.true(
+ writeText.calledWithExactly("direct payload"),
+ "the direct value takes precedence over the selector"
+ );
+ });
+
test("@isCopied shows the confirmation for a copy made by the caller", async function (assert) {
const state = new (class {
@tracked isCopied = false;
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-logs.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-logs.gjs
index b7a0ae9bfcc..53a59b69a74 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-logs.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-logs.gjs
@@ -7,6 +7,7 @@ import { service } from "@ember/service";
import Form from "discourse/components/form";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
+import DiscourseURL from "discourse/lib/url";
import UserChooser from "discourse/select-kit/components/user-chooser";
import { eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
@@ -53,6 +54,7 @@ export default class AiLogs extends Component {
filterFormApi;
_requestId = 0;
+ _openRequestId = 0;
_openLogId;
constructor() {
@@ -262,6 +264,18 @@ export default class AiLogs extends Component {
});
}
+ updateDetailsUrl(logId, { replace = false } = {}) {
+ const url = new URL(window.location.href);
+ if (logId) {
+ url.searchParams.set("details", logId);
+ } else {
+ url.searchParams.delete("details");
+ }
+
+ const path = `${url.pathname}${url.search}${url.hash}`;
+ DiscourseURL[replace ? "replaceState" : "pushState"](path);
+ }
+
@action
async refresh() {
const requestId = ++this._requestId;
@@ -496,16 +510,24 @@ export default class AiLogs extends Component {
@action
openDetails(logId, { updateUrl = true } = {}) {
- this._openLogId = String(logId);
+ const openLogId = String(logId);
+ const openRequestId = ++this._openRequestId;
+ this._openLogId = openLogId;
if (updateUrl) {
- this.updateUrl({ details: this._openLogId });
+ this.updateDetailsUrl(openLogId);
}
+
this.modal.show(AiLogDetailModal, {
model: {
logId,
onClose: () => {
- this._openLogId = undefined;
- this.updateUrl({ details: null });
+ if (
+ this._openRequestId === openRequestId &&
+ this._openLogId === openLogId
+ ) {
+ this._openLogId = undefined;
+ this.updateDetailsUrl(null, { replace: true });
+ }
},
},
});
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/modal/ai-log-detail-modal.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/modal/ai-log-detail-modal.gjs
index baaae495b46..4454f9e27e8 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/modal/ai-log-detail-modal.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/modal/ai-log-detail-modal.gjs
@@ -8,9 +8,15 @@ import getURL from "discourse/lib/get-url";
import { eq, or } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
+import DCopyButton from "discourse/ui-kit/d-copy-button";
import DModal from "discourse/ui-kit/d-modal";
import { i18n } from "discourse-i18n";
+import AiDecodedTranscript from "discourse/plugins/discourse-ai/discourse/components/ai-decoded-transcript";
import AiPayloadViewer from "discourse/plugins/discourse-ai/discourse/components/ai-payload-viewer";
+import {
+ decodedResponseText,
+ isDecodedResponse,
+} from "discourse/plugins/discourse-ai/discourse/lib/decoded-response";
export default class AiLogDetailModal extends Component {
@tracked log;
@@ -18,6 +24,7 @@ export default class AiLogDetailModal extends Component {
@tracked failed = false;
@tracked notFound = false;
@tracked activeTab = "request";
+ @tracked showRawResponse = false;
constructor() {
super(...arguments);
@@ -29,6 +36,7 @@ export default class AiLogDetailModal extends Component {
this.loading = true;
this.failed = false;
this.notFound = false;
+ this.showRawResponse = false;
try {
const log = await ajax(
@@ -82,10 +90,62 @@ export default class AiLogDetailModal extends Component {
: this.log?.raw_response_payload_truncated;
}
+ get hasDecodedResponse() {
+ return isDecodedResponse(this.log?.decoded_response);
+ }
+
+ get showDecodedResponse() {
+ return (
+ this.activeTab === "response" &&
+ !this.showRawResponse &&
+ this.hasDecodedResponse
+ );
+ }
+
+ get showResponseToggle() {
+ return this.activeTab === "response" && this.hasDecodedResponse;
+ }
+
+ get copyPayload() {
+ if (this.activeTab === "context") {
+ return this.contextPayload;
+ }
+
+ if (this.showDecodedResponse) {
+ return decodedResponseText(this.log.decoded_response);
+ }
+
+ if (this.activeTab === "request" || this.activeTab === "response") {
+ return this.activePayload;
+ }
+
+ return null;
+ }
+
get activeCopyLabel() {
- return this.activeTab === "request"
- ? i18n("discourse_ai.logs.detail.copy_request")
- : i18n("discourse_ai.logs.detail.copy_response");
+ if (this.activeTab === "context") {
+ return i18n("discourse_ai.logs.detail.copy_context");
+ }
+
+ if (this.activeTab === "request") {
+ return i18n("discourse_ai.logs.detail.copy_request");
+ }
+
+ if (!this.hasDecodedResponse) {
+ return i18n("discourse_ai.logs.detail.copy_response");
+ }
+
+ return i18n(
+ `discourse_ai.logs.detail.${
+ this.showRawResponse ? "copy_raw_response" : "copy_response"
+ }`
+ );
+ }
+
+ get responseToggleLabel() {
+ return i18n(
+ `discourse_ai.${this.showRawResponse ? "view_decoded" : "view_raw"}`
+ );
}
get contextPayload() {
@@ -167,6 +227,12 @@ export default class AiLogDetailModal extends Component {
@action
selectTab(tab) {
this.activeTab = tab;
+ this.showRawResponse = false;
+ }
+
+ @action
+ toggleResponseView() {
+ this.showRawResponse = !this.showRawResponse;
}
@action
@@ -255,31 +321,55 @@ export default class AiLogDetailModal extends Component {
{{/if}}
</dl>
- <nav
- class="ai-log-detail-modal__tabs"
- aria-label={{i18n "discourse_ai.logs.detail.tabs_label"}}
- >
- {{#each this.tabs as |tab|}}
- <DButton
- class={{if
- (eq this.activeTab tab.id)
- "btn-primary"
- "btn-default"
- }}
- @action={{fn this.selectTab tab.id}}
- @ariaPressed={{eq this.activeTab tab.id}}
- @translatedLabel={{tab.label}}
- />
- {{/each}}
- </nav>
-
- {{#if
+ <div class="ai-log-detail-modal__tabs">
+ <nav
+ class="ai-log-detail-modal__tab-list"
+ aria-label={{i18n "discourse_ai.logs.detail.tabs_label"}}
+ >
+ {{#each this.tabs as |tab|}}
+ <DButton
+ class={{if
+ (eq this.activeTab tab.id)
+ "btn-primary"
+ "btn-default"
+ }}
+ @action={{fn this.selectTab tab.id}}
+ @ariaPressed={{eq this.activeTab tab.id}}
+ @translatedLabel={{tab.label}}
+ />
+ {{/each}}
+ </nav>
+ {{#if this.copyPayload}}
+ <div class="ai-log-detail-modal__actions">
+ {{#if this.showResponseToggle}}
+ <DButton
+ class="btn-default ai-log-detail-modal__response-toggle"
+ @action={{this.toggleResponseView}}
+ @translatedLabel={{this.responseToggleLabel}}
+ />
+ {{/if}}
+ <DCopyButton
+ @value={{this.copyPayload}}
+ @copyClass="btn-default ai-log-detail-modal__copy"
+ @translatedLabel={{this.activeCopyLabel}}
+ @translatedLabelAfterCopy={{i18n "discourse_ai.copied"}}
+ />
+ </div>
+ {{/if}}
+ </div>
+
+ {{#if this.showDecodedResponse}}
+ <AiDecodedTranscript
+ class="ai-log-detail-modal__preview"
+ @response={{this.log.decoded_response}}
+ />
+ {{else if
(or (eq this.activeTab "request") (eq this.activeTab "response"))
}}
<AiPayloadViewer
@payload={{this.activePayload}}
+ @unbounded={{true}}
@truncated={{this.activePayloadTruncated}}
- @copyLabel={{this.activeCopyLabel}}
@emptyMessage={{i18n
"discourse_ai.logs.detail.payload_unavailable"
}}
@@ -290,7 +380,7 @@ export default class AiLogDetailModal extends Component {
{{else if (eq this.activeTab "context")}}
<AiPayloadViewer
@payload={{this.contextPayload}}
- @copyLabel={{i18n "discourse_ai.logs.detail.copy_context"}}
+ @unbounded={{true}}
@emptyMessage={{i18n
"discourse_ai.logs.detail.payload_unavailable"
}}
diff --git a/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb b/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb
index 3722469b1e2..de5fd8a2ec8 100644
--- a/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/ai_api_audit_log_serializer.rb
@@ -10,6 +10,7 @@ class AiApiAuditLogSerializer < ApplicationSerializer
:cache_write_tokens,
:raw_request_payload,
:raw_response_payload,
+ :decoded_response,
:topic_id,
:post_id,
:feature_name,
@@ -29,6 +30,10 @@ class AiApiAuditLogSerializer < ApplicationSerializer
:conversation_cache_write_tokens,
:conversation_spending
+ def decoded_response
+ DiscourseAi::AiApiAuditLogResponseDecoder.decode(object.raw_response_payload)
+ end
+
def request_attempts
object.request_attempts
end
diff --git a/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_detail_serializer.rb b/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_detail_serializer.rb
index 1fbd4189f99..e8b911abcde 100644
--- a/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_detail_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_detail_serializer.rb
@@ -28,12 +28,20 @@ module DiscourseAi
:request_attempts,
:raw_request_payload,
:raw_response_payload,
+ :decoded_response,
:raw_request_payload_bytes,
:raw_response_payload_bytes,
:raw_request_payload_truncated,
:raw_response_payload_truncated,
:payload_available
+ def decoded_response
+ DiscourseAi::AiApiAuditLogResponseDecoder.decode(
+ object.raw_response_payload,
+ truncated: raw_response_payload_truncated,
+ )
+ end
+
def provider_name
AiLogSerializerHelpers.provider_name(object.provider_id)
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-decoded-transcript.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-decoded-transcript.gjs
new file mode 100644
index 00000000000..df8af1e414e
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-decoded-transcript.gjs
@@ -0,0 +1,183 @@
+import Component from "@glimmer/component";
+import { cached } from "@glimmer/tracking";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+import { i18n } from "discourse-i18n";
+import {
+ isDecodedResponse,
+ isValidDecodedToolCall,
+ isValidDecodedToolResult,
+} from "discourse/plugins/discourse-ai/discourse/lib/decoded-response";
+
+export default class AiDecodedTranscript extends Component {
+ @cached
+ get transcript() {
+ const response = this.args.response;
+ if (!isDecodedResponse(response)) {
+ return null;
+ }
+
+ const transcript = {
+ thinking: this.#format(response.thinking),
+ toolCalls: this.#toolCalls(response.tool_calls),
+ toolResults: this.#toolResults(response.tool_results),
+ response: this.#format(response.response),
+ };
+
+ return transcript.thinking ||
+ transcript.response ||
+ transcript.toolCalls.length ||
+ transcript.toolResults.length
+ ? transcript
+ : null;
+ }
+
+ #toolCalls(toolCalls) {
+ if (!Array.isArray(toolCalls)) {
+ return [];
+ }
+
+ return toolCalls.filter(isValidDecodedToolCall).map((call) => {
+ return {
+ name:
+ this.#format(call.name) ||
+ i18n("discourse_ai.ai_decoded_transcript.tool"),
+ id: this.#format(call.id),
+ arguments: this.#format(call.arguments),
+ };
+ });
+ }
+
+ #toolResults(toolResults) {
+ if (!Array.isArray(toolResults)) {
+ return [];
+ }
+
+ return toolResults.filter(isValidDecodedToolResult).map((result) => {
+ return {
+ callId: this.#format(result.call_id),
+ type: this.#format(result.type),
+ content: this.#format(result.result),
+ isError: result.is_error === true,
+ };
+ });
+ }
+
+ #format(value) {
+ if (value === undefined || value === null) {
+ return "";
+ }
+
+ if (typeof value === "string") {
+ return value;
+ }
+
+ return JSON.stringify(value, null, 2);
+ }
+
+ <template>
+ {{#if this.transcript}}
+ <div class="ai-decoded-transcript" ...attributes>
+ {{#if this.transcript.thinking}}
+ <details class="ai-decoded-transcript__thinking" open>
+ <summary class="ai-decoded-transcript__summary">
+ {{i18n "discourse_ai.ai_decoded_transcript.thinking"}}
+ </summary>
+ <div
+ class="ai-decoded-transcript__text"
+ >{{this.transcript.thinking}}</div>
+ </details>
+ {{/if}}
+
+ {{#if this.transcript.toolCalls.length}}
+ <section class="ai-decoded-transcript__section --tool-calls">
+ <h2 class="ai-decoded-transcript__heading">
+ {{i18n "discourse_ai.ai_decoded_transcript.tool_calls"}}
+ </h2>
+ <ol class="ai-decoded-transcript__list" role="list">
+ {{#each this.transcript.toolCalls as |toolCall|}}
+ <li class="ai-decoded-transcript__item">
+ <h3 class="ai-decoded-transcript__item-heading">
+ {{toolCall.name}}
+ </h3>
+ {{#if toolCall.id}}
+ <dl class="ai-decoded-transcript__metadata">
+ <dt>{{i18n
+ "discourse_ai.ai_decoded_transcript.call_id"
+ }}</dt>
+ <dd>{{toolCall.id}}</dd>
+ </dl>
+ {{/if}}
+ <h4 class="ai-decoded-transcript__label">
+ {{i18n "discourse_ai.ai_decoded_transcript.arguments"}}
+ </h4>
+ <pre
+ class="ai-decoded-transcript__code"
+ dir="ltr"
+ >{{toolCall.arguments}}</pre>
+ </li>
+ {{/each}}
+ </ol>
+ </section>
+ {{/if}}
+
+ {{#if this.transcript.toolResults.length}}
+ <section class="ai-decoded-transcript__section --tool-results">
+ <h2 class="ai-decoded-transcript__heading">
+ {{i18n "discourse_ai.ai_decoded_transcript.tool_results"}}
+ </h2>
+ <ol class="ai-decoded-transcript__list" role="list">
+ {{#each this.transcript.toolResults as |toolResult|}}
+ <li
+ class={{dConcatClass
+ "ai-decoded-transcript__item"
+ (if toolResult.isError "--error")
+ }}
+ >
+ <h3 class="ai-decoded-transcript__item-heading">
+ {{i18n "discourse_ai.ai_decoded_transcript.tool_result"}}
+ {{#if toolResult.isError}}
+ <span class="ai-decoded-transcript__error">
+ {{i18n "discourse_ai.ai_decoded_transcript.error"}}
+ </span>
+ {{/if}}
+ </h3>
+ {{#if toolResult.callId}}
+ <dl class="ai-decoded-transcript__metadata">
+ <dt>{{i18n
+ "discourse_ai.ai_decoded_transcript.call_id"
+ }}</dt>
+ <dd>{{toolResult.callId}}</dd>
+ </dl>
+ {{/if}}
+ {{#if toolResult.type}}
+ <dl class="ai-decoded-transcript__metadata">
+ <dt>{{i18n
+ "discourse_ai.ai_decoded_transcript.type"
+ }}</dt>
+ <dd>{{toolResult.type}}</dd>
+ </dl>
+ {{/if}}
+ <pre
+ class="ai-decoded-transcript__code"
+ dir="ltr"
+ >{{toolResult.content}}</pre>
+ </li>
+ {{/each}}
+ </ol>
+ </section>
+ {{/if}}
+
+ {{#if this.transcript.response}}
+ <section class="ai-decoded-transcript__section --response">
+ <h2 class="ai-decoded-transcript__heading">
+ {{i18n "discourse_ai.ai_decoded_transcript.response"}}
+ </h2>
+ <div
+ class="ai-decoded-transcript__text"
+ >{{this.transcript.response}}</div>
+ </section>
+ {{/if}}
+ </div>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-payload-viewer.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-payload-viewer.gjs
index 574ffb705d2..8676a7b3c4c 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-payload-viewer.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-payload-viewer.gjs
@@ -1,8 +1,8 @@
import Component from "@glimmer/component";
import { cached } from "@glimmer/tracking";
-import { action } from "@ember/object";
-import { clipboardCopy } from "discourse/lib/utilities";
-import DButton from "discourse/ui-kit/d-button";
+import DCopyButton from "discourse/ui-kit/d-copy-button";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+import { i18n } from "discourse-i18n";
const JSON_FORMAT_LIMIT = 200_000;
@@ -25,28 +25,30 @@ export default class AiPayloadViewer extends Component {
}
}
- @action
- copy() {
- clipboardCopy(this.args.payload || "");
- this.args.onCopy?.();
- }
-
<template>
- <div class="ai-payload-viewer" ...attributes>
+ <div
+ class={{dConcatClass "ai-payload-viewer" (if @unbounded "--unbounded")}}
+ ...attributes
+ >
{{#if @payload}}
{{#if @truncated}}
<p class="ai-payload-viewer__notice">
{{@truncatedMessage}}
</p>
{{/if}}
- <pre class="ai-payload-viewer__content" tabindex="0"><code
- >{{this.content}}</code></pre>
- <DButton
- class="btn-default ai-payload-viewer__copy"
- @icon="copy"
- @action={{this.copy}}
- @translatedLabel={{@copyLabel}}
- />
+ <pre
+ class="ai-payload-viewer__content"
+ tabindex={{if @unbounded undefined "0"}}
+ ><code>{{this.content}}</code></pre>
+ {{#if @copyLabel}}
+ <DCopyButton
+ @value={{@payload}}
+ @copyClass="btn-default ai-payload-viewer__copy"
+ @translatedLabel={{@copyLabel}}
+ @translatedLabelAfterCopy={{i18n "discourse_ai.copied"}}
+ @copied={{@onCopy}}
+ />
+ {{/if}}
{{else}}
<p class="ai-payload-viewer__empty">{{@emptyMessage}}</p>
{{/if}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/debug-ai-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/debug-ai-modal.gjs
index 13de1ff61a3..76f7782ee54 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/debug-ai-modal.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/debug-ai-modal.gjs
@@ -5,16 +5,21 @@ import { action } from "@ember/object";
import { next } from "@ember/runloop";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
-import discourseLater from "discourse/lib/later";
import DButton from "discourse/ui-kit/d-button";
+import DCopyButton from "discourse/ui-kit/d-copy-button";
import DModal from "discourse/ui-kit/d-modal";
import { i18n } from "discourse-i18n";
+import AiDecodedTranscript from "discourse/plugins/discourse-ai/discourse/components/ai-decoded-transcript";
import AiPayloadViewer from "discourse/plugins/discourse-ai/discourse/components/ai-payload-viewer";
+import {
+ decodedResponseText,
+ isDecodedResponse,
+} from "discourse/plugins/discourse-ai/discourse/lib/decoded-response";
export default class DebugAiModal extends Component {
@tracked info = null;
- @tracked justCopiedText = "";
@tracked activeTab = "request";
+ @tracked showRawResponse = false;
constructor() {
super(...arguments);
@@ -33,22 +38,45 @@ export default class DebugAiModal extends Component {
: this.info.raw_response_payload;
}
- get activeCopyLabel() {
- return this.activeTab === "request"
- ? i18n("discourse_ai.ai_bot.debug_ai_modal.copy_request")
- : i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response");
+ get hasDecodedResponse() {
+ return isDecodedResponse(this.info?.decoded_response);
}
- @action
- copied() {
- this.justCopiedText = i18n("discourse_ai.ai_bot.conversation_shared");
+ get showDecodedResponse() {
+ return (
+ this.activeTab === "response" &&
+ !this.showRawResponse &&
+ this.hasDecodedResponse
+ );
+ }
+
+ get showResponseToggle() {
+ return this.activeTab === "response" && this.hasDecodedResponse;
+ }
+
+ get copyValue() {
+ return this.showDecodedResponse
+ ? decodedResponseText(this.info.decoded_response)
+ : this.activePayload;
+ }
+
+ get activeCopyLabel() {
+ if (this.activeTab === "request") {
+ return i18n("discourse_ai.ai_bot.debug_ai_modal.copy_request");
+ }
+
+ return i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response");
+ }
- discourseLater(() => {
- this.justCopiedText = "";
- }, 2000);
+ get responseToggleLabel() {
+ return i18n(
+ `discourse_ai.${this.showRawResponse ? "view_decoded" : "view_raw"}`
+ );
}
async loadLog(logId) {
+ this.showRawResponse = false;
+
try {
await ajax(`/discourse-ai/ai-bot/show-debug-info/${logId}.json`).then(
(result) => {
@@ -71,6 +99,7 @@ export default class DebugAiModal extends Component {
}
loadApiRequestInfo() {
+ this.showRawResponse = false;
ajax(`/discourse-ai/ai-bot/post/${this.args.model.id}/show-debug-info.json`)
.then((result) => {
this.info = result;
@@ -91,15 +120,22 @@ export default class DebugAiModal extends Component {
@action
requestClicked(e) {
this.activeTab = "request";
+ this.showRawResponse = false;
e.preventDefault();
}
@action
responseClicked(e) {
this.activeTab = "response";
+ this.showRawResponse = false;
e.preventDefault();
}
+ @action
+ toggleResponseView() {
+ this.showRawResponse = !this.showRawResponse;
+ }
+
get formattedDurationSummary() {
const durationSeconds = this.seconds(this.info?.duration_msecs);
const firstTokenSeconds = this.seconds(
@@ -264,22 +300,28 @@ export default class DebugAiModal extends Component {
</p>
{{/if}}
</div>
- <AiPayloadViewer
- class="ai-debug-modal__preview"
- @payload={{this.activePayload}}
- @copyLabel={{this.activeCopyLabel}}
- @onCopy={{this.copied}}
- @emptyMessage={{i18n
- "discourse_ai.ai_bot.debug_ai_modal.payload_unavailable"
- }}
- />
+ {{#if this.showDecodedResponse}}
+ <AiDecodedTranscript
+ class="ai-debug-modal__preview"
+ @response={{this.info.decoded_response}}
+ />
+ {{else}}
+ <AiPayloadViewer
+ class="ai-debug-modal__preview"
+ @payload={{this.activePayload}}
+ @unbounded={{true}}
+ @emptyMessage={{i18n
+ "discourse_ai.ai_bot.debug_ai_modal.payload_unavailable"
+ }}
+ />
+ {{/if}}
{{/if}}
</:body>
<:footer>
{{#if this.info.prev_log_id}}
<DButton
- class="btn"
+ class="btn ai-debug-modal__previous"
@icon="angles-left"
@action={{this.prevLog}}
@label="discourse_ai.ai_bot.debug_ai_modal.previous_log"
@@ -287,13 +329,36 @@ export default class DebugAiModal extends Component {
{{/if}}
{{#if this.info.next_log_id}}
<DButton
- class="btn"
+ class="btn ai-debug-modal__next"
@icon="angles-right"
@action={{this.nextLog}}
@label="discourse_ai.ai_bot.debug_ai_modal.next_log"
/>
{{/if}}
- <span class="ai-debug-modal__just-copied">{{this.justCopiedText}}</span>
+ <div class="ai-debug-modal__copy-actions">
+ {{#if this.showResponseToggle}}
+ <DButton
+ class="btn-default ai-debug-modal__response-toggle"
+ @action={{this.toggleResponseView}}
+ @translatedLabel={{this.responseToggleLabel}}
+ />
+ {{/if}}
+ {{#if this.copyValue}}
+ <DCopyButton
+ @value={{this.copyValue}}
+ @copyClass="btn-default ai-debug-modal__copy"
+ @translatedLabel={{this.activeCopyLabel}}
+ @translatedLabelAfterCopy={{i18n "discourse_ai.copied"}}
+ />
+ {{else}}
+ <DButton
+ class="btn-default ai-debug-modal__copy"
+ @icon="copy"
+ @translatedLabel={{this.activeCopyLabel}}
+ @disabled={{true}}
+ />
+ {{/if}}
+ </div>
</:footer>
</DModal>
</template>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/decoded-response.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/decoded-response.js
new file mode 100644
index 00000000000..1218adf2366
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/decoded-response.js
@@ -0,0 +1,50 @@
+export function isValidDecodedToolCall(toolCall) {
+ return Boolean(
+ toolCall &&
+ typeof toolCall === "object" &&
+ !Array.isArray(toolCall) &&
+ typeof toolCall.name === "string" &&
+ Object.hasOwn(toolCall, "arguments")
+ );
+}
+
+export function isValidDecodedToolResult(toolResult) {
+ return Boolean(
+ toolResult &&
+ typeof toolResult === "object" &&
+ !Array.isArray(toolResult) &&
+ Object.hasOwn(toolResult, "result")
+ );
+}
+
+export function isDecodedResponse(value) {
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
+ return false;
+ }
+
+ const hasThinking =
+ typeof value.thinking === "string" && value.thinking.length > 0;
+ const hasResponse =
+ typeof value.response === "string" && value.response.length > 0;
+ const hasToolCalls =
+ Array.isArray(value.tool_calls) &&
+ value.tool_calls.some(isValidDecodedToolCall);
+ const hasToolResults =
+ Array.isArray(value.tool_results) &&
+ value.tool_results.some(isValidDecodedToolResult);
+
+ return hasThinking || hasResponse || hasToolCalls || hasToolResults;
+}
+
+export function decodedResponseText(decodedResponse) {
+ if (!isDecodedResponse(decodedResponse)) {
+ return null;
+ }
+
+ const keys = Object.keys(decodedResponse);
+ if (keys.length === 1 && keys[0] === "response") {
+ return decodedResponse.response;
+ }
+
+ return JSON.stringify(decodedResponse, null, 2);
+}
diff --git a/plugins/discourse-ai/assets/stylesheets/admin/ai-logs.scss b/plugins/discourse-ai/assets/stylesheets/admin/ai-logs.scss
index 1f72ff7b31a..623e3f1e724 100644
--- a/plugins/discourse-ai/assets/stylesheets/admin/ai-logs.scss
+++ b/plugins/discourse-ai/assets/stylesheets/admin/ai-logs.scss
@@ -322,6 +322,7 @@
.d-modal.ai-log-detail-modal {
--modal-max-width: min(96vw, 75rem);
+ overflow: hidden;
}
.ai-log-detail-modal {
@@ -349,13 +350,31 @@
}
}
- &__tabs {
+ &__tabs,
+ &__tab-list,
+ &__actions {
display: flex;
flex-wrap: wrap;
gap: var(--space-1);
+ align-items: center;
+ }
+
+ &__tabs {
margin-bottom: var(--space-3);
}
+ &__actions {
+ justify-content: flex-end;
+ max-width: 100%;
+ margin-inline-start: auto;
+
+ .btn {
+ max-width: 100%;
+ white-space: normal;
+ overflow-wrap: anywhere;
+ }
+ }
+
&__attempts {
margin: 0;
padding-inline-start: var(--space-6);
diff --git a/plugins/discourse-ai/assets/stylesheets/common/ai-decoded-transcript.scss b/plugins/discourse-ai/assets/stylesheets/common/ai-decoded-transcript.scss
new file mode 100644
index 00000000000..ca8718acfa4
--- /dev/null
+++ b/plugins/discourse-ai/assets/stylesheets/common/ai-decoded-transcript.scss
@@ -0,0 +1,110 @@
+.ai-decoded-transcript {
+ display: flex;
+ flex-direction: column;
+ gap: var(--space-4);
+
+ &__thinking,
+ &__section {
+ border: 1px solid var(--content-border-color);
+ border-radius: var(--d-border-radius);
+ background: var(--primary-very-low);
+ }
+
+ &__summary,
+ &__heading {
+ font-size: var(--font-up-1);
+ font-weight: 600;
+ }
+
+ &__summary {
+ padding: var(--space-3);
+ cursor: pointer;
+ }
+
+ &__heading {
+ margin: 0;
+ padding: var(--space-3);
+ border-bottom: 1px solid var(--content-border-color);
+ }
+
+ &__text {
+ padding: 0 var(--space-3) var(--space-3);
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+ }
+
+ &__section > &__text {
+ padding-top: var(--space-3);
+ }
+
+ &__list {
+ display: flex;
+ flex-direction: column;
+ gap: var(--space-3);
+ margin: 0;
+ padding: var(--space-3);
+ list-style: none;
+ }
+
+ &__item {
+ padding: var(--space-3);
+ border: 1px solid var(--content-border-color);
+ border-radius: var(--d-border-radius);
+ background: var(--secondary);
+
+ &.--error {
+ border-color: var(--danger);
+ }
+ }
+
+ &__item-heading,
+ &__label {
+ font-size: var(--font-0);
+ }
+
+ &__item-heading {
+ margin: 0 0 var(--space-2);
+ }
+
+ &__label {
+ margin: var(--space-3) 0 var(--space-2);
+ }
+
+ &__metadata {
+ display: grid;
+ grid-template-columns: max-content minmax(0, 1fr);
+ gap: var(--space-1) var(--space-2);
+ margin: var(--space-1) 0;
+
+ dt {
+ color: var(--primary-high);
+ font-weight: 600;
+ }
+
+ dd {
+ min-width: 0;
+ margin: 0;
+ overflow-wrap: anywhere;
+ }
+ }
+
+ &__code {
+ margin: 0;
+ padding: var(--space-3);
+ overflow: visible;
+ border: 1px solid var(--content-border-color);
+ border-radius: var(--d-border-radius);
+ background: var(--primary-very-low);
+ color: var(--primary);
+ font-family: var(--d-font-family--monospace);
+ font-size: var(--font-down-1);
+ line-height: var(--line-height-medium);
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+ }
+
+ &__error {
+ color: var(--danger);
+ font-size: var(--font-down-1);
+ }
+}
diff --git a/plugins/discourse-ai/assets/stylesheets/common/ai-payload-viewer.scss b/plugins/discourse-ai/assets/stylesheets/common/ai-payload-viewer.scss
index 24fca891411..b86326760bf 100644
--- a/plugins/discourse-ai/assets/stylesheets/common/ai-payload-viewer.scss
+++ b/plugins/discourse-ai/assets/stylesheets/common/ai-payload-viewer.scss
@@ -23,6 +23,20 @@
outline: 2px solid var(--tertiary);
outline-offset: 2px;
}
+
+ > code {
+ max-height: none;
+ padding: 0;
+ overflow: visible;
+ background: none;
+ color: inherit;
+ font: inherit;
+ }
+ }
+
+ &.--unbounded &__content {
+ max-height: none;
+ overflow: visible;
}
&__empty {
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss
index 768c2e578cf..8c45c9611fa 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss
@@ -140,6 +140,7 @@ span.onebox-ai-llm-title {
.d-modal.ai-debug-modal {
--modal-max-width: 99%;
+ overflow: hidden;
ul {
padding-left: 1em;
@@ -175,11 +176,22 @@ span.onebox-ai-llm-title {
}
}
-.ai-debug-modal__just-copied {
- font-size: var(--font-down-1);
- color: var(--success);
- margin-left: auto;
- align-self: center;
+.ai-debug-modal__copy-actions {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ max-width: 100%;
+ gap: var(--space-2);
+ margin-inline-start: auto;
+
+ .btn {
+ max-width: 100%;
+ white-space: normal;
+ }
+
+ .d-button-label {
+ overflow-wrap: anywhere;
+ }
}
.d-modal ul.ai-debug-modal__nav {
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index e6a48c8d56a..8109d10bf10 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -232,6 +232,20 @@ en:
discourse_ai:
title: "AI"
+ copied: "Copied"
+ view_raw: "View raw"
+ view_decoded: "View decoded"
+ ai_decoded_transcript:
+ thinking: "Thinking"
+ tool_calls: "Tool calls"
+ tool_results: "Tool results"
+ tool: "Tool"
+ arguments: "Arguments"
+ tool_result: "Tool result"
+ call_id: "Call ID"
+ type: "Type"
+ error: "Error"
+ response: "Response"
post_image_captions:
edit: "Edit AI image caption"
title: "Edit AI image caption"
@@ -724,6 +738,7 @@ en:
tabs_label: "AI log payload sections"
copy_request: "Copy request"
copy_response: "Copy response"
+ copy_raw_response: "Copy raw response"
copy_context: "Copy context"
payload_unavailable: "Payload not available"
payload_truncated: "This payload is too large to display completely and has been truncated."
diff --git a/plugins/discourse-ai/lib/ai_api_audit_log_response_decoder.rb b/plugins/discourse-ai/lib/ai_api_audit_log_response_decoder.rb
new file mode 100644
index 00000000000..808d88be334
--- /dev/null
+++ b/plugins/discourse-ai/lib/ai_api_audit_log_response_decoder.rb
@@ -0,0 +1,272 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ class AiApiAuditLogResponseDecoder
+ MAX_INPUT_BYTES = 1.megabyte
+ MAX_EVENTS = 10_000
+
+ def self.decode(raw_response_payload, truncated: false)
+ new(raw_response_payload, truncated:).decode
+ end
+
+ def initialize(raw_response_payload, truncated:)
+ @raw_response_payload = raw_response_payload
+ @truncated = truncated
+ @stream_terminated = false
+ @reasoning_fragments = []
+ @answer_fragments = []
+ end
+
+ def decode
+ return if invalid_input?
+
+ events = parse_events
+ return if events.nil?
+
+ details =
+ DiscourseAi::AiApiAuditLogStreamDetailsExtractor.extract(
+ events,
+ stream_terminated: @stream_terminated,
+ )
+ return if details.invalid?
+
+ events.each { |event| extract_fragments(event) }
+
+ {
+ "thinking" => @reasoning_fragments.join.presence,
+ "tool_calls" => details.tool_calls.presence,
+ "tool_results" => details.tool_results.presence,
+ "response" => @answer_fragments.join.presence,
+ }.compact.presence
+ rescue JSON::ParserError, TypeError, ArgumentError
+ nil
+ end
+
+ private
+
+ def invalid_input?
+ @truncated || !@raw_response_payload.is_a?(String) || @raw_response_payload.blank? ||
+ !@raw_response_payload.valid_encoding? || @raw_response_payload.bytesize > MAX_INPUT_BYTES
+ end
+
+ def parse_events
+ if sse_transport?
+ parse_sse
+ else
+ parse_newline_delimited_json
+ end
+ end
+
+ def sse_transport?
+ @raw_response_payload.each_line.any? do |line|
+ line.match?(/\A(?:data|event):/) || line.start_with?(":")
+ end
+ end
+
+ def parse_sse
+ events = []
+ record_lines = []
+ done = false
+ valid = true
+
+ @raw_response_payload.each_line do |line|
+ line = line.chomp
+
+ if line.empty?
+ parsed_record = parse_sse_record(record_lines)
+ if parsed_record == :invalid
+ valid = false
+ break
+ end
+
+ if parsed_record == :done
+ done = true
+ elsif parsed_record
+ if done || events.length >= MAX_EVENTS
+ valid = false
+ break
+ end
+
+ events << parsed_record
+ end
+ record_lines = []
+ else
+ record_lines << line
+ end
+ end
+
+ return if !valid || record_lines.present?
+
+ @stream_terminated = done
+ return if events.empty?
+
+ events
+ end
+
+ def parse_sse_record(lines)
+ return if lines.empty?
+
+ data_lines = []
+ lines.each do |line|
+ next if line.start_with?(":")
+
+ field, value = line.split(":", 2)
+ return :invalid if value.nil? || !%w[data event].include?(field)
+
+ value = value.delete_prefix(" ")
+ data_lines << value if field == "data"
+ end
+
+ return if data_lines.empty?
+
+ data = data_lines.join("\n")
+ return :done if data.strip == "[DONE]"
+
+ parsed = JSON.parse(data)
+ parsed.is_a?(Hash) ? parsed : :invalid
+ end
+
+ def parse_newline_delimited_json
+ events = []
+ valid = true
+
+ @raw_response_payload.each_line do |line|
+ line = line.chomp
+ next if line.blank?
+
+ if events.length >= MAX_EVENTS
+ valid = false
+ break
+ end
+
+ event = JSON.parse(line)
+ if !event.is_a?(Hash)
+ valid = false
+ break
+ end
+
+ events << event
+ end
+
+ return if !valid || events.length < 2
+
+ events
+ end
+
+ def extract_fragments(event)
+ extract_open_ai_chat(event)
+ extract_open_ai_response(event)
+ extract_anthropic(event)
+ extract_gemini(event)
+ extract_gemini_interaction(event)
+ extract_ollama(event)
+ extract_cohere(event)
+ end
+
+ def extract_open_ai_chat(event)
+ choices = event["choices"]
+ return if !choices.is_a?(Array)
+
+ choices.each do |choice|
+ delta = choice.is_a?(Hash) ? choice["delta"] : nil
+ next if !delta.is_a?(Hash)
+
+ reasoning =
+ if delta["reasoning"].is_a?(String)
+ delta["reasoning"]
+ elsif delta["reasoning_content"].is_a?(String)
+ delta["reasoning_content"]
+ end
+ append(@reasoning_fragments, reasoning)
+ append(@answer_fragments, delta["content"])
+ end
+ end
+
+ def extract_open_ai_response(event)
+ case event["type"]
+ when "response.reasoning_summary_text.delta"
+ append(@reasoning_fragments, event["delta"])
+ when "response.output_text.delta"
+ append(@answer_fragments, event["delta"])
+ end
+ end
+
+ def extract_anthropic(event)
+ case event["type"]
+ when "content_block_start"
+ content_block = event["content_block"]
+ return if !content_block.is_a?(Hash)
+
+ case content_block["type"]
+ when "thinking"
+ append(@reasoning_fragments, content_block["thinking"])
+ when "text"
+ append(@answer_fragments, content_block["text"])
+ end
+ when "content_block_delta"
+ delta = event["delta"]
+ return if !delta.is_a?(Hash)
+
+ case delta["type"]
+ when "thinking_delta"
+ append(@reasoning_fragments, delta["thinking"])
+ when "text_delta"
+ append(@answer_fragments, delta["text"])
+ end
+ end
+ end
+
+ def extract_gemini(event)
+ candidates = event["candidates"]
+ return if !candidates.is_a?(Array)
+
+ candidates.each do |candidate|
+ parts = candidate.is_a?(Hash) ? candidate.dig("content", "parts") : nil
+ next if !parts.is_a?(Array)
+
+ parts.each do |part|
+ next if !part.is_a?(Hash)
+
+ fragments = part["thought"] == true ? @reasoning_fragments : @answer_fragments
+ append(fragments, part["text"])
+ end
+ end
+ end
+
+ def extract_gemini_interaction(event)
+ return if event["event_type"] != "step.delta"
+
+ delta = event["delta"]
+ return if !delta.is_a?(Hash)
+
+ case delta["type"]
+ when "thought_summary"
+ content = delta["content"]
+ append(@reasoning_fragments, content["text"]) if content.is_a?(Hash)
+ when "text"
+ append(@answer_fragments, delta["text"])
+ end
+ end
+
+ def extract_ollama(event)
+ message = event["message"]
+ return if !message.is_a?(Hash)
+
+ append(@reasoning_fragments, message["thinking"])
+ append(@answer_fragments, message["content"])
+ end
+
+ def extract_cohere(event)
+ case event["event_type"]
+ when "text-generation"
+ append(@answer_fragments, event["text"])
+ when "tool-calls-generation"
+ append(@reasoning_fragments, event["text"])
+ end
+ end
+
+ def append(fragments, value)
+ fragments << value if value.is_a?(String) && !value.empty?
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/ai_api_audit_log_stream_details_extractor.rb b/plugins/discourse-ai/lib/ai_api_audit_log_stream_details_extractor.rb
new file mode 100644
index 00000000000..80cd5e99939
--- /dev/null
+++ b/plugins/discourse-ai/lib/ai_api_audit_log_stream_details_extractor.rb
@@ -0,0 +1,469 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ class AiApiAuditLogStreamDetailsExtractor
+ NATIVE_INTERACTION_CALL_TYPES = %w[
+ code_execution_call
+ file_search_call
+ google_maps_call
+ google_search_call
+ url_context_call
+ ].freeze
+ NATIVE_INTERACTION_RESULT_TYPES = %w[
+ code_execution_result
+ file_search_result
+ google_maps_result
+ google_search_result
+ url_context_result
+ ].freeze
+
+ Result =
+ Struct.new(:tool_calls, :tool_results, :invalid, keyword_init: true) do
+ def invalid?
+ invalid
+ end
+ end
+
+ def self.extract(events, stream_terminated: false)
+ new(events, stream_terminated:).extract
+ end
+
+ def initialize(events, stream_terminated:)
+ @events = events
+ @stream_terminated = stream_terminated
+ @tool_calls = []
+ @tool_results = []
+ @open_ai_chat_calls = {}
+ @open_ai_response_calls = {}
+ @open_ai_native_items = {}
+ @anthropic_blocks = {}
+ @interaction_steps = {}
+ @gemini_calls = {}
+ @invalid = false
+ end
+
+ def extract
+ @events.each do |event|
+ extract_open_ai_chat(event)
+ extract_open_ai_response(event)
+ extract_anthropic(event)
+ extract_gemini(event)
+ extract_gemini_interaction(event)
+ extract_ollama(event)
+ extract_cohere(event)
+ end
+
+ finalize_open_ai_chat
+ finalize_open_ai_responses
+ finalize_anthropic
+ finalize_gemini
+ finalize_gemini_interactions
+ finalize_open_ai_native_items
+
+ Result.new(tool_calls: @tool_calls, tool_results: @tool_results, invalid: @invalid)
+ rescue JSON::ParserError, TypeError, ArgumentError
+ Result.new(tool_calls: [], tool_results: [], invalid: true)
+ end
+
+ private
+
+ def extract_open_ai_chat(event)
+ choices = event["choices"]
+ return if !choices.is_a?(Array)
+
+ choices.each_with_index do |choice, choice_position|
+ next if !choice.is_a?(Hash)
+
+ @open_ai_chat_complete = true if choice["finish_reason"].present?
+ tool_deltas = choice.dig("delta", "tool_calls")
+ next if tool_deltas.nil?
+
+ if !tool_deltas.is_a?(Array)
+ @invalid = true
+ next
+ end
+
+ choice_key = choice["index"] || choice_position
+ tool_deltas.each_with_index do |tool_delta, tool_position|
+ if !tool_delta.is_a?(Hash)
+ @invalid = true
+ next
+ end
+
+ key = open_ai_chat_key(choice_key, tool_delta, tool_position)
+ call = (@open_ai_chat_calls[key] ||= empty_fragmented_call)
+ assign_consistent(call, "id", tool_delta["id"])
+
+ function = tool_delta["function"]
+ if function.nil?
+ next
+ elsif !function.is_a?(Hash)
+ @invalid = true
+ next
+ end
+
+ assign_consistent(call, "name", function["name"])
+ append_arguments(call, function["arguments"])
+ end
+ end
+ end
+
+ def open_ai_chat_key(choice_key, tool_delta, tool_position)
+ tool_key = tool_delta["index"] || tool_delta["id"]
+ if tool_key.nil?
+ existing_keys = @open_ai_chat_calls.keys.select { |key| key.first == choice_key }
+ tool_key = existing_keys.one? ? existing_keys.first.last : tool_position
+ end
+ [choice_key, tool_key]
+ end
+
+ def extract_open_ai_response(event)
+ case event["type"]
+ when "response.output_item.added"
+ capture_open_ai_response_item(event["item"], complete: false)
+ when "response.function_call_arguments.delta"
+ call = @open_ai_response_calls[event["item_id"]]
+ if call
+ append_arguments(call, event["delta"])
+ else
+ @invalid = true
+ end
+ when "response.function_call_arguments.done"
+ capture_open_ai_response_snapshot(event["item_id"], event["arguments"])
+ when "response.output_item.done"
+ capture_open_ai_response_item(event["item"], complete: true)
+ when "response.completed"
+ output = event.dig("response", "output")
+ return if output.nil?
+
+ if output.is_a?(Array)
+ output.each { |item| capture_open_ai_response_item(item, complete: true) }
+ else
+ @invalid = true
+ end
+ end
+ end
+
+ def capture_open_ai_response_item(item, complete:)
+ return if !item.is_a?(Hash)
+
+ type = item["type"]
+ if type == "function_call"
+ item_id = item["id"]
+ if !item_id.is_a?(String)
+ @invalid = true
+ return
+ end
+
+ call = (@open_ai_response_calls[item_id] ||= empty_fragmented_call)
+ assign_consistent(call, "id", item["call_id"] || item_id)
+ assign_consistent(call, "name", item["name"])
+ arguments = item["arguments"]
+ if complete
+ capture_open_ai_response_snapshot(item_id, arguments)
+ call[:complete] = true
+ elsif arguments.present?
+ append_arguments(call, arguments)
+ end
+ elsif type&.end_with?("_call")
+ @open_ai_native_items[item["id"] || [type, @open_ai_native_items.length]] = item
+ elsif type&.end_with?("_result")
+ append_tool_result(item)
+ end
+ end
+
+ def capture_open_ai_response_snapshot(item_id, arguments)
+ call = @open_ai_response_calls[item_id]
+ if !call || !arguments.is_a?(String)
+ @invalid = true
+ return
+ end
+
+ if call[:snapshot] && call[:snapshot] != arguments
+ @invalid = true
+ else
+ call[:snapshot] = arguments
+ end
+ end
+
+ def extract_anthropic(event)
+ index = event["index"]
+ case event["type"]
+ when "content_block_start"
+ block = event["content_block"]
+ return if !block.is_a?(Hash)
+
+ case block["type"]
+ when "tool_use", "server_tool_use"
+ @anthropic_blocks[index] = {
+ type: block["type"],
+ id: block["id"],
+ name: block["name"],
+ initial_arguments: block["input"],
+ argument_fragments: +"",
+ complete: false,
+ }
+ when /_tool_result\z/
+ append_tool_result(block)
+ end
+ when "content_block_delta"
+ return if event.dig("delta", "type") != "input_json_delta"
+
+ block = @anthropic_blocks[index]
+ if block
+ append_arguments(block, event.dig("delta", "partial_json"))
+ else
+ @invalid = true
+ end
+ when "content_block_stop"
+ block = @anthropic_blocks[index]
+ block[:complete] = true if block
+ end
+ end
+
+ def extract_gemini(event)
+ candidates = event["candidates"]
+ return if !candidates.is_a?(Array)
+
+ candidates.each_with_index do |candidate, candidate_position|
+ parts = candidate.is_a?(Hash) ? candidate.dig("content", "parts") : nil
+ next if !parts.is_a?(Array)
+
+ candidate_key = candidate["index"] || candidate_position
+ parts.each_with_index do |part, part_position|
+ function_call = part.is_a?(Hash) ? part["functionCall"] : nil
+ next if function_call.nil?
+
+ if !function_call.is_a?(Hash) || !function_call["name"].is_a?(String) ||
+ !function_call["args"].is_a?(Hash)
+ @invalid = true
+ next
+ end
+
+ @gemini_calls[[candidate_key, part_position]] = {
+ "name" => function_call["name"],
+ "arguments" => function_call["args"],
+ }
+ end
+ end
+ end
+
+ def extract_gemini_interaction(event)
+ index = event["index"]
+ case event["event_type"]
+ when "step.start"
+ step = event["step"]
+ return if !step.is_a?(Hash)
+ return if !interaction_tool_type?(step["type"])
+
+ @interaction_steps[index] = { step: step.dup, argument_fragments: +"", complete: false }
+ when "step.delta"
+ delta = event["delta"]
+ return if !delta.is_a?(Hash) || !interaction_tool_type?(delta["type"])
+
+ state = @interaction_steps[index]
+ if !state
+ @invalid = true
+ elsif %w[arguments arguments_delta].include?(delta["type"])
+ append_arguments(state, delta["arguments"] || delta["partial_arguments"])
+ else
+ state[:step] = deep_merge(state[:step], delta)
+ end
+ when "step.stop"
+ state = @interaction_steps[index]
+ state[:complete] = true if state
+ end
+ end
+
+ def interaction_tool_type?(type)
+ type == "function_call" || %w[arguments arguments_delta].include?(type) ||
+ NATIVE_INTERACTION_CALL_TYPES.include?(type) ||
+ NATIVE_INTERACTION_RESULT_TYPES.include?(type)
+ end
+
+ def extract_ollama(event)
+ tool_calls = event.dig("message", "tool_calls")
+ return if tool_calls.nil?
+
+ if !tool_calls.is_a?(Array)
+ @invalid = true
+ return
+ end
+
+ tool_calls.each do |tool_call|
+ function = tool_call.is_a?(Hash) ? tool_call["function"] : nil
+ if !function.is_a?(Hash) || !function["name"].is_a?(String) ||
+ !function["arguments"].is_a?(Hash)
+ @invalid = true
+ next
+ end
+
+ @tool_calls << { "name" => function["name"], "arguments" => function["arguments"] }
+ end
+ end
+
+ def extract_cohere(event)
+ return if event["event_type"] != "tool-calls-generation"
+
+ tool_calls = event["tool_calls"]
+ return if tool_calls.nil?
+
+ if !tool_calls.is_a?(Array)
+ @invalid = true
+ return
+ end
+
+ tool_calls.each do |tool_call|
+ if !tool_call.is_a?(Hash) || !tool_call["name"].is_a?(String) ||
+ !tool_call["parameters"].is_a?(Hash)
+ @invalid = true
+ next
+ end
+
+ @tool_calls << { "name" => tool_call["name"], "arguments" => tool_call["parameters"] }
+ end
+ end
+
+ def finalize_open_ai_chat
+ if @open_ai_chat_calls.present? && !@open_ai_chat_complete && !@stream_terminated
+ @invalid = true
+ return
+ end
+
+ @open_ai_chat_calls.each_value { |call| append_fragmented_call(call) }
+ end
+
+ def finalize_open_ai_responses
+ @open_ai_response_calls.each_value do |call|
+ if !call[:complete] || !call[:snapshot]
+ @invalid = true
+ next
+ end
+
+ if call[:argument_fragments].present? && call[:argument_fragments] != call[:snapshot]
+ @invalid = true
+ next
+ end
+
+ append_fragmented_call(call, arguments: call[:snapshot])
+ end
+ end
+
+ def finalize_anthropic
+ @anthropic_blocks.each_value do |block|
+ if !block[:complete]
+ @invalid = true
+ next
+ end
+
+ arguments =
+ if block[:argument_fragments].present?
+ parse_arguments(block[:argument_fragments])
+ elsif block[:initial_arguments].is_a?(Hash)
+ block[:initial_arguments]
+ end
+ append_call(block[:id], block[:name], arguments)
+ end
+ end
+
+ def finalize_gemini
+ @tool_calls.concat(@gemini_calls.values)
+ end
+
+ def finalize_gemini_interactions
+ @interaction_steps.each_value do |state|
+ if !state[:complete]
+ @invalid = true
+ next
+ end
+
+ step = state[:step]
+ type = step["type"]
+ if type == "function_call"
+ raw_arguments = step["arguments"].to_s + state[:argument_fragments]
+ append_call(step["id"], step["name"], parse_arguments(raw_arguments))
+ elsif NATIVE_INTERACTION_CALL_TYPES.include?(type)
+ append_call(step["id"], type.delete_suffix("_call"), step["arguments"] || {})
+ elsif NATIVE_INTERACTION_RESULT_TYPES.include?(type)
+ append_tool_result(step)
+ end
+ end
+ end
+
+ def finalize_open_ai_native_items
+ @open_ai_native_items.each_value do |item|
+ append_call(
+ item["id"],
+ item["type"].delete_suffix("_call"),
+ item["arguments"] || item["action"] || {},
+ )
+ end
+ end
+
+ def empty_fragmented_call
+ { "id" => nil, "name" => nil, :argument_fragments => +"" }
+ end
+
+ def append_arguments(call, arguments)
+ return if arguments.nil?
+
+ if arguments.is_a?(String)
+ call[:argument_fragments] << arguments
+ else
+ @invalid = true
+ end
+ end
+
+ def append_fragmented_call(call, arguments: call[:argument_fragments])
+ append_call(call["id"], call["name"], parse_arguments(arguments))
+ end
+
+ def append_call(id, name, arguments)
+ if !name.is_a?(String) || arguments.nil?
+ @invalid = true
+ return
+ end
+
+ @tool_calls << { "id" => id, "name" => name, "arguments" => arguments }.compact
+ end
+
+ def append_tool_result(item)
+ result = item.key?("result") ? item["result"] : item["content"]
+ return if result.nil?
+
+ @tool_results << {
+ "call_id" => item["call_id"] || item["tool_use_id"],
+ "type" => item["type"],
+ "result" => result,
+ "is_error" => item["is_error"],
+ }.compact
+ end
+
+ def parse_arguments(arguments)
+ return arguments if arguments.is_a?(Hash)
+ return if !arguments.is_a?(String) || arguments.empty?
+
+ JSON.parse(arguments)
+ end
+
+ def assign_consistent(call, key, value)
+ return if value.nil?
+
+ if !value.is_a?(String) || (call[key] && call[key] != value)
+ @invalid = true
+ else
+ call[key] = value
+ end
+ end
+
+ def deep_merge(left, right)
+ left.merge(right) do |_key, left_value, right_value|
+ if left_value.is_a?(Hash) && right_value.is_a?(Hash)
+ deep_merge(left_value, right_value)
+ else
+ right_value
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 2b8d5415192..260f27d50b6 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -21,6 +21,7 @@ register_asset "stylesheets/common/ai-blinking-animation.scss"
register_asset "stylesheets/common/ai-user-settings.scss"
register_asset "stylesheets/common/ai-features.scss"
register_asset "stylesheets/common/ai-payload-viewer.scss"
+register_asset "stylesheets/common/ai-decoded-transcript.scss"
register_asset "stylesheets/admin/ai-features-editor.scss", :admin
register_asset "stylesheets/admin/ai-logs.scss", :admin
diff --git a/plugins/discourse-ai/spec/lib/ai_api_audit_log_response_decoder_spec.rb b/plugins/discourse-ai/spec/lib/ai_api_audit_log_response_decoder_spec.rb
new file mode 100644
index 00000000000..68aa33ace0d
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/ai_api_audit_log_response_decoder_spec.rb
@@ -0,0 +1,574 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::AiApiAuditLogResponseDecoder do
+ subject(:decode) { described_class.decode(raw_response, truncated:) }
+
+ let(:truncated) { false }
+
+ def sse(*events)
+ events.map { |event| "data: #{event.is_a?(String) ? event : event.to_json}\n\n" }.join
+ end
+
+ describe ".decode" do
+ context "with OpenAI-compatible events" do
+ let(:raw_response) do
+ sse(
+ { choices: [{ delta: { reasoning: "No" } }] },
+ { choices: [{ delta: { reasoning: " funny" } }] },
+ { choices: [{ delta: { reasoning: " stories" } }] },
+ "[DONE]",
+ )
+ end
+
+ it "assembles the retained VLLM reasoning fragments in order" do
+ expect(decode).to eq("thinking" => "No funny stories")
+ end
+ end
+
+ it "supports CRLF records, comments, event fields, multiline data, and a done sentinel" do
+ raw_response = <<~SSE.gsub("\n", "\r\n")
+ : keepalive
+
+ event: message
+ data: {"choices":[
+ data: {"delta":{"content":"Hello"}}]}
+
+ event: complete
+ data: [DONE]
+
+ SSE
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("response" => "Hello")
+ end
+
+ it "distinguishes structured model output from a decoded transcript" do
+ raw_response = sse({ choices: [{ delta: { content: '{"thinking":"about this"}' } }] })
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("response" => '{"thinking":"about this"}')
+ end
+
+ it "keeps reasoning and answer channels separate" do
+ cases = {
+ sse({ choices: [{ delta: { content: "answer" } }] }) => {
+ "response" => "answer",
+ },
+ sse({ choices: [{ delta: { reasoning_content: "reasoning" } }] }) => {
+ "thinking" => "reasoning",
+ },
+ sse(
+ { choices: [{ delta: { reasoning: "reason" } }] },
+ { choices: [{ delta: { content: "answer" } }] },
+ ) => {
+ "thinking" => "reason",
+ "response" => "answer",
+ },
+ }
+
+ cases.each do |payload, expected|
+ result = described_class.decode(payload)
+ expect(result).to eq(expected)
+ end
+ end
+
+ it "extracts verified readable fields from supported SSE provider events" do
+ cases = {
+ "OpenAI Responses" => [
+ { type: "response.reasoning_summary_text.delta", delta: "reason" },
+ { type: "response.output_text.delta", delta: "answer" },
+ { "thinking" => "reason", "response" => "answer" },
+ ],
+ "Anthropic" => [
+ { type: "content_block_delta", delta: { type: "thinking_delta", thinking: "reason" } },
+ { type: "content_block_delta", delta: { type: "text_delta", text: "answer" } },
+ { "thinking" => "reason", "response" => "answer" },
+ ],
+ "Gemini content" => [
+ {
+ candidates: [
+ { content: { parts: [{ thought: true, text: "reason" }, { text: "answer" }] } },
+ ],
+ },
+ { usageMetadata: { candidatesTokenCount: 2 } },
+ { "thinking" => "reason", "response" => "answer" },
+ ],
+ "Gemini Interactions" => [
+ {
+ event_type: "step.delta",
+ delta: {
+ type: "thought_summary",
+ content: {
+ text: "reason",
+ },
+ },
+ },
+ { event_type: "step.delta", delta: { type: "text", text: "answer" } },
+ { "thinking" => "reason", "response" => "answer" },
+ ],
+ }
+
+ cases.each do |provider, (first_event, second_event, expected)|
+ result = described_class.decode(sse(first_event, second_event, "[DONE]"))
+ expect(result).to eq(expected), provider
+ end
+ end
+
+ it "reconstructs fragmented tool calls from supported SSE provider events" do
+ cases = {
+ "OpenAI-compatible" => [
+ {
+ choices: [
+ {
+ index: 0,
+ delta: {
+ tool_calls: [
+ { index: 0, id: "call-1", function: { name: "search", arguments: '{"q":"' } },
+ ],
+ },
+ },
+ ],
+ },
+ {
+ choices: [
+ {
+ index: 0,
+ delta: {
+ tool_calls: [{ index: 0, function: { arguments: 'docs"}' } }],
+ },
+ finish_reason: "tool_calls",
+ },
+ ],
+ },
+ { "id" => "call-1", "name" => "search", "arguments" => { "q" => "docs" } },
+ ],
+ "Anthropic" => [
+ {
+ type: "content_block_start",
+ index: 0,
+ content_block: {
+ type: "tool_use",
+ id: "tool-1",
+ name: "search",
+ input: {
+ },
+ },
+ },
+ {
+ type: "content_block_delta",
+ index: 0,
+ delta: {
+ type: "input_json_delta",
+ partial_json: '{"q":"docs"}',
+ },
+ },
+ { type: "content_block_stop", index: 0 },
+ { "id" => "tool-1", "name" => "search", "arguments" => { "q" => "docs" } },
+ ],
+ "Gemini Interactions" => [
+ {
+ event_type: "step.start",
+ index: 0,
+ step: {
+ type: "function_call",
+ id: "call-1",
+ name: "search",
+ arguments: '{"q":"',
+ },
+ },
+ {
+ event_type: "step.delta",
+ index: 0,
+ delta: {
+ type: "arguments",
+ partial_arguments: 'docs"}',
+ },
+ },
+ { event_type: "step.stop", index: 0 },
+ { "id" => "call-1", "name" => "search", "arguments" => { "q" => "docs" } },
+ ],
+ }
+
+ cases.each do |provider, entries|
+ expected = entries.pop
+ result = described_class.decode(sse(*entries))
+ expect(result).to eq("tool_calls" => [expected]), provider
+ end
+ end
+
+ it "deduplicates complete OpenAI Responses tool-call snapshots" do
+ item = {
+ id: "item-1",
+ type: "function_call",
+ call_id: "call-1",
+ name: "search",
+ arguments: '{"q":"docs"}',
+ }
+ raw_response =
+ sse(
+ { type: "response.output_item.added", item: item.merge(arguments: "") },
+ { type: "response.function_call_arguments.delta", item_id: "item-1", delta: '{"q":' },
+ { type: "response.function_call_arguments.delta", item_id: "item-1", delta: '"docs"}' },
+ {
+ type: "response.function_call_arguments.done",
+ item_id: "item-1",
+ arguments: item[:arguments],
+ },
+ { type: "response.output_item.done", item: item },
+ { type: "response.completed", response: { output: [item] } },
+ )
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq(
+ "tool_calls" => [
+ { "id" => "call-1", "name" => "search", "arguments" => { "q" => "docs" } },
+ ],
+ )
+ end
+
+ it "extracts complete Gemini, Cohere, and Ollama tool calls" do
+ payloads = {
+ sse(
+ {
+ candidates: [
+ { content: { parts: [{ functionCall: { name: "search", args: { q: "docs" } } }] } },
+ ],
+ },
+ ) => {
+ "name" => "search",
+ "arguments" => {
+ "q" => "docs",
+ },
+ },
+ [
+ { event_type: "stream-start" },
+ {
+ event_type: "tool-calls-generation",
+ tool_calls: [{ name: "search", parameters: { q: "docs" } }],
+ },
+ { event_type: "stream-end" },
+ ].map(&:to_json).join("\n") => {
+ "name" => "search",
+ "arguments" => {
+ "q" => "docs",
+ },
+ },
+ [
+ {
+ message: {
+ tool_calls: [{ function: { name: "search", arguments: { q: "docs" } } }],
+ },
+ done: false,
+ },
+ { message: { content: "" }, done: true },
+ ].map(&:to_json).join("\n") => {
+ "name" => "search",
+ "arguments" => {
+ "q" => "docs",
+ },
+ },
+ }
+
+ payloads.each do |payload, expected|
+ result = described_class.decode(payload)
+ expect(result).to eq("tool_calls" => [expected])
+ end
+ end
+
+ it "includes thinking, server tool calls, results, and the final response" do
+ raw_response =
+ sse(
+ {
+ event_type: "step.delta",
+ delta: {
+ type: "thought_summary",
+ content: {
+ text: "Check it",
+ },
+ },
+ },
+ {
+ event_type: "step.start",
+ index: 0,
+ step: {
+ type: "code_execution_call",
+ id: "code-1",
+ },
+ },
+ {
+ event_type: "step.delta",
+ index: 0,
+ delta: {
+ type: "code_execution_call",
+ arguments: {
+ language: "python",
+ code: "print(42)",
+ },
+ },
+ },
+ { event_type: "step.stop", index: 0 },
+ {
+ event_type: "step.start",
+ index: 1,
+ step: {
+ type: "code_execution_result",
+ call_id: "code-1",
+ },
+ },
+ {
+ event_type: "step.delta",
+ index: 1,
+ delta: {
+ type: "code_execution_result",
+ result: "42\n",
+ is_error: false,
+ },
+ },
+ { event_type: "step.stop", index: 1 },
+ { event_type: "step.delta", delta: { type: "text", text: "42" } },
+ )
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq(
+ "thinking" => "Check it",
+ "tool_calls" => [
+ {
+ "id" => "code-1",
+ "name" => "code_execution",
+ "arguments" => {
+ "language" => "python",
+ "code" => "print(42)",
+ },
+ },
+ ],
+ "tool_results" => [
+ {
+ "call_id" => "code-1",
+ "type" => "code_execution_result",
+ "result" => "42\n",
+ "is_error" => false,
+ },
+ ],
+ "response" => "42",
+ )
+ end
+
+ it "falls back atomically for incomplete or conflicting tool calls" do
+ incomplete =
+ sse(
+ {
+ type: "content_block_start",
+ index: 0,
+ content_block: {
+ type: "tool_use",
+ id: "tool-1",
+ name: "search",
+ input: {
+ },
+ },
+ },
+ {
+ type: "content_block_delta",
+ index: 0,
+ delta: {
+ type: "input_json_delta",
+ partial_json: '{"q":',
+ },
+ },
+ )
+ conflicting =
+ sse(
+ {
+ type: "response.output_item.added",
+ item: {
+ id: "item-1",
+ type: "function_call",
+ call_id: "call-1",
+ name: "search",
+ arguments: "",
+ },
+ },
+ { type: "response.function_call_arguments.delta", item_id: "item-1", delta: "{}" },
+ {
+ type: "response.output_item.done",
+ item: {
+ id: "item-1",
+ type: "function_call",
+ call_id: "call-1",
+ name: "search",
+ arguments: '{"q":"different"}',
+ },
+ },
+ )
+
+ [incomplete, conflicting].each do |payload|
+ result = described_class.decode(payload)
+ expect(result).to be_nil
+ end
+ end
+
+ it "extracts Anthropic content block initial text" do
+ raw_response =
+ sse(
+ { type: "content_block_start", content_block: { type: "thinking", thinking: "why" } },
+ { type: "content_block_start", content_block: { type: "text", text: "yes" } },
+ )
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("thinking" => "why", "response" => "yes")
+ end
+
+ it "decodes newline-delimited Ollama content" do
+ raw_response = [
+ { message: { role: "assistant", content: "Hello " }, done: false },
+ { message: { role: "assistant", content: "world" }, done: false },
+ { message: { role: "assistant", content: "" }, done: true },
+ ].map(&:to_json).join("\n")
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("response" => "Hello world")
+ end
+
+ it "decodes Ollama thinking separately from content" do
+ raw_response = [
+ { message: { thinking: "Check", content: "" }, done: false },
+ { message: { thinking: " first", content: "Answer" }, done: false },
+ { message: { thinking: "", content: "" }, done: true },
+ ].map(&:to_json).join("\n")
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("thinking" => "Check first", "response" => "Answer")
+ end
+
+ it "separates Cohere tool rationale from generated response text" do
+ raw_response = [
+ { event_type: "stream-start", text: "metadata" },
+ { event_type: "tool-calls-generation", text: "I will search" },
+ { event_type: "text-generation", text: "Hello" },
+ { event_type: "stream-end", response: { text: "duplicate" } },
+ ].map(&:to_json).join("\n")
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("thinking" => "I will search", "response" => "Hello")
+ end
+
+ it "preserves JSON escapes, Unicode, and fragment whitespace" do
+ raw_response =
+ sse(
+ { choices: [{ delta: { content: "A quoted \"word\"" } }] },
+ { choices: [{ delta: { content: " and café ☕" } }] },
+ )
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to eq("response" => "A quoted \"word\" and café ☕")
+ end
+
+ it "falls back for non-streaming JSON, plain text, nil, and blank input" do
+ payloads = ['{"choices":[{"message":{"content":"hello"}}]}', "server error", nil, " \n"]
+
+ payloads.each do |payload|
+ result = described_class.decode(payload)
+ expect(result).to be_nil
+ end
+ end
+
+ it "falls back when a stream contains no known readable text" do
+ payloads = [
+ sse({ unknown: { text: "not supported" } }, "[DONE]"),
+ sse({ choices: [{ delta: { tool_calls: [{ function: { arguments: "secret" } }] } }] }),
+ sse({ type: "response.completed", response: { usage: { output_tokens: 10 } } }),
+ ]
+
+ payloads.each do |payload|
+ result = described_class.decode(payload)
+ expect(result).to be_nil
+ end
+ end
+
+ it "atomically falls back for malformed JSON in the middle or at the end" do
+ valid_event = { choices: [{ delta: { content: "partial" } }] }.to_json
+ payloads = [
+ "data: #{valid_event}\n\ndata: {broken}\n\ndata: #{valid_event}\n\n",
+ "data: #{valid_event}\n\ndata: {\"choices\":[",
+ "#{valid_event}\n{broken}\n#{valid_event}\n",
+ ]
+
+ payloads.each do |payload|
+ result = described_class.decode(payload)
+ expect(result).to be_nil
+ end
+ end
+
+ it "falls back for a retry buffer with an error followed by a valid stream" do
+ raw_response = "upstream error\n" + sse({ choices: [{ delta: { content: "answer" } }] })
+
+ result = described_class.decode(raw_response)
+
+ expect(result).to be_nil
+ end
+
+ it "returns nil for invalidly encoded binary input" do
+ raw_response = "\x00\xFF\x01".dup.force_encoding(Encoding::UTF_8)
+
+ result = nil
+ expect { result = described_class.decode(raw_response) }.not_to raise_error
+ expect(result).to be_nil
+ end
+
+ it "returns nil when the caller marks the payload as truncated" do
+ raw_response = sse({ choices: [{ delta: { content: "partial" } }] })
+ result = described_class.decode(raw_response, truncated: true)
+
+ expect(result).to be_nil
+ end
+
+ it "accepts exactly the input-size limit and rejects larger payloads" do
+ prefix = 'data: {"choices":[{"delta":{"content":"'
+ suffix = "\"}}]}\n\n"
+ fragment = "a" * (described_class::MAX_INPUT_BYTES - prefix.bytesize - suffix.bytesize)
+ at_limit = prefix + fragment + suffix
+ over_limit = at_limit.sub(fragment, "#{fragment}a")
+
+ expect(at_limit.bytesize).to eq(described_class::MAX_INPUT_BYTES)
+ expect(described_class.decode(at_limit)).to eq("response" => fragment)
+ expect(described_class.decode(over_limit)).to be_nil
+ end
+
+ it "accepts the event-count limit and atomically falls back above it" do
+ readable_event = { choices: [{ delta: { content: "answer" } }] }
+ formats = {
+ "SSE" => ->(events) { sse(*events, "[DONE]") },
+ "NDJSON" => ->(events) { events.map(&:to_json).join("\n") },
+ }
+
+ formats.each do |transport, encode|
+ at_limit_events = [readable_event] + Array.new(described_class::MAX_EVENTS - 1) { {} }
+ over_limit_events = [readable_event] + Array.new(described_class::MAX_EVENTS) { {} }
+ at_limit = encode.call(at_limit_events)
+ over_limit = encode.call(over_limit_events)
+
+ expect(described_class.decode(at_limit)).to eq("response" => "answer"), transport
+ expect(described_class.decode(over_limit)).to be_nil, transport
+ end
+ end
+
+ it "does not mutate the input" do
+ expected_raw_response = sse({ choices: [{ delta: { content: "answer" } }] })
+ raw_response = expected_raw_response.freeze
+
+ result = nil
+ expect { result = described_class.decode(raw_response) }.not_to raise_error
+ expect(result).to eq("response" => "answer")
+ expect(raw_response).to eq(expected_raw_response)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_logs_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_logs_controller_spec.rb
index dd80277e964..80c3e23de40 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_logs_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_logs_controller_spec.rb
@@ -133,6 +133,7 @@ RSpec.describe DiscourseAi::Admin::AiLogsController do
expect(response.parsed_body["logs"].last.keys).not_to include(
"raw_request_payload",
"raw_response_payload",
+ "decoded_response",
"feature_context",
"request_attempts",
)
@@ -318,11 +319,17 @@ RSpec.describe DiscourseAi::Admin::AiLogsController do
describe "GET /admin/plugins/discourse-ai/ai-logs/:id.json" do
it "returns raw details and truncates oversized payloads" do
+ raw_response = <<~SSE
+ data: {"choices":[{"delta":{"content":"decoded answer"}}]}
+
+ data: [DONE]
+
+ SSE
log =
Fabricate(
:ai_api_audit_log,
raw_request_payload: "a" * (1.megabyte + 10),
- raw_response_payload: '{"answer":"ok"}',
+ raw_response_payload: raw_response,
feature_context: {
source: "spec",
},
@@ -340,11 +347,17 @@ RSpec.describe DiscourseAi::Admin::AiLogsController do
"payload_available" => true,
"raw_request_payload_bytes" => 1.megabyte + 10,
"raw_request_payload_truncated" => true,
- "raw_response_payload" => log.raw_response_payload,
+ "raw_response_payload" => raw_response,
+ "raw_response_payload_bytes" => raw_response.bytesize,
+ "raw_response_payload_truncated" => false,
+ "decoded_response" => {
+ "response" => "decoded answer",
+ },
"duration_msecs" => 1_400,
"time_to_first_token_msecs" => 320,
"spending" => 0.0,
)
+ expect(response.parsed_body).not_to have_key("has_decoded_response")
expect(response.parsed_body["raw_request_payload"].bytesize).to eq(1.megabyte)
end
@@ -362,6 +375,23 @@ RSpec.describe DiscourseAi::Admin::AiLogsController do
expect(response.parsed_body["raw_request_payload_truncated"]).to eq(true)
end
+ it "falls back to an oversized response projection without partially decoding it" do
+ complete_event = "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n"
+ raw_response = complete_event + (" " * 1.megabyte)
+ log = Fabricate(:ai_api_audit_log, raw_response_payload: raw_response)
+
+ get "/admin/plugins/discourse-ai/ai-logs/#{log.id}.json"
+
+ returned_payload = response.parsed_body["raw_response_payload"]
+ expect(response.status).to eq(200)
+ expect(response.parsed_body).to include(
+ "raw_response_payload_bytes" => raw_response.bytesize,
+ "raw_response_payload_truncated" => true,
+ "decoded_response" => nil,
+ )
+ expect(returned_payload.bytesize).to eq(1.megabyte)
+ end
+
it "returns a payload-unavailable record and 404 for a missing record" do
log = Fabricate(:ai_api_audit_log, raw_request_payload: nil, raw_response_payload: nil)
@@ -370,6 +400,7 @@ RSpec.describe DiscourseAi::Admin::AiLogsController do
"payload_available" => false,
"raw_request_payload" => nil,
"raw_response_payload" => nil,
+ "decoded_response" => nil,
)
get "/admin/plugins/discourse-ai/ai-logs/#{log.id + 100_000}.json"
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
index fbb4287e553..4411e1db825 100644
--- a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb
@@ -128,6 +128,12 @@ RSpec.describe DiscourseAi::AiBot::BotController do
response_tokens: 2,
)
+ decoded_stream = <<~SSE
+ data: {"choices":[{"delta":{"content":"decoded response"}}]}
+
+ data: [DONE]
+
+ SSE
log2 =
AiApiAuditLog.create!(
post_id: pm_post.id,
@@ -135,7 +141,7 @@ RSpec.describe DiscourseAi::AiBot::BotController do
topic_id: pm_topic.id,
feature_name: "ai_bot",
raw_request_payload: "request",
- raw_response_payload: "response",
+ raw_response_payload: decoded_stream,
request_tokens: 1,
response_tokens: 2,
)
@@ -166,7 +172,8 @@ RSpec.describe DiscourseAi::AiBot::BotController do
expect(response.parsed_body["request_tokens"]).to eq(1)
expect(response.parsed_body["response_tokens"]).to eq(2)
expect(response.parsed_body["raw_request_payload"]).to eq("request")
- expect(response.parsed_body["raw_response_payload"]).to eq("response")
+ expect(response.parsed_body["raw_response_payload"]).to eq(decoded_stream)
+ expect(response.parsed_body["decoded_response"]).to eq("response" => "decoded response")
get "/discourse-ai/ai-bot/post/#{pm_post3.id}/show-debug-info"
expect(response.status).to eq(200)
@@ -176,6 +183,10 @@ RSpec.describe DiscourseAi::AiBot::BotController do
get "/discourse-ai/ai-bot/show-debug-info/#{log1.id}"
expect(response.status).to eq(200)
expect(response.parsed_body["id"]).to eq(log1.id)
+ expect(response.parsed_body).to include(
+ "raw_response_payload" => "response",
+ "decoded_response" => nil,
+ )
end
it "prefers the post's own log over a newer topic-scoped log like title generation" do
diff --git a/plugins/discourse-ai/spec/serializers/ai_api_audit_log_serializer_spec.rb b/plugins/discourse-ai/spec/serializers/ai_api_audit_log_serializer_spec.rb
index f4ba98a57ab..3f3da40ab11 100644
--- a/plugins/discourse-ai/spec/serializers/ai_api_audit_log_serializer_spec.rb
+++ b/plugins/discourse-ai/spec/serializers/ai_api_audit_log_serializer_spec.rb
@@ -38,6 +38,45 @@ RSpec.describe AiApiAuditLogSerializer do
expect(serialized[:time_to_first_token_msecs]).to eq(125)
end
+ it "decodes supported streaming responses" do
+ raw_response = <<~SSE
+ data: {"choices":[{"delta":{"reasoning":"No"}}]}
+
+ data: {"choices":[{"delta":{"reasoning":" funny stories"}}]}
+
+ data: [DONE]
+
+ SSE
+ log = AiApiAuditLog.new(raw_response_payload: raw_response)
+
+ serialized = described_class.new(log).as_json[:ai_api_audit_log]
+
+ expect(serialized).to include(
+ raw_response_payload: raw_response,
+ decoded_response: {
+ "thinking" => "No funny stories",
+ },
+ )
+ expect(serialized).not_to have_key(:has_decoded_response)
+ end
+
+ it "returns nil when a response cannot be decoded" do
+ raw_response = '{"choices":[{"message":{"content":"hello"}}]}'
+ log = AiApiAuditLog.new(raw_response_payload: raw_response)
+
+ serialized = described_class.new(log).as_json[:ai_api_audit_log]
+
+ expect(serialized).to include(raw_response_payload: raw_response, decoded_response: nil)
+ end
+
+ it "safely serializes missing logs and response payloads" do
+ expect(described_class.new(nil, root: false).as_json).to be_nil
+
+ serialized =
+ described_class.new(AiApiAuditLog.new(raw_response_payload: nil)).as_json[:ai_api_audit_log]
+ expect(serialized).to include(decoded_response: nil)
+ end
+
it "includes a null time to first token for historical logs" do
log = AiApiAuditLog.new(provider_id: AiApiAuditLog::Provider::OpenAI)
diff --git a/plugins/discourse-ai/spec/system/ai_logs_spec.rb b/plugins/discourse-ai/spec/system/ai_logs_spec.rb
index 27866c586fb..49350e5ca3e 100644
--- a/plugins/discourse-ai/spec/system/ai_logs_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_logs_spec.rb
@@ -10,18 +10,54 @@ RSpec.describe "AI logs admin page" do
language_model: llm_model.name,
feature_name: "system-test",
raw_request_payload: '{"prompt":"Inspect this request"}',
- raw_response_payload: '{"answer":"Inspectable response"}',
+ raw_response_payload: <<~SSE,
+ data: {"choices":[{"delta":{"reasoning":"Inspect before answering"}}]}
+
+ data: {"choices":[{"delta":{"content":"Inspectable decoded response"}}]}
+
+ data: [DONE]
+
+ SSE
request_tokens: 20,
response_tokens: 10,
response_status: 200,
duration_msecs: 1_400,
time_to_first_token_msecs: 320,
estimated_cost: 0,
+ feature_context: {
+ source: "system test",
+ },
)
end
let(:ai_logs_page) { PageObjects::Pages::AiLogs.new }
+ def expect_modal_body_to_own_scrolling
+ overflow = page.evaluate_script(<<~JS)
+ (() => {
+ const modal = document.querySelector(".ai-log-detail-modal");
+ const body = modal.querySelector(".d-modal__body");
+ const payload = modal.querySelector(".ai-payload-viewer__content");
+
+ return {
+ modal: getComputedStyle(modal).overflowY,
+ body: getComputedStyle(body).overflowY,
+ payload: getComputedStyle(payload).overflowY,
+ };
+ })()
+ JS
+
+ expect(overflow).to eq("modal" => "hidden", "body" => "auto", "payload" => "visible")
+ expect(page).to have_css(".ai-payload-viewer.--unbounded")
+ expect(page).to have_css(".ai-payload-viewer__content:not([tabindex])")
+ end
+
+ def expect_top_copy(label)
+ expect(page).to have_css(".ai-log-detail-modal__tabs .ai-log-detail-modal__copy", text: label)
+ expect(page).to have_no_css(".ai-payload-viewer__copy")
+ expect(page).to have_no_css(".ai-log-detail-modal input[type='hidden']", visible: :all)
+ end
+
before do
enable_current_plugin
sign_in(admin)
@@ -88,11 +124,51 @@ RSpec.describe "AI logs admin page" do
ai_logs_page.open_log(log)
expect(ai_logs_page).to have_payload("Inspect this request")
+ expect_modal_body_to_own_scrolling
+ expect_top_copy(I18n.t("js.discourse_ai.logs.detail.copy_request"))
expect(page).to have_css(".ai-log-detail-modal__metadata-duration", text: "1.4 s")
expect(page).to have_css(".ai-log-detail-modal__metadata-first-token", text: "0.3 s")
expect(page).to have_no_css(".ai-log-detail-modal__metadata-cost")
expect(page).to have_current_path(/details=#{log.id}/, url: true)
+ find(
+ ".ai-log-detail-modal__tabs .btn",
+ text: I18n.t("js.discourse_ai.logs.detail.response"),
+ ).click
+ expect(page).to have_css(".ai-decoded-transcript__thinking", text: "Inspect before answering")
+ expect(page).to have_css(
+ ".ai-decoded-transcript__section.--response",
+ text: "Inspectable decoded response",
+ )
+ expect(page).to have_css(
+ ".ai-log-detail-modal__tabs .ai-log-detail-modal__response-toggle",
+ text: I18n.t("js.discourse_ai.view_raw"),
+ )
+ expect_top_copy(I18n.t("js.discourse_ai.logs.detail.copy_response"))
+
+ find(".ai-log-detail-modal__response-toggle").click
+ expect(ai_logs_page).to have_payload("data:")
+ expect_modal_body_to_own_scrolling
+ expect_top_copy(I18n.t("js.discourse_ai.logs.detail.copy_raw_response"))
+ expect(page).to have_css(
+ ".ai-log-detail-modal__response-toggle",
+ text: I18n.t("js.discourse_ai.view_decoded"),
+ )
+
+ find(".ai-log-detail-modal__response-toggle").click
+ expect(page).to have_css(
+ ".ai-decoded-transcript__section.--response",
+ text: "Inspectable decoded response",
+ )
+
+ find(
+ ".ai-log-detail-modal__tabs .btn",
+ text: I18n.t("js.discourse_ai.logs.detail.context"),
+ ).click
+ expect(ai_logs_page).to have_payload("system test")
+ expect_modal_body_to_own_scrolling
+ expect_top_copy(I18n.t("js.discourse_ai.logs.detail.copy_context"))
+
page.go_back
expect(page).to have_no_css(".ai-log-detail-modal")
expect(page).to have_current_path(%r{/ai-logs$}, url: true)
@@ -100,6 +176,7 @@ RSpec.describe "AI logs admin page" do
page.go_forward
expect(ai_logs_page).to have_payload("Inspect this request")
find(".ai-log-detail-modal .modal-close").click
+ expect(page).to have_current_path(%r{/ai-logs$}, url: true)
ai_logs_page.open_retention
expect(ai_logs_page).to have_retention_modal
expect(page).to have_field(
diff --git a/plugins/discourse-ai/test/javascripts/acceptance/debug-ai-modal-test.gjs b/plugins/discourse-ai/test/javascripts/acceptance/debug-ai-modal-test.gjs
index d458e0edcae..2fe624bcfee 100644
--- a/plugins/discourse-ai/test/javascripts/acceptance/debug-ai-modal-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/acceptance/debug-ai-modal-test.gjs
@@ -1,30 +1,50 @@
import { getOwner } from "@ember/owner";
-import { settled, visit } from "@ember/test-helpers";
+import { click, settled, visit } from "@ember/test-helpers";
import { test } from "qunit";
+import sinon from "sinon";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import { i18n } from "discourse-i18n";
import DebugAiModal from "discourse/plugins/discourse-ai/discourse/components/modal/debug-ai-modal";
const MODEL = { id: 42 };
-function auditLog(timeToFirstToken, duration = 22_300) {
+const RAW_RESPONSE =
+ 'data: {"choices":[{"delta":{"content":"Decoded response"}}]}\n\ndata: [DONE]\n\n';
+
+function auditLog(timeToFirstToken, duration = 22_300, overrides = {}) {
return {
id: 1,
request_tokens: 10,
response_tokens: 5,
raw_request_payload: "{}",
- raw_response_payload: "response",
+ raw_response_payload: RAW_RESPONSE,
+ decoded_response: { response: "Decoded response" },
duration_msecs: duration,
time_to_first_token_msecs: timeToFirstToken,
+ ...overrides,
};
}
acceptance("AI debug modal", function (needs) {
let timeToFirstToken;
+ let initialOverrides;
needs.user();
+ needs.hooks.beforeEach(() => {
+ timeToFirstToken = undefined;
+ initialOverrides = undefined;
+ });
needs.pretender((server, helper) => {
server.get("/discourse-ai/ai-bot/post/42/show-debug-info.json", () =>
- helper.response(auditLog(timeToFirstToken))
+ helper.response(auditLog(timeToFirstToken, 22_300, initialOverrides))
+ );
+ server.get("/discourse-ai/ai-bot/show-debug-info/2.json", () =>
+ helper.response(
+ auditLog(timeToFirstToken, 22_300, {
+ id: 2,
+ decoded_response: { response: "Next decoded response" },
+ })
+ )
);
});
@@ -43,6 +63,178 @@ acceptance("AI debug modal", function (needs) {
);
});
+ test("it displays decoded responses and switches representations", async function (assert) {
+ await visit("/");
+ getOwner(this).lookup("service:modal").show(DebugAiModal, { model: MODEL });
+ await settled();
+
+ await click(".ai-debug-modal__nav li:nth-child(2) a");
+
+ assert
+ .dom(".ai-decoded-transcript__text")
+ .hasText("Decoded response", "the decoded response is shown first");
+ assert
+ .dom(".d-modal__footer .ai-debug-modal__response-toggle")
+ .hasText(
+ i18n("discourse_ai.view_raw"),
+ "the raw response can be selected from the footer"
+ );
+ assert
+ .dom(".d-modal__footer .ai-debug-modal__copy")
+ .hasText(
+ i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response"),
+ "the response copy action stays in the modal footer"
+ );
+ assert
+ .dom(".d-modal__body .ai-debug-modal__copy")
+ .doesNotExist("the copy action is outside the scrolling modal body");
+
+ await click(".ai-debug-modal__response-toggle");
+
+ assert
+ .dom(".ai-payload-viewer__content")
+ .hasText(RAW_RESPONSE, "the exact raw response is shown");
+ assert
+ .dom(".ai-debug-modal__response-toggle")
+ .hasText(
+ i18n("discourse_ai.view_decoded"),
+ "the decoded response can be restored"
+ );
+ assert
+ .dom(".ai-debug-modal__copy")
+ .hasText(
+ i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response"),
+ "the response copy action is identified"
+ );
+
+ await click(".ai-debug-modal__response-toggle");
+ assert
+ .dom(".ai-decoded-transcript__text")
+ .hasText("Decoded response", "the decoded response is restored");
+ assert
+ .dom("#ai-debug-modal-copy-source")
+ .doesNotExist("copying does not depend on a fixed DOM source");
+
+ const writeText = sinon.stub().resolves();
+ sinon.stub(window.navigator, "clipboard").get(() => ({ writeText }));
+
+ assert
+ .dom(".ai-debug-modal__copy-actions .sr-only[aria-live='polite']")
+ .exists("copy feedback can be announced");
+
+ await click(".ai-debug-modal__copy");
+
+ assert.true(
+ writeText.calledWithExactly("Decoded response"),
+ "the active decoded payload is copied"
+ );
+ });
+
+ test("it presents thinking and tool activity as structured details", async function (assert) {
+ initialOverrides = {
+ decoded_response: {
+ thinking: "Check the documentation",
+ tool_calls: [
+ { id: "call-1", name: "search", arguments: { query: "decoder" } },
+ ],
+ tool_results: [
+ { call_id: "call-1", type: "search_result", result: "Found it" },
+ ],
+ response: "Done",
+ },
+ };
+ await visit("/");
+ getOwner(this).lookup("service:modal").show(DebugAiModal, { model: MODEL });
+ await settled();
+
+ await click(".ai-debug-modal__nav li:nth-child(2) a");
+
+ assert
+ .dom(".ai-decoded-transcript__thinking")
+ .includesText(
+ "Check the documentation",
+ "thinking is clearly identified"
+ );
+ assert
+ .dom(".ai-decoded-transcript__item-heading")
+ .includesText("search", "tool calls are clearly identified");
+ assert
+ .dom(".ai-decoded-transcript__code")
+ .includesText('"query": "decoder"', "tool arguments are readable");
+ assert
+ .dom(".ai-decoded-transcript__section.--tool-results")
+ .includesText("Found it", "tool results are clearly identified");
+ assert
+ .dom(".ai-decoded-transcript__section.--response")
+ .includesText("Done", "the final response is clearly identified");
+ });
+
+ test("it keeps structured model output in the response", async function (assert) {
+ initialOverrides = {
+ decoded_response: {
+ response: JSON.stringify({ thinking: "about this" }),
+ },
+ };
+ await visit("/");
+ getOwner(this).lookup("service:modal").show(DebugAiModal, { model: MODEL });
+ await settled();
+
+ await click(".ai-debug-modal__nav li:nth-child(2) a");
+
+ assert
+ .dom(".ai-decoded-transcript__thinking")
+ .doesNotExist("the model output is not presented as transcript thinking");
+ assert
+ .dom(".ai-decoded-transcript__text")
+ .hasText(
+ '{"thinking":"about this"}',
+ "the structured model response remains intact"
+ );
+ });
+
+ test("it omits the switch for raw fallback responses", async function (assert) {
+ initialOverrides = {
+ raw_response_payload: "upstream error",
+ decoded_response: null,
+ };
+ await visit("/");
+ getOwner(this).lookup("service:modal").show(DebugAiModal, { model: MODEL });
+ await settled();
+
+ await click(".ai-debug-modal__nav li:nth-child(2) a");
+
+ assert
+ .dom(".ai-debug-modal__response-toggle")
+ .doesNotExist("no representation switch is shown");
+ assert
+ .dom(".ai-debug-modal__copy")
+ .hasText(
+ i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response"),
+ "the existing copy action remains unchanged"
+ );
+ });
+
+ test("it resets to decoded when navigating between logs", async function (assert) {
+ initialOverrides = { next_log_id: 2 };
+ await visit("/");
+ getOwner(this).lookup("service:modal").show(DebugAiModal, { model: MODEL });
+ await settled();
+
+ await click(".ai-debug-modal__nav li:nth-child(2) a");
+ await click(".ai-debug-modal__response-toggle");
+ await click(".ai-debug-modal__next");
+
+ assert
+ .dom(".ai-decoded-transcript__text")
+ .hasText("Next decoded response", "navigation restores the decoded view");
+ assert
+ .dom(".ai-debug-modal__copy")
+ .hasText(
+ i18n("discourse_ai.ai_bot.debug_ai_modal.copy_response"),
+ "the response copy label is restored"
+ );
+ });
+
test("it identifies unavailable historical timing", async function (assert) {
timeToFirstToken = null;
await visit("/");
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-decoded-transcript-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-decoded-transcript-test.gjs
new file mode 100644
index 00000000000..ca9a633b014
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-decoded-transcript-test.gjs
@@ -0,0 +1,128 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import AiDecodedTranscript from "discourse/plugins/discourse-ai/discourse/components/ai-decoded-transcript";
+
+module("Integration | Component | AiDecodedTranscript", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("renders thinking, tool activity, and the response", async function (assert) {
+ const transcript = {
+ thinking: "Check <img src=x onerror=alert(1)>",
+ tool_calls: [
+ {
+ id: "call-1",
+ name: "search",
+ arguments: { query: "decoder" },
+ },
+ ],
+ tool_results: [
+ {
+ call_id: "call-1",
+ type: "search_result",
+ result: { matches: ["Found it"] },
+ is_error: true,
+ },
+ ],
+ response: "Done",
+ };
+
+ await render(
+ <template><AiDecodedTranscript @response={{transcript}} /></template>
+ );
+
+ assert
+ .dom(".ai-decoded-transcript__thinking[open]")
+ .exists("thinking is visible by default");
+ assert
+ .dom(".ai-decoded-transcript__thinking")
+ .includesText(
+ "Check <img src=x onerror=alert(1)>",
+ "thinking is rendered as text"
+ );
+ assert
+ .dom(".ai-decoded-transcript img")
+ .doesNotExist("model-generated HTML is escaped");
+ assert
+ .dom(".ai-decoded-transcript__item")
+ .exists({ count: 2 }, "the tool call and result are listed");
+ assert
+ .dom(".ai-decoded-transcript__item-heading")
+ .includesText("search", "the tool name is visible");
+ assert
+ .dom(".ai-decoded-transcript__code")
+ .includesText('"query": "decoder"', "arguments are formatted");
+ assert
+ .dom(".ai-decoded-transcript__item.--error")
+ .includesText('"matches": [', "structured error results are formatted");
+ assert
+ .dom(".ai-decoded-transcript__list[role='list']")
+ .exists({ count: 2 }, "tool collections retain list semantics");
+ assert
+ .dom(".ai-decoded-transcript__code[dir='ltr']")
+ .exists({ count: 2 }, "structured payloads use isolated text direction");
+ assert
+ .dom(".ai-decoded-transcript__code[tabindex]")
+ .doesNotExist("non-scrollable code blocks are not tab stops");
+ assert
+ .dom(".ai-decoded-transcript__section.--response")
+ .includesText("Done", "the final response is visible");
+ });
+
+ test("renders answer-only decoded responses without inferring transcript fields", async function (assert) {
+ const response = { response: '{"thinking":"about this"}' };
+
+ await render(
+ <template><AiDecodedTranscript @response={{response}} /></template>
+ );
+
+ assert
+ .dom(".ai-decoded-transcript__thinking")
+ .doesNotExist("model-authored JSON is not presented as thinking");
+ assert
+ .dom(".ai-decoded-transcript__section.--response")
+ .includesText(
+ '{"thinking":"about this"}',
+ "the model response remains intact"
+ );
+ });
+
+ test("ignores invalid and empty decoded responses", async function (assert) {
+ for (const response of [
+ null,
+ "response",
+ [],
+ {},
+ { tool_calls: [null, {}], tool_results: [null, {}] },
+ ]) {
+ await render(
+ <template><AiDecodedTranscript @response={{response}} /></template>
+ );
+ assert
+ .dom(".ai-decoded-transcript")
+ .doesNotExist("invalid decoded responses render nothing");
+ }
+ });
+
+ test("tolerates missing and unexpected tool data", async function (assert) {
+ const transcript = {
+ thinking: "Still checking",
+ tool_calls: [null, { name: "calculator", arguments: false }],
+ tool_results: "not an array",
+ };
+
+ await render(
+ <template><AiDecodedTranscript @response={{transcript}} /></template>
+ );
+
+ assert
+ .dom(".ai-decoded-transcript__item")
+ .exists({ count: 1 }, "invalid calls are omitted from the transcript");
+ assert
+ .dom(".ai-decoded-transcript__code")
+ .hasText("false", "non-object arguments remain readable");
+ assert
+ .dom(".ai-decoded-transcript__section")
+ .exists({ count: 1 }, "invalid result collections are omitted");
+ });
+});
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-payload-viewer-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-payload-viewer-test.gjs
index f2e914afa90..6fba650bb5a 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-payload-viewer-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-payload-viewer-test.gjs
@@ -1,5 +1,6 @@
-import { render } from "@ember/test-helpers";
+import { click, render } from "@ember/test-helpers";
import { module, test } from "qunit";
+import sinon from "sinon";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import AiPayloadViewer from "discourse/plugins/discourse-ai/discourse/components/ai-payload-viewer";
@@ -8,6 +9,8 @@ module("Integration | Component | AiPayloadViewer", function (hooks) {
test("formats JSON as escaped text", async function (assert) {
const payload = JSON.stringify({ html: "<img src=x onerror=alert(1)>" });
+ const writeText = sinon.stub().resolves();
+ sinon.stub(window.navigator, "clipboard").get(() => ({ writeText }));
await render(
<template>
@@ -28,9 +31,53 @@ module("Integration | Component | AiPayloadViewer", function (hooks) {
assert
.dom(".ai-payload-viewer__content")
.hasAttribute("tabindex", "0", "long payloads are keyboard-scrollable");
+ assert
+ .dom(".ai-payload-viewer__copy")
+ .hasText("Copy payload", "the copy action is shown by default");
+ assert
+ .dom(".ai-payload-viewer input[type='hidden']")
+ .doesNotExist("copying does not duplicate the payload in the DOM");
+ assert
+ .dom(".ai-payload-viewer .sr-only[aria-live='polite']")
+ .exists("copy confirmation can be announced");
assert
.dom(".ai-payload-viewer img")
.doesNotExist("payload HTML is not rendered");
+
+ await click(".ai-payload-viewer__copy");
+
+ assert.true(
+ writeText.calledWithExactly(payload),
+ "the original payload is copied directly"
+ );
+ });
+
+ test("omits the copy action without a label", async function (assert) {
+ await render(
+ <template>
+ <AiPayloadViewer
+ @payload="payload"
+ @unbounded={{true}}
+ @emptyMessage="Payload not available"
+ />
+ </template>
+ );
+
+ assert
+ .dom(".ai-payload-viewer__copy")
+ .doesNotExist("the copy action can be rendered elsewhere");
+ assert
+ .dom(".ai-payload-viewer input[type='hidden']")
+ .doesNotExist("no unused copy source is rendered");
+ assert
+ .dom(".ai-payload-viewer")
+ .hasClass("--unbounded", "the unbounded layout is exposed as a modifier");
+ assert
+ .dom(".ai-payload-viewer__content")
+ .doesNotHaveAttribute(
+ "tabindex",
+ "unbounded payloads are not unnecessary tab stops"
+ );
});
test("shows the unavailable and truncated states", async function (assert) {
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/decoded-response-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/decoded-response-test.js
new file mode 100644
index 00000000000..d92c068f261
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/decoded-response-test.js
@@ -0,0 +1,58 @@
+import { module, test } from "qunit";
+import {
+ decodedResponseText,
+ isDecodedResponse,
+} from "discourse/plugins/discourse-ai/discourse/lib/decoded-response";
+
+module("Unit | Lib | decoded-response", function () {
+ test("identifies normalized decoded responses", function (assert) {
+ assert.true(
+ isDecodedResponse({ response: "answer" }),
+ "answer-only responses are valid"
+ );
+ assert.true(
+ isDecodedResponse({
+ tool_calls: [{ name: "search", arguments: { query: "docs" } }],
+ }),
+ "valid tool-only responses are valid"
+ );
+ assert.true(
+ isDecodedResponse({ tool_results: [{ result: "Found it" }] }),
+ "valid result-only responses are valid"
+ );
+ assert.false(
+ isDecodedResponse({ tool_calls: [null, {}] }),
+ "invalid tool calls do not create a decoded response"
+ );
+ assert.false(
+ isDecodedResponse({ tool_results: [null, {}] }),
+ "invalid tool results do not create a decoded response"
+ );
+ assert.false(
+ isDecodedResponse({ response: { thinking: "spoofed" } }),
+ "non-string responses are invalid"
+ );
+ assert.false(isDecodedResponse({}), "empty objects are invalid");
+ assert.false(isDecodedResponse(null), "null is invalid");
+ assert.false(isDecodedResponse("answer"), "strings are invalid");
+ assert.false(isDecodedResponse([]), "arrays are invalid");
+ });
+
+ test("preserves plain answer copy and formats transcripts", function (assert) {
+ assert.strictEqual(
+ decodedResponseText({ response: "answer" }),
+ "answer",
+ "answer-only responses copy as plain text"
+ );
+ assert.strictEqual(
+ decodedResponseText({ thinking: "reason", response: "answer" }),
+ '{\n "thinking": "reason",\n "response": "answer"\n}',
+ "multi-section responses copy as readable JSON"
+ );
+ assert.strictEqual(
+ decodedResponseText(null),
+ null,
+ "invalid responses have no copy value"
+ );
+ });
+});