Repository navigation
Add native card renderers for custom response element types #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
3a871f9
f407466
1d89653
398f787
955f0c5
89aa0c9
e1eca42
429eb66
05305fa
9aa3119
b96888d
9e3fe32
c088d67
6a52c85
43ffaae
54aca35
2ec024b
b8b5523
e475f67
13dbfa8
4d26f1d
ac41aba
cc524b4
d9e4996
1262406
8c1f69d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package com.adobe.marketing.mobile.concierge | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
|
|
||
| /** | ||
| * Renders one transcript card for a response element whose `type` was registered with | ||
| * [Concierge.registerCardRenderer]. | ||
| * | ||
| * - `element`: the complete, read-only element dictionary from `multimodalElements.elements`. | ||
| * - `context`: read-only card context, rebuilt on every recomposition: `cardId`, `sessionId`, | ||
| * `conversationId`, `interactionId` (each may be absent), `isBusy`, `isStale`, and `state` | ||
| * (this card's last saved state, if any). | ||
| * - `saveState`: replaces this card's private state. Callable from any thread; applied on the main | ||
| * thread. Never sent to Brand Concierge. | ||
| * | ||
| * Renderers must not throw, and must not start work during composition. | ||
| */ | ||
| typealias ConciergeCardRenderer = @Composable ( | ||
| element: Map<String, Any?>, | ||
| context: Map<String, Any?>, | ||
| saveState: (Map<String, Any?>) -> Unit | ||
| ) -> Unit |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package com.adobe.marketing.mobile.concierge.cards | ||
|
|
||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import java.math.BigDecimal | ||
| import java.math.BigInteger | ||
| import java.util.Collections | ||
|
|
||
| /** | ||
| * JSON value model for response elements, renderer actions, and action results: | ||
| * `String`, `Boolean`, finite `Number`, `null`, `List`, and string-keyed `Map`. | ||
| */ | ||
| internal object CardJson { | ||
| private const val MAX_DEPTH = 64 | ||
| private const val MAX_NODES = 100_000 | ||
|
|
||
| private class CopyBudget(var remaining: Int = MAX_NODES) { | ||
| fun consume(path: String) { | ||
| require(remaining > 0) { "$path exceeds the $MAX_NODES value limit" } | ||
| remaining-- | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a deep, read-only copy of [value], preserving nulls and number types. | ||
| * | ||
| * @throws IllegalArgumentException naming the path of the first unsupported value. | ||
| */ | ||
| fun readOnlyCopy(value: Map<*, *>): Map<String, Any?> = copyObject(value, "$", 0, CopyBudget()) | ||
|
|
||
| private fun copyObject(value: Map<*, *>, path: String, depth: Int, budget: CopyBudget): Map<String, Any?> { | ||
| require(depth < MAX_DEPTH) { "$path nests deeper than $MAX_DEPTH levels" } | ||
| budget.consume(path) | ||
| require(value.size <= budget.remaining) { "$path exceeds the $MAX_NODES value limit" } | ||
| val copy = LinkedHashMap<String, Any?>(value.size) | ||
| for ((key, entry) in value) { | ||
| require(key is String) { "$path has a non-string key" } | ||
| copy[key] = copyValue(entry, "$path.$key", depth + 1, budget) | ||
| } | ||
| return Collections.unmodifiableMap(copy) | ||
| } | ||
|
|
||
| private fun copyValue(value: Any?, path: String, depth: Int, budget: CopyBudget): Any? = when (value) { | ||
| is Map<*, *> -> copyObject(value, path, depth, budget) | ||
| is List<*> -> { | ||
| require(depth < MAX_DEPTH) { "$path nests deeper than $MAX_DEPTH levels" } | ||
| budget.consume(path) | ||
| require(value.size <= budget.remaining) { "$path exceeds the $MAX_NODES value limit" } | ||
| Collections.unmodifiableList(value.mapIndexed { index, item -> copyValue(item, "$path[$index]", depth + 1, budget) }) | ||
| } | ||
| null, is String, is Boolean -> value.also { budget.consume(path) } | ||
| is Double, is Float -> value.also { | ||
| require((value as Number).toDouble().isFinite()) { "$path is not a finite number" } | ||
| budget.consume(path) | ||
| } | ||
| is Int, is Long, is Short, is Byte, is BigDecimal, is BigInteger -> value.also { budget.consume(path) } | ||
| else -> throw IllegalArgumentException("$path has an unsupported ${value.javaClass.simpleName} value") | ||
| } | ||
|
|
||
| /** Converts a value of the JSON model to org.json types (null becomes [JSONObject.NULL]). */ | ||
| fun toJsonValue(value: Any?): Any = when (value) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW]
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
| null -> JSONObject.NULL | ||
| is Map<*, *> -> JSONObject().apply { value.forEach { (key, entry) -> put(key.toString(), toJsonValue(entry)) } } | ||
| is List<*> -> JSONArray().apply { value.forEach { put(toJsonValue(it)) } } | ||
| else -> value | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package com.adobe.marketing.mobile.concierge.cards | ||
|
|
||
| import com.adobe.marketing.mobile.concierge.ConciergeCardRenderer | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| /** Process-wide card renderers by exact element `type`; the last registration wins. */ | ||
| internal object ConciergeCardRenderers { | ||
| private val renderers = ConcurrentHashMap<String, ConciergeCardRenderer>() | ||
|
|
||
| fun register(type: String, renderer: ConciergeCardRenderer) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also need a way to un-register the renderer?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be added as an enhancement for later. |
||
| require(type.isNotBlank()) { "Card renderer type must not be blank." } | ||
| renderers[type] = renderer | ||
| } | ||
|
|
||
| fun rendererFor(type: String?): ConciergeCardRenderer? = type?.let(renderers::get) | ||
|
|
||
| val hasAnyRenderer: Boolean | ||
| get() = renderers.isNotEmpty() | ||
|
|
||
| internal fun clear() = renderers.clear() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if it is safe to replace
CardJsonwith a smalltoJsonSafe()extension something like below