Skip to content

Commit

Permalink
Demo Intent Resolver Working
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Jun 20, 2024
1 parent 71d2e6c commit 14f9424
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export class DefaultDesktopAgentIntentResolver implements IntentResolver {

themeContainer(container: HTMLDivElement) {
const css = this.details.css ?? DEFAULT_INTENT_RESOLVER_DETAILS.css!!
for (const k in CSS_ELEMENTS) {
for (let i = 0; i < CSS_ELEMENTS.length; i++) {
const k = CSS_ELEMENTS[i]
const value: string | undefined = css[(k as string)]
if (value != null) {
container.style.setProperty(k, value)
Expand Down
2 changes: 1 addition & 1 deletion packages/demo/src/client/ui/channel-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ window.addEventListener("load", () => {
channels = details.channels
channelId = details.selected

const selectedColor = (channelId ? (channels.find(c => c.id == channelId)?.displayMetadata?.color) : null) ?? 'black'
const selectedColor = (channelId ? (channels.find(c => c.id == channelId)?.displayMetadata?.color) : null) ?? 'white'
logo.style.backgroundColor = selectedColor
}
})
Expand Down
120 changes: 44 additions & 76 deletions packages/demo/src/client/ui/intent-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,57 @@
import { AppIdentifier, AppIntent, IntentMetadata } from "@finos/fdc3";
import { AppMetadata } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { io } from "socket.io-client"
import { FDC3_APP_EVENT } from "../../message-types";
import { IntentResolutionChoiceAgentRequest } from "@kite9/fdc3-common";
import { v4 as uuid } from 'uuid'
import { ResolverIntents, ResolverMessageChoice, SingleAppIntent } from "@kite9/fdc3-common";

function getQueryVariable(variable: string): string {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}

return ""
}

function getAppIntents(): AppIntent[] {
const intentDetails = getQueryVariable("intentDetails")
const decoded = decodeURIComponent(intentDetails)
const object: AppIntent[] = JSON.parse(decoded)
return object
}
window.addEventListener("load", () => {

function getSource(): AppIdentifier {
const source = getQueryVariable("source")
const decoded = decodeURIComponent(source)
const object: AppIdentifier = JSON.parse(decoded)
return object
}
const parent = window.parent;

const socket = io()

socket.on("connect", async () => {

const intentDetails = getAppIntents()
const source = getSource()
const mc = new MessageChannel();
const myPort = mc.port1
myPort.start()

const list = document.getElementById("intent-list")!!

function sendChosenIntent(intent: IntentMetadata, app: AppMetadata, source: AppMetadata) {
const out: IntentResolutionChoiceAgentRequest = {
type: "intentResolutionChoice",
meta: {
requestUuid: uuid(),
timestamp: new Date(),
source
},
payload: {
chosenApp: app,
intent: intent,
}
}
parent.postMessage({ type: "SelectorMessageInitialize" }, "*", [mc.port2]);

socket.emit(FDC3_APP_EVENT, out, source)
function callback(si: SingleAppIntent | null) {
myPort.postMessage({
type: "ResolverMessageChoice",
payload: si
} as ResolverMessageChoice)
}

const debug = document.createElement("p")
debug.textContent = JSON.stringify(source)
document.body.appendChild(debug)

intentDetails.forEach(intent => {

intent.apps.forEach(app => {
const li = document.createElement("li")
const a = document.createElement("a")
const description = document.createElement("em")

if (app.instanceId) {
description.textContent = `${intent.intent.displayName ?? ""} on app instance ${app.instanceId} of ${app.appId}`
} else {
description.textContent = ` ${intent.intent.displayName ?? ""} on a new instance of ${app.appId}`
}

a.textContent = intent.intent.name

li.appendChild(a)
li.appendChild(description)
list.appendChild(li)
a.setAttribute("href", "#")
a.onclick = () => sendChosenIntent(intent.intent, app, source)
})
myPort.addEventListener("message", (e) => {
if (e.data.type == 'ResolverIntents') {
const details = e.data as ResolverIntents
console.log(JSON.stringify("INTENT DETAILS: " + JSON.stringify(details)))

details.appIntents.forEach(intent => {

intent.apps.forEach(app => {
const li = document.createElement("li")
const a = document.createElement("a")
const description = document.createElement("em")

if (app.instanceId) {
description.textContent = `${intent.intent.displayName ?? ""} on app instance ${app.instanceId} of ${app.appId}`
} else {
description.textContent = ` ${intent.intent.displayName ?? ""} on a new instance of ${app.appId}`
}

a.textContent = intent.intent.name

li.appendChild(a)
li.appendChild(description)
list.appendChild(li)
a.setAttribute("href", "#")
a.onclick = () => callback({ intent: intent.intent, chosenApp: app })
})
})
}
})

document.getElementById("cancel")!!.addEventListener("click", () => {
callback(null);
})


})
13 changes: 6 additions & 7 deletions packages/demo/static/da/intent-resolver.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<html lang="en">

<head>
<head>
<title>Desktop Agent Intent Resolver</title>
<meta charset="UTF-8" />
<script type="module" src="/src/client/ui/intent-resolver.ts"></script>
</head>
</head>

<body style="background-color: #ddd;">
<body style="background-color: #ddd">
<h1>Intent Resolution</h1>
<ul id="intent-list"></ul>
</body>

</html>
<button id="cancel">Cancel</button>
</body>
</html>

0 comments on commit 14f9424

Please sign in to comment.