FEATURE: persistent key-value storage for AI Artifacts (#1417)
Introduces a persistent, user-scoped key-value storage system for
AI Artifacts, enabling them to be stateful and interactive. This
transforms artifacts from static content into mini-applications that can
save user input, preferences, and other data.
The core components of this feature are:
1. **Model and API**:
- A new `AiArtifactKeyValue` model and corresponding database table to
store data associated with a user and an artifact.
- A new `ArtifactKeyValuesController` provides a RESTful API for
CRUD operations (`index`, `set`, `destroy`) on the key-value data.
- Permissions are enforced: users can only modify their own data but
can view public data from other users.
2. **Secure JavaScript Bridge**:
- A `postMessage` communication bridge is established between the
sandboxed artifact `iframe` and the parent Discourse window.
- A JavaScript API is exposed to the artifact as `window.discourseArtifact`
with async methods: `get(key)`, `set(key, value, options)`,
`delete(key)`, and `index(filter)`.
- The parent window handles these requests, makes authenticated calls to the
new controller, and returns the results to the iframe. This ensures
security by keeping untrusted JS isolated.
3. **AI Tool Integration**:
- The `create_artifact` tool is updated with a `requires_storage`
boolean parameter.
- If an artifact requires storage, its metadata is flagged, and the
system prompt for the code-generating AI is augmented with detailed
documentation for the new storage API.
4. **Configuration**:
- Adds hidden site settings `ai_artifact_kv_value_max_length` and
`ai_artifact_max_keys_per_user_per_artifact` for throttling.
This also includes a minor fix to use `jsonb_set` when updating
artifact metadata, ensuring other metadata fields are preserved.
2025-06-11 06:59:46 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2025-06-20 23:49:44 +02:00
|
|
|
RSpec.describe "AI Artifact Key-Value API", type: :system do
|
FEATURE: persistent key-value storage for AI Artifacts (#1417)
Introduces a persistent, user-scoped key-value storage system for
AI Artifacts, enabling them to be stateful and interactive. This
transforms artifacts from static content into mini-applications that can
save user input, preferences, and other data.
The core components of this feature are:
1. **Model and API**:
- A new `AiArtifactKeyValue` model and corresponding database table to
store data associated with a user and an artifact.
- A new `ArtifactKeyValuesController` provides a RESTful API for
CRUD operations (`index`, `set`, `destroy`) on the key-value data.
- Permissions are enforced: users can only modify their own data but
can view public data from other users.
2. **Secure JavaScript Bridge**:
- A `postMessage` communication bridge is established between the
sandboxed artifact `iframe` and the parent Discourse window.
- A JavaScript API is exposed to the artifact as `window.discourseArtifact`
with async methods: `get(key)`, `set(key, value, options)`,
`delete(key)`, and `index(filter)`.
- The parent window handles these requests, makes authenticated calls to the
new controller, and returns the results to the iframe. This ensures
security by keeping untrusted JS isolated.
3. **AI Tool Integration**:
- The `create_artifact` tool is updated with a `requires_storage`
boolean parameter.
- If an artifact requires storage, its metadata is flagged, and the
system prompt for the code-generating AI is augmented with detailed
documentation for the new storage API.
4. **Configuration**:
- Adds hidden site settings `ai_artifact_kv_value_max_length` and
`ai_artifact_max_keys_per_user_per_artifact` for throttling.
This also includes a minor fix to use `jsonb_set` when updating
artifact metadata, ensuring other metadata fields are preserved.
2025-06-11 06:59:46 +10:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:private_message_topic) { Fabricate(:private_message_topic, user: user) }
|
|
|
|
fab!(:private_message_post) { Fabricate(:post, topic: private_message_topic, user: user) }
|
|
|
|
fab!(:artifact) do
|
|
|
|
Fabricate(
|
|
|
|
:ai_artifact,
|
|
|
|
post: private_message_post,
|
|
|
|
metadata: {
|
|
|
|
public: true,
|
|
|
|
},
|
|
|
|
html: '<div id="log">Artifact Loaded</div>',
|
|
|
|
js: <<~JS,
|
|
|
|
const logElement = document.getElementById('log');
|
|
|
|
|
|
|
|
window.addEventListener('load', async function() {
|
|
|
|
try {
|
|
|
|
logElement.innerHTML = "TESTING KEY-VALUE API...";
|
|
|
|
const log = [];
|
|
|
|
await window.discourseArtifact.set('test_key', 'test_value');
|
|
|
|
log.push('Set operation completed');
|
|
|
|
logElement.innerHTML = log.join('<br>');
|
|
|
|
|
|
|
|
const value = await window.discourseArtifact.get('test_key');
|
|
|
|
log.push('Got value:' + value);
|
|
|
|
|
|
|
|
await window.discourseArtifact.delete('test_key');
|
|
|
|
log.push('Delete operation completed');
|
|
|
|
|
|
|
|
const deletedValue = await window.discourseArtifact.get('test_key');
|
|
|
|
log.push('Deleted value should be null:' + deletedValue);
|
|
|
|
|
|
|
|
logElement.innerHTML = log.join('<br>');
|
|
|
|
logElement.setAttribute('data-test-complete', 'true');
|
|
|
|
} catch (error) {
|
|
|
|
logElement.innerHTML = error.message;
|
|
|
|
logElement.setAttribute('data-test-error', 'true');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.discourse_ai_enabled = true
|
|
|
|
SiteSetting.ai_bot_enabled = true
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "provides working key-value API in artifact JavaScript" do
|
|
|
|
visit "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
|
|
|
|
|
|
|
|
within_frame(find("iframe")) do
|
|
|
|
expect(page).to have_selector("#log", wait: 2)
|
|
|
|
expect(page).to have_selector("#log[data-test-complete='true']", wait: 2)
|
|
|
|
expect(page).to have_no_selector("#log[data-test-error]")
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(artifact.key_values.find_by(key: "test_key", user: user)).to be_nil
|
|
|
|
end
|
|
|
|
end
|