Skip to content
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

KupOpenAI: handled context, managed disconnect last session, on new connect #1794

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5065,7 +5065,10 @@ export class KupDataTable {
}
icon="open-ai"
onkup-button-click={() =>
this.#kupManager.openAI.show(this.data)
this.#kupManager.openAI.show({
context: this.rootElement.tagName,
dataset: this.data,
})
}
/>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| `rtl` | `rtl` | | `boolean` | `undefined` |
| `scrollNumber` | `scroll-number` | | `number` | `undefined` |
| `svgWidth` | `svg-width` | | `number` | `undefined` |
| `taskGanttRef` | -- | | `HTMLDivElement` | `undefined` |
| `taskListTrueRef` | -- | | `HTMLKupTaskListElement` | `undefined` |
| `taskListWidth` | `task-list-width` | | `number` | `undefined` |


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// export interface KupOpenAiInterfaceCallbacks {
// confirm?: (e?: PointerEvent, anchor?: HTMLElement) => void;
// close?: (e?: PointerEvent, anchor?: HTMLElement) => void;
// }
import { KupDataDataset } from '../kup-data/kup-data-declarations';

export interface KupOpenAIParameters {
context?: string;
dataset?: KupDataDataset;
}

export interface KupOpenAISessionInfo {
context: string;
fileId: string;
threadId: string;
}
41 changes: 24 additions & 17 deletions packages/ketchup/src/managers/kup-openai/kup-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import {
import { KupTextField } from '../../components/kup-text-field/kup-text-field';
import { KupDebugCategory } from '../kup-debug/kup-debug-declarations';
import { KupDom } from '../kup-manager/kup-manager-declarations';
import { KupOpenAISessionInfo } from './kup-openai-declarations';
import {
KupOpenAIParameters,
KupOpenAISessionInfo,
} from './kup-openai-declarations';

const dom: KupDom = document.documentElement as KupDom;

export class KupOpenAI {
card: HTMLKupCardElement = null;
container: HTMLElement;
context = '';
data: KupDataTableDataset = null;
dialog: HTMLKupDialogElement = null;
sessionInfo: KupOpenAISessionInfo = null;
Expand Down Expand Up @@ -104,7 +108,10 @@ export class KupOpenAI {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ data: this.data }),
body: JSON.stringify({
context: this.context,
data: this.data,
}),
});
} catch (e) {
this.#setError(e.message);
Expand All @@ -118,6 +125,7 @@ export class KupOpenAI {
}
const responseJson = await response.json();
this.sessionInfo = {
context: this.context,
fileId: responseJson.fileId,
threadId: responseJson.threadId,
};
Expand All @@ -144,6 +152,7 @@ export class KupOpenAI {
'Content-Type': 'application/json',
},
body: JSON.stringify({
context: this.sessionInfo.context,
fileId: this.sessionInfo.fileId,
threadId: this.sessionInfo.threadId,
}),
Expand All @@ -161,29 +170,23 @@ export class KupOpenAI {
}
}

/**
* Shows the component
* @param data
* @param options
*/
show(data: KupDataTableDataset) {
this.data = data;
async show(parameters: KupOpenAIParameters) {
this.context = parameters.context;
this.data = parameters.dataset;

if (!this.card) {
this.#create();
if (this.card) {
await this.hide();
}
this.#create();
}

/**
* Hides the component.
*/
hide() {
async hide() {
if (this.card) {
this.card.remove();
this.card = null;
this.dialog.remove();
this.dialog = null;
this.#disconnect();
await this.#disconnect();
}
}

Expand Down Expand Up @@ -225,7 +228,10 @@ export class KupOpenAI {
openAI.card.refresh();
}

async chat(disableInteractivity, inputArea: HTMLKupTextFieldElement) {
async chat(
disableInteractivity: (status: boolean) => void,
inputArea: HTMLKupTextFieldElement
) {
const communicate = async (
question: string
): Promise<KupCardBuiltInOpenAIMessages[]> => {
Expand All @@ -245,6 +251,7 @@ export class KupOpenAI {
'Content-Type': 'application/json',
},
body: JSON.stringify({
context: openAI.sessionInfo.context,
fileId: openAI.sessionInfo.fileId,
threadId: openAI.sessionInfo.threadId,
question: question,
Expand Down
Loading