Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit a1754f3

Browse files
committed
FEATURE: allow passing in data attributes to an artifact
Also allow artifact access to current username Usage inside artifact is: 1. await window.discourseArtifactReady; 2. access data via window.discourseArtifactData;
1 parent 925949d commit a1754f3

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

app/controllers/discourse_ai/ai_bot/artifacts_controller.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ def show
4242
<style>
4343
#{artifact.css}
4444
</style>
45+
<script>
46+
window._discourse_user_data = {
47+
#{current_user ? "username: #{current_user.username.to_json}" : "username: null"}
48+
};
49+
window.discourseArtifactReady = new Promise(resolve => {
50+
window._resolveArtifactData = resolve;
51+
});
52+
window.addEventListener('message', function(event) {
53+
if (event.data && event.data.type === 'discourse-artifact-data') {
54+
window.discourseArtifactData = event.data.dataset || {};
55+
Object.assign(window.discourseArtifactData, window._discourse_user_data);
56+
window._resolveArtifactData(window.discourseArtifactData);
57+
}
58+
});
59+
</script>
4560
</head>
4661
<body>
4762
#{artifact.html}
@@ -74,6 +89,19 @@ def show
7489
</head>
7590
<body>
7691
<iframe sandbox="allow-scripts allow-forms" height="100%" width="100%" srcdoc="#{ERB::Util.html_escape(untrusted_html)}" frameborder="0"></iframe>
92+
<script>
93+
document.querySelector('iframe').addEventListener('load', function() {
94+
try {
95+
const iframeWindow = this.contentWindow;
96+
const message = { type: 'discourse-artifact-data', dataset: {} };
97+
98+
if (window.frameElement && window.frameElement.dataset) {
99+
Object.assign(message.dataset, window.frameElement.dataset);
100+
}
101+
iframeWindow.postMessage(message, '*');
102+
} catch (e) { console.error('Error passing data to artifact:', e); }
103+
});
104+
</script>
77105
</body>
78106
</html>
79107
HTML

assets/javascripts/discourse/components/ai-artifact.gjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Component from "@glimmer/component";
22
import { tracked } from "@glimmer/tracking";
33
import { action } from "@ember/object";
4+
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
45
import { service } from "@ember/service";
56
import DButton from "discourse/components/d-button";
67
import htmlClass from "discourse/helpers/html-class";
@@ -88,6 +89,15 @@ export default class AiArtifactComponent extends Component {
8889
}`;
8990
}
9091

92+
@action
93+
setDataAttributes(element) {
94+
if (this.args.dataAttributes) {
95+
Object.entries(this.args.dataAttributes).forEach(([key, value]) => {
96+
element.setAttribute(key, value);
97+
});
98+
}
99+
}
100+
91101
<template>
92102
{{#if this.expanded}}
93103
{{htmlClass "ai-artifact-expanded"}}
@@ -118,6 +128,7 @@ export default class AiArtifactComponent extends Component {
118128
src={{this.artifactUrl}}
119129
width="100%"
120130
frameborder="0"
131+
{{didInsert this.setDataAttributes}}
121132
></iframe>
122133
{{/if}}
123134
{{#unless this.requireClickToRun}}

assets/javascripts/initializers/ai-artifacts.gjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@ function initializeAiArtifacts(api) {
1818
"data-ai-artifact-version"
1919
);
2020

21+
const dataAttributes = {};
22+
for (const attr of artifactElement.attributes) {
23+
if (
24+
attr.name.startsWith("data-") &&
25+
attr.name !== "data-ai-artifact-id" &&
26+
attr.name !== "data-ai-artifact-version"
27+
) {
28+
dataAttributes[attr.name] = attr.value;
29+
}
30+
}
31+
2132
helper.renderGlimmer(
2233
artifactElement,
2334
<template>
2435
<AiArtifact
2536
@artifactId={{artifactId}}
2637
@artifactVersion={{artifactVersion}}
38+
@dataAttributes={{dataAttributes}}
2739
/>
2840
</template>
2941
);

0 commit comments

Comments
 (0)