Skip to content

Commit c0591c3

Browse files
committed
1.0.4 - made spiner into ActivityIndicator and part of the API
1 parent f4b41f1 commit c0591c3

File tree

8 files changed

+69
-12
lines changed

8 files changed

+69
-12
lines changed
File renamed without changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Description
2+
3+
This is a more advanced example that generates an image using the DALL-E-3 model. The model is capable of generating images from textual prompts. The prompt is a description of the image you want to generate. The model will generate an image based on the description.
4+
5+
This demonstrates using the OpenAI Client Library directly in javaScript code to interact with the OpenAI API.
6+
7+
# Template Code
8+
9+
```javascript
10+
<%*
11+
const selectedText = tp.file.selection();
12+
if (selectedText === null || selectedText.length === 0) {
13+
new Notice("Select text that will serve as the prompt.", 10000);
14+
return;
15+
}
16+
17+
// More info for parameters to the API: https://platform.openai.com/docs/api-reference/images/create
18+
const model = "dall-e-3";
19+
const size = "1024x1024"; // for dall-e-3: 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models.
20+
const style = "vivid"; // for dall-e-3: vivid or natural
21+
const quality = "standard"; // for dall-e-3: hd or standard
22+
23+
const openai = new ait.OpenAI({
24+
apiKey: ait.defaultClientSettings.defaultApiKey,
25+
dangerouslyAllowBrowser: true
26+
});
27+
28+
const activityIndicator = new ait.ActivityIndicator();
29+
30+
try {
31+
activityIndicator.add();
32+
const imageB64 = await openai.images.generate({
33+
model: model,
34+
prompt: selectedText,
35+
response_format: "b64_json",
36+
size: size,
37+
style: style,
38+
quality: quality
39+
});
40+
const now = new Date();
41+
const dateString = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + '_' + now.getHours() + now.getMinutes() + now.getSeconds();
42+
const fileName = "/dalle_" + dateString + ".jpg";
43+
const imageBuffer = tp.obsidian.base64ToArrayBuffer(imageB64.data[0].b64_json)
44+
app.vault.adapter.writeBinary(fileName, imageBuffer)
45+
activityIndicator.remove();
46+
return `${selectedText}\n\n![${fileName}](${fileName})\n\n`;
47+
} catch (error) {
48+
new Notice("Error: " + error.message, 10000);
49+
activityIndicator.remove();
50+
return selectedText;
51+
}
52+
%>
53+
```

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "ai-templater",
33
"name": "AI for Templater",
4-
"version": "1.0.3",
4+
"version": "1.0.4",
55
"minAppVersion": "1.5.11",
66
"description": "AI Extension for the Templater plugin with the OpenAI Client Library.",
77
"author": "TfTHacker",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openaisdk",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "OpenAI SDK plugin for Obsidian.",
55
"main": "main.js",
66
"scripts": {

src/open_ai/AitApi.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// a global window object named ait for access to this plugin, plus access to the OpenAI client library
22

33
/* eslint-disable import/no-named-as-default, @typescript-eslint/naming-convention -- for OpenAi export */
4+
import ActivityIndicator from 'src/utils/ActivityIndicator';
45
import type AitPlugin from '../main';
56
import type { Settings } from '../settings/settings';
67
import ChatBuilder from './ChatBuilder';
@@ -12,11 +13,12 @@ declare global {
1213
ait?: {
1314
availableModels: () => Promise<string[]>;
1415
chat: (promptOrMessages: string | ChatCompletionMessageParam[]) => Promise<string>;
15-
ChatBuilder: typeof ChatBuilder;
1616
defaultClientSettings: Settings;
1717
plugin: AitPlugin;
18+
ActivityIndicator: typeof ActivityIndicator;
19+
ChatBuilder: typeof ChatBuilder;
20+
OpenAI: typeof OpenAI;
1821
};
19-
OpenAI: typeof OpenAI;
2022
}
2123
}
2224

@@ -27,7 +29,8 @@ export const setupAitApi = (plugin: AitPlugin) => {
2729
chat: plugin.openAiApi.chat,
2830
ChatBuilder: ChatBuilder,
2931
defaultClientSettings: plugin.settings,
30-
plugin: plugin
32+
plugin: plugin,
33+
ActivityIndicator: ActivityIndicator,
34+
OpenAI: OpenAI
3135
};
32-
window.OpenAI = OpenAI;
3336
};

src/templater/InternalModuleAit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { InternalModule } from 'templater-obsidian/src/core/functions/internal_f
77
import type { ModuleName } from 'templater-obsidian/src/editor/TpDocumentation';
88
import type { ChatCompletionMessageParam } from 'openai/resources';
99
import type { TFile } from 'obsidian';
10-
import Spinner from '../utils/Spinner';
10+
import ActivityIndicator from '../utils/ActivityIndicator';
1111

1212
export class InternalModuleAit extends InternalModule {
1313
// @ts-expect-error -- adding internal module to templater
@@ -73,7 +73,7 @@ export class InternalModuleAit extends InternalModule {
7373
apiKey?: string,
7474
organization?: string | null
7575
) => {
76-
const spinner = new Spinner();
76+
const spinner = new ActivityIndicator();
7777
spinner.add();
7878
try {
7979
const result =

src/utils/Spinner.ts renamed to src/utils/ActivityIndicator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// creates and removes a spinner element that is appended to the active workspace leaf's view header
1+
// creates and removes a visual indicator element that is appended to the active workspace leaf's view header
22
// provides user feedback that the plugin is working on a AI task
33

4-
class Spinner {
4+
class ActivityIndicator {
55
private spinner: HTMLDivElement | null = null;
66
private timerId: NodeJS.Timeout | null = null;
77

@@ -44,4 +44,4 @@ class Spinner {
4444
}
4545
}
4646

47-
export default Spinner;
47+
export default ActivityIndicator;

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"1.0.0": "1.5.11",
33
"1.0.1": "1.5.11",
44
"1.0.2": "1.5.11",
5-
"1.0.3": "1.5.11"
5+
"1.0.3": "1.5.11",
6+
"1.0.4": "1.5.11"
67
}

0 commit comments

Comments
 (0)