@picteus/extension-sdk 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +3 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +2 -0
  4. package/dist/picteus-ws-client/apis/AdministrationApi.d.ts +48 -0
  5. package/dist/picteus-ws-client/apis/AdministrationApi.js +45 -0
  6. package/dist/picteus-ws-client/apis/ExperimentApi.d.ts +48 -0
  7. package/dist/picteus-ws-client/apis/ExperimentApi.js +51 -0
  8. package/dist/picteus-ws-client/apis/ExtensionApi.d.ts +410 -0
  9. package/dist/picteus-ws-client/apis/ExtensionApi.js +482 -0
  10. package/dist/picteus-ws-client/apis/ImageApi.d.ts +593 -0
  11. package/dist/picteus-ws-client/apis/ImageApi.js +729 -0
  12. package/dist/picteus-ws-client/apis/ImageAttachmentApi.d.ts +85 -0
  13. package/dist/picteus-ws-client/apis/ImageAttachmentApi.js +105 -0
  14. package/dist/picteus-ws-client/apis/PingApi.d.ts +48 -0
  15. package/dist/picteus-ws-client/apis/PingApi.js +51 -0
  16. package/dist/picteus-ws-client/apis/RepositoryApi.d.ts +385 -0
  17. package/dist/picteus-ws-client/apis/RepositoryApi.js +462 -0
  18. package/dist/picteus-ws-client/apis/SettingsApi.d.ts +76 -0
  19. package/dist/picteus-ws-client/apis/SettingsApi.js +80 -0
  20. package/dist/picteus-ws-client/apis/index.d.ts +8 -0
  21. package/dist/picteus-ws-client/apis/index.js +10 -0
  22. package/dist/picteus-ws-client/index.d.ts +3 -0
  23. package/dist/picteus-ws-client/index.js +5 -0
  24. package/dist/picteus-ws-client/models/Automatic1111Instruction.d.ts +38 -0
  25. package/dist/picteus-ws-client/models/Automatic1111Instruction.js +47 -0
  26. package/dist/picteus-ws-client/models/Automatic1111UserComment.d.ts +45 -0
  27. package/dist/picteus-ws-client/models/Automatic1111UserComment.js +50 -0
  28. package/dist/picteus-ws-client/models/ComfyUiPromptAndWorkflow.d.ts +38 -0
  29. package/dist/picteus-ws-client/models/ComfyUiPromptAndWorkflow.js +47 -0
  30. package/dist/picteus-ws-client/models/CommandEntity.d.ts +26 -0
  31. package/dist/picteus-ws-client/models/CommandEntity.js +44 -0
  32. package/dist/picteus-ws-client/models/ConfigurationCapability.d.ts +39 -0
  33. package/dist/picteus-ws-client/models/ConfigurationCapability.js +48 -0
  34. package/dist/picteus-ws-client/models/ConfigurationExtensionCommand.d.ts +39 -0
  35. package/dist/picteus-ws-client/models/ConfigurationExtensionCommand.js +48 -0
  36. package/dist/picteus-ws-client/models/Dates.d.ts +38 -0
  37. package/dist/picteus-ws-client/models/Dates.js +47 -0
  38. package/dist/picteus-ws-client/models/Extension.d.ts +40 -0
  39. package/dist/picteus-ws-client/models/Extension.js +49 -0
  40. package/dist/picteus-ws-client/models/ExtensionActivity.d.ts +39 -0
  41. package/dist/picteus-ws-client/models/ExtensionActivity.js +48 -0
  42. package/dist/picteus-ws-client/models/ExtensionActivityKind.d.ts +26 -0
  43. package/dist/picteus-ws-client/models/ExtensionActivityKind.js +44 -0
  44. package/dist/picteus-ws-client/models/ExtensionGenerationOptions.d.ts +63 -0
  45. package/dist/picteus-ws-client/models/ExtensionGenerationOptions.js +64 -0
  46. package/dist/picteus-ws-client/models/ExtensionImageEmbeddings.d.ts +38 -0
  47. package/dist/picteus-ws-client/models/ExtensionImageEmbeddings.js +47 -0
  48. package/dist/picteus-ws-client/models/ExtensionImageFeature.d.ts +58 -0
  49. package/dist/picteus-ws-client/models/ExtensionImageFeature.js +59 -0
  50. package/dist/picteus-ws-client/models/ExtensionImageTag.d.ts +38 -0
  51. package/dist/picteus-ws-client/models/ExtensionImageTag.js +47 -0
  52. package/dist/picteus-ws-client/models/ExtensionSettings.d.ts +32 -0
  53. package/dist/picteus-ws-client/models/ExtensionSettings.js +43 -0
  54. package/dist/picteus-ws-client/models/ExtensionStatus.d.ts +25 -0
  55. package/dist/picteus-ws-client/models/ExtensionStatus.js +43 -0
  56. package/dist/picteus-ws-client/models/ExtensionsConfiguration.d.ts +40 -0
  57. package/dist/picteus-ws-client/models/ExtensionsConfiguration.js +49 -0
  58. package/dist/picteus-ws-client/models/Image.d.ts +135 -0
  59. package/dist/picteus-ws-client/models/Image.js +110 -0
  60. package/dist/picteus-ws-client/models/ImageDimensions.d.ts +38 -0
  61. package/dist/picteus-ws-client/models/ImageDimensions.js +47 -0
  62. package/dist/picteus-ws-client/models/ImageDistance.d.ts +39 -0
  63. package/dist/picteus-ws-client/models/ImageDistance.js +48 -0
  64. package/dist/picteus-ws-client/models/ImageEmbeddings.d.ts +32 -0
  65. package/dist/picteus-ws-client/models/ImageEmbeddings.js +43 -0
  66. package/dist/picteus-ws-client/models/ImageFeature.d.ts +52 -0
  67. package/dist/picteus-ws-client/models/ImageFeature.js +55 -0
  68. package/dist/picteus-ws-client/models/ImageFeatureFormat.d.ts +29 -0
  69. package/dist/picteus-ws-client/models/ImageFeatureFormat.js +47 -0
  70. package/dist/picteus-ws-client/models/ImageFeatureType.d.ts +27 -0
  71. package/dist/picteus-ws-client/models/ImageFeatureType.js +45 -0
  72. package/dist/picteus-ws-client/models/ImageFormat.d.ts +29 -0
  73. package/dist/picteus-ws-client/models/ImageFormat.js +47 -0
  74. package/dist/picteus-ws-client/models/ImageGenerator.d.ts +26 -0
  75. package/dist/picteus-ws-client/models/ImageGenerator.js +44 -0
  76. package/dist/picteus-ws-client/models/ImageMetadata.d.ts +68 -0
  77. package/dist/picteus-ws-client/models/ImageMetadata.js +53 -0
  78. package/dist/picteus-ws-client/models/ImageSummary.d.ts +114 -0
  79. package/dist/picteus-ws-client/models/ImageSummary.js +95 -0
  80. package/dist/picteus-ws-client/models/ImageSummaryList.d.ts +39 -0
  81. package/dist/picteus-ws-client/models/ImageSummaryList.js +48 -0
  82. package/dist/picteus-ws-client/models/Manifest.d.ts +77 -0
  83. package/dist/picteus-ws-client/models/Manifest.js +72 -0
  84. package/dist/picteus-ws-client/models/ManifestCapability.d.ts +33 -0
  85. package/dist/picteus-ws-client/models/ManifestCapability.js +44 -0
  86. package/dist/picteus-ws-client/models/ManifestCapabilityId.d.ts +27 -0
  87. package/dist/picteus-ws-client/models/ManifestCapabilityId.js +45 -0
  88. package/dist/picteus-ws-client/models/ManifestEvent.d.ts +33 -0
  89. package/dist/picteus-ws-client/models/ManifestEvent.js +51 -0
  90. package/dist/picteus-ws-client/models/ManifestExecution.d.ts +38 -0
  91. package/dist/picteus-ws-client/models/ManifestExecution.js +47 -0
  92. package/dist/picteus-ws-client/models/ManifestExtensionCommand.d.ts +52 -0
  93. package/dist/picteus-ws-client/models/ManifestExtensionCommand.js +57 -0
  94. package/dist/picteus-ws-client/models/ManifestExtensionCommandLabel.d.ts +38 -0
  95. package/dist/picteus-ws-client/models/ManifestExtensionCommandLabel.js +47 -0
  96. package/dist/picteus-ws-client/models/ManifestExtensionCommandOn.d.ts +39 -0
  97. package/dist/picteus-ws-client/models/ManifestExtensionCommandOn.js +46 -0
  98. package/dist/picteus-ws-client/models/ManifestInstructions.d.ts +54 -0
  99. package/dist/picteus-ws-client/models/ManifestInstructions.js +55 -0
  100. package/dist/picteus-ws-client/models/ManifestInterfaceElement.d.ts +39 -0
  101. package/dist/picteus-ws-client/models/ManifestInterfaceElement.js +48 -0
  102. package/dist/picteus-ws-client/models/ManifestRuntime.d.ts +33 -0
  103. package/dist/picteus-ws-client/models/ManifestRuntime.js +44 -0
  104. package/dist/picteus-ws-client/models/ManifestRuntimeEnvironment.d.ts +25 -0
  105. package/dist/picteus-ws-client/models/ManifestRuntimeEnvironment.js +43 -0
  106. package/dist/picteus-ws-client/models/ManifestUserInterface.d.ts +33 -0
  107. package/dist/picteus-ws-client/models/ManifestUserInterface.js +44 -0
  108. package/dist/picteus-ws-client/models/MidjourneyInstructions.d.ts +74 -0
  109. package/dist/picteus-ws-client/models/MidjourneyInstructions.js +71 -0
  110. package/dist/picteus-ws-client/models/Repository.d.ts +82 -0
  111. package/dist/picteus-ws-client/models/Repository.js +73 -0
  112. package/dist/picteus-ws-client/models/RepositoryActivity.d.ts +39 -0
  113. package/dist/picteus-ws-client/models/RepositoryActivity.js +48 -0
  114. package/dist/picteus-ws-client/models/RepositoryActivityKind.d.ts +26 -0
  115. package/dist/picteus-ws-client/models/RepositoryActivityKind.js +44 -0
  116. package/dist/picteus-ws-client/models/RepositoryLocationType.d.ts +24 -0
  117. package/dist/picteus-ws-client/models/RepositoryLocationType.js +42 -0
  118. package/dist/picteus-ws-client/models/RepositoryStatus.d.ts +27 -0
  119. package/dist/picteus-ws-client/models/RepositoryStatus.js +45 -0
  120. package/dist/picteus-ws-client/models/SearchCriteria.d.ts +47 -0
  121. package/dist/picteus-ws-client/models/SearchCriteria.js +48 -0
  122. package/dist/picteus-ws-client/models/SearchKeyword.d.ts +50 -0
  123. package/dist/picteus-ws-client/models/SearchKeyword.js +55 -0
  124. package/dist/picteus-ws-client/models/SearchRange.d.ts +38 -0
  125. package/dist/picteus-ws-client/models/SearchRange.js +43 -0
  126. package/dist/picteus-ws-client/models/SearchSorting.d.ts +39 -0
  127. package/dist/picteus-ws-client/models/SearchSorting.js +46 -0
  128. package/dist/picteus-ws-client/models/SearchSortingProperty.d.ts +29 -0
  129. package/dist/picteus-ws-client/models/SearchSortingProperty.js +47 -0
  130. package/dist/picteus-ws-client/models/Settings.d.ts +32 -0
  131. package/dist/picteus-ws-client/models/Settings.js +41 -0
  132. package/dist/picteus-ws-client/models/UserInterfaceAnchor.d.ts +26 -0
  133. package/dist/picteus-ws-client/models/UserInterfaceAnchor.js +44 -0
  134. package/dist/picteus-ws-client/models/index.d.ts +55 -0
  135. package/dist/picteus-ws-client/models/index.js +57 -0
  136. package/dist/picteus-ws-client/runtime.d.ts +184 -0
  137. package/dist/picteus-ws-client/runtime.js +333 -0
  138. package/dist/picteusExtension.d.ts +145 -0
  139. package/dist/picteusExtension.js +302 -0
  140. package/package.json +53 -0
@@ -0,0 +1,145 @@
1
+ import { Socket } from "socket.io-client";
2
+ import { Configuration, ImageApi, ImageAttachmentApi, Json, RepositoryApi } from "./index.js";
3
+ export type NotificationValue = Record<string, any>;
4
+ export declare enum NotificationReturnedErrorCause {
5
+ Cancel = 0,
6
+ Error = 1
7
+ }
8
+ export declare class NotificationReturnedError extends Error {
9
+ readonly reason: NotificationReturnedErrorCause;
10
+ constructor(message: string, reason: NotificationReturnedErrorCause);
11
+ }
12
+ export interface NotificationsParametersIntent {
13
+ readonly parameters: Json;
14
+ }
15
+ export declare enum NotificationsUiAnchor {
16
+ Modal = "modal",
17
+ Sidebar = "sidebar",
18
+ ImageDetail = "imageDetail"
19
+ }
20
+ export interface NotificationsUi {
21
+ readonly anchor: NotificationsUiAnchor;
22
+ readonly url: string;
23
+ }
24
+ export interface NotificationsUiIntent {
25
+ readonly ui: NotificationsUi;
26
+ }
27
+ export declare enum NotificationsDialogType {
28
+ Error = "Error",
29
+ Info = "Info",
30
+ Question = "Question"
31
+ }
32
+ export interface NotificationsDialog {
33
+ readonly type: NotificationsDialogType;
34
+ readonly title: string;
35
+ readonly description: string;
36
+ readonly details?: string;
37
+ readonly buttons: {
38
+ yes: string;
39
+ no?: string;
40
+ };
41
+ }
42
+ export interface NotificationsDialogIntent {
43
+ readonly dialog: NotificationsDialog;
44
+ }
45
+ export interface NotificationsImage {
46
+ readonly imageId: string;
47
+ readonly title?: string;
48
+ readonly description?: string;
49
+ readonly details?: string;
50
+ }
51
+ export interface NotificationsImages {
52
+ readonly images: NotificationsImage[];
53
+ readonly title?: string;
54
+ readonly description?: string;
55
+ readonly details?: string;
56
+ }
57
+ export interface NotificationsImagesIntent {
58
+ readonly images: NotificationsImages;
59
+ }
60
+ export declare enum NotificationsShowType {
61
+ ExtensionSettings = "ExtensionSettings",
62
+ Image = "Image",
63
+ Repository = "Repository"
64
+ }
65
+ export interface NotificationsShow {
66
+ readonly type: NotificationsShowType;
67
+ readonly id: string;
68
+ }
69
+ export interface NotificationsShowIntent {
70
+ readonly show: NotificationsShow;
71
+ }
72
+ export type NotificationsIntent = NotificationsParametersIntent | NotificationsUiIntent | NotificationsDialogIntent | NotificationsImagesIntent | NotificationsShowIntent;
73
+ export declare enum NotificationEvent {
74
+ ProcessRunCommand = "process.runCommand",
75
+ ImageCreated = "image.created",
76
+ ImageUpdated = "image.updated",
77
+ ImageDeleted = "image.deleted",
78
+ ImageComputeFeatures = "image.computeFeatures",
79
+ ImageComputeEmbeddings = "image.computeEmbeddings",
80
+ ImageComputeTags = "image.computeTags",
81
+ ImageRunCommand = "image.runCommand",
82
+ TextComputeEmbeddings = "text.computeEmbeddings"
83
+ }
84
+ interface LeveledLogMethod {
85
+ (message: string, ...meta: any[]): Logger;
86
+ (message: any): Logger;
87
+ (infoObject: object): Logger;
88
+ }
89
+ export declare class Logger {
90
+ readonly debug: LeveledLogMethod;
91
+ readonly info: LeveledLogMethod;
92
+ readonly warn: LeveledLogMethod;
93
+ readonly error: LeveledLogMethod;
94
+ }
95
+ declare class ExtensionParameters {
96
+ readonly apiKey: string;
97
+ readonly extensionId: string;
98
+ readonly webServicesBaseUrl: string;
99
+ constructor(parameters: Record<string, any>);
100
+ }
101
+ declare class MessageSender {
102
+ private readonly logger;
103
+ private readonly parameters;
104
+ private readonly socket;
105
+ private readonly toString;
106
+ private readonly contextId?;
107
+ constructor(logger: Logger, parameters: ExtensionParameters, socket: Socket, toString: () => string, contextId?: string);
108
+ sendMessage(channel: string, body: Record<string, any>, callback?: (result: any) => Promise<void>): void;
109
+ }
110
+ export declare class Communicator {
111
+ private readonly logger;
112
+ private readonly sender;
113
+ constructor(logger: Logger, sender: MessageSender);
114
+ sendLog(message: string, level: "debug" | "info" | "warn" | "error"): void;
115
+ sendNotification(value: Record<string, any>): void;
116
+ launchIntent<T>(intent: NotificationsIntent): Promise<T>;
117
+ private sendMessage;
118
+ }
119
+ export declare class PicteusExtension {
120
+ static getSdkVersion(): string;
121
+ protected readonly logger: Logger;
122
+ protected readonly parameters: ExtensionParameters;
123
+ protected readonly extensionId: string;
124
+ protected readonly webServicesBaseUrl: string;
125
+ protected readonly apiKey: string;
126
+ protected readonly configuration: Configuration;
127
+ private socket?;
128
+ private globalCommunicator?;
129
+ constructor();
130
+ run(): Promise<void>;
131
+ protected toString(): string;
132
+ protected initialize(): Promise<boolean>;
133
+ protected onReady(communicator?: Communicator): Promise<void>;
134
+ protected onTerminate(): Promise<void>;
135
+ protected onEvent(communicator: Communicator, event: string, value: NotificationValue): Promise<any>;
136
+ protected getRepositoryApi(): RepositoryApi;
137
+ protected getImageApi(): ImageApi;
138
+ protected getImageAttachmentApi(): ImageAttachmentApi;
139
+ protected getSettings(): Promise<Record<string, any>>;
140
+ private getParameters;
141
+ private connectSocket;
142
+ private disconnectSocket;
143
+ private getApiConfiguration;
144
+ }
145
+ export {};
@@ -0,0 +1,302 @@
1
+ import path, { dirname } from "node:path";
2
+ import fs from "node:fs";
3
+ import { fileURLToPath } from "node:url";
4
+ // We do not import the "process" package on purpose, otherwise we get the runtime error "process.on is not a function"
5
+ import console from "node:console";
6
+ import { io } from "socket.io-client";
7
+ import { Configuration, ExtensionApi, ImageApi, ImageAttachmentApi, RepositoryApi } from "./index.js";
8
+ export var NotificationReturnedErrorCause;
9
+ (function (NotificationReturnedErrorCause) {
10
+ NotificationReturnedErrorCause[NotificationReturnedErrorCause["Cancel"] = 0] = "Cancel";
11
+ NotificationReturnedErrorCause[NotificationReturnedErrorCause["Error"] = 1] = "Error";
12
+ })(NotificationReturnedErrorCause || (NotificationReturnedErrorCause = {}));
13
+ export class NotificationReturnedError extends Error {
14
+ reason;
15
+ constructor(message, reason) {
16
+ super(message);
17
+ this.reason = reason;
18
+ }
19
+ }
20
+ export var NotificationsUiAnchor;
21
+ (function (NotificationsUiAnchor) {
22
+ // noinspection JSUnusedGlobalSymbols
23
+ NotificationsUiAnchor["Modal"] = "modal";
24
+ NotificationsUiAnchor["Sidebar"] = "sidebar";
25
+ NotificationsUiAnchor["ImageDetail"] = "imageDetail";
26
+ })(NotificationsUiAnchor || (NotificationsUiAnchor = {}));
27
+ export var NotificationsDialogType;
28
+ (function (NotificationsDialogType) {
29
+ NotificationsDialogType["Error"] = "Error";
30
+ NotificationsDialogType["Info"] = "Info";
31
+ NotificationsDialogType["Question"] = "Question";
32
+ })(NotificationsDialogType || (NotificationsDialogType = {}));
33
+ export var NotificationsShowType;
34
+ (function (NotificationsShowType) {
35
+ NotificationsShowType["ExtensionSettings"] = "ExtensionSettings";
36
+ NotificationsShowType["Image"] = "Image";
37
+ NotificationsShowType["Repository"] = "Repository";
38
+ })(NotificationsShowType || (NotificationsShowType = {}));
39
+ export var NotificationEvent;
40
+ (function (NotificationEvent) {
41
+ NotificationEvent["ProcessRunCommand"] = "process.runCommand";
42
+ NotificationEvent["ImageCreated"] = "image.created";
43
+ NotificationEvent["ImageUpdated"] = "image.updated";
44
+ NotificationEvent["ImageDeleted"] = "image.deleted";
45
+ NotificationEvent["ImageComputeFeatures"] = "image.computeFeatures";
46
+ NotificationEvent["ImageComputeEmbeddings"] = "image.computeEmbeddings";
47
+ NotificationEvent["ImageComputeTags"] = "image.computeTags";
48
+ NotificationEvent["ImageRunCommand"] = "image.runCommand";
49
+ NotificationEvent["TextComputeEmbeddings"] = "text.computeEmbeddings";
50
+ })(NotificationEvent || (NotificationEvent = {}));
51
+ const dateToString = (date) => {
52
+ function padNumber(number, length) {
53
+ return number.toString().padStart(length, "0");
54
+ }
55
+ return `${padNumber(date.getHours(), 2)}:${padNumber(date.getMinutes(), 2)}:${padNumber(date.getSeconds(), 2)}.${padNumber(date.getMilliseconds(), 3)}`;
56
+ };
57
+ const computeLeveledLogMethod = (logger, level) => {
58
+ return function (message, ...meta) {
59
+ // @ts-ignore
60
+ const consoleFunction = console[level];
61
+ const prefix = `${dateToString(new Date())} | ${process.pid} | main [${level.toUpperCase().padStart(5, " ")}]`;
62
+ consoleFunction(`${prefix}: ${message}`, ...meta);
63
+ return logger;
64
+ };
65
+ };
66
+ export class Logger {
67
+ debug = computeLeveledLogMethod(this, "debug");
68
+ info = computeLeveledLogMethod(this, "info");
69
+ warn = computeLeveledLogMethod(this, "warn");
70
+ error = computeLeveledLogMethod(this, "error");
71
+ }
72
+ class ExtensionParameters {
73
+ apiKey;
74
+ extensionId;
75
+ webServicesBaseUrl;
76
+ constructor(parameters) {
77
+ this.apiKey = parameters.apiKey;
78
+ this.extensionId = parameters.extensionId;
79
+ this.webServicesBaseUrl = parameters.webServicesBaseUrl;
80
+ }
81
+ }
82
+ class MessageSender {
83
+ logger;
84
+ parameters;
85
+ socket;
86
+ toString;
87
+ contextId;
88
+ constructor(logger, parameters, socket, toString, contextId) {
89
+ this.logger = logger;
90
+ this.parameters = parameters;
91
+ this.socket = socket;
92
+ this.toString = toString;
93
+ this.contextId = contextId;
94
+ }
95
+ sendMessage(channel, body, callback) {
96
+ const contextId = this.contextId;
97
+ this.logger.debug(`Sending the message '${JSON.stringify(body)}' on channel '${channel}' for ${this.toString()}${contextId === undefined ? "" : ` attached to the context with id '${contextId}'`}${callback === undefined ? "" : " and waiting for a callback"}`);
98
+ const value = {
99
+ apiKey: this.parameters.apiKey,
100
+ extensionId: this.parameters.extensionId,
101
+ ...body
102
+ };
103
+ if (contextId !== undefined) {
104
+ value.contextId = contextId;
105
+ }
106
+ if (callback === undefined) {
107
+ this.socket.emit(channel, value);
108
+ }
109
+ else {
110
+ this.socket.emit(channel, value, callback);
111
+ }
112
+ }
113
+ }
114
+ const notificationsChannel = "notifications";
115
+ export class Communicator {
116
+ logger;
117
+ sender;
118
+ constructor(logger, sender) {
119
+ this.logger = logger;
120
+ this.sender = sender;
121
+ }
122
+ sendLog(message, level) {
123
+ this.logger[level](message);
124
+ this.sendMessage(notificationsChannel, { log: { message, level } });
125
+ }
126
+ sendNotification(value) {
127
+ this.sendMessage(notificationsChannel, { notification: value });
128
+ }
129
+ async launchIntent(intent) {
130
+ return await new Promise((resolve, reject) => {
131
+ this.sendMessage(notificationsChannel, { intent }, async ({ value, cancel, error }) => {
132
+ this.logger.debug(`Received a result related to the intent '${JSON.stringify(intent)}' for ${this.toString()}`);
133
+ if (cancel !== undefined) {
134
+ reject(new NotificationReturnedError(cancel, NotificationReturnedErrorCause.Cancel));
135
+ }
136
+ else if (error !== undefined) {
137
+ reject(new NotificationReturnedError(error, NotificationReturnedErrorCause.Error));
138
+ }
139
+ else {
140
+ resolve(value);
141
+ }
142
+ });
143
+ });
144
+ }
145
+ sendMessage(channel, body, callback) {
146
+ this.sender.sendMessage(channel, body, callback);
147
+ }
148
+ }
149
+ export class PicteusExtension {
150
+ static getSdkVersion() {
151
+ return JSON.parse(fs.readFileSync(path.join(dirname(fileURLToPath(import.meta.url)), "..", "package.json"), { encoding: "utf8" })).version;
152
+ }
153
+ logger = new Logger();
154
+ parameters;
155
+ extensionId;
156
+ webServicesBaseUrl;
157
+ apiKey;
158
+ configuration;
159
+ socket;
160
+ globalCommunicator;
161
+ constructor() {
162
+ this.logger.info(`Instantiating the ${this.toString()} through the process with id '${process.pid}' relying on the SDK version '${PicteusExtension.getSdkVersion()}'`);
163
+ process.on("SIGTERM", async (signal) => {
164
+ this.logger.info(`Received the termination signal '${signal}' regarding the ${this.toString()}`);
165
+ try {
166
+ await this.onTerminate();
167
+ }
168
+ finally {
169
+ this.disconnectSocket();
170
+ this.logger.info(`Exiting from the ${this.toString()}`);
171
+ process.exit(0);
172
+ }
173
+ });
174
+ this.parameters = new ExtensionParameters(this.getParameters());
175
+ this.extensionId = this.parameters.extensionId;
176
+ this.webServicesBaseUrl = this.parameters.webServicesBaseUrl;
177
+ this.apiKey = this.parameters.apiKey;
178
+ this.configuration = this.getApiConfiguration();
179
+ }
180
+ async run() {
181
+ this.logger.info(`Running the ${(this.toString())}`);
182
+ let result;
183
+ try {
184
+ result = await this.initialize();
185
+ }
186
+ catch (error) {
187
+ this.logger.error(`The initialization of the ${this.toString()} failed. Reason: '${error.message}'`, error);
188
+ process.exit(1);
189
+ }
190
+ try {
191
+ if (result === true) {
192
+ try {
193
+ this.connectSocket();
194
+ }
195
+ catch (error) {
196
+ this.logger.error(`The connection of the ${this.toString()} failed. Reason: '${error.message}'`, error);
197
+ process.exit(2);
198
+ }
199
+ }
200
+ else {
201
+ await this.onReady();
202
+ }
203
+ }
204
+ finally {
205
+ this.logger.info(`The ${this.toString()} is over`);
206
+ }
207
+ }
208
+ toString() {
209
+ return `extension` + (this.extensionId === undefined ? "" : ` with id '${this.extensionId}'`) + ` of class '${this.constructor.name}'`;
210
+ }
211
+ async initialize() {
212
+ return true;
213
+ }
214
+ async onReady(communicator) {
215
+ }
216
+ async onTerminate() {
217
+ }
218
+ // noinspection JSUnusedLocalSymbols
219
+ async onEvent(communicator, event, value) {
220
+ }
221
+ getRepositoryApi() {
222
+ return new RepositoryApi(this.configuration);
223
+ }
224
+ getImageApi() {
225
+ return new ImageApi(this.configuration);
226
+ }
227
+ getImageAttachmentApi() {
228
+ return new ImageAttachmentApi(this.configuration);
229
+ }
230
+ async getSettings() {
231
+ return (await new ExtensionApi(this.configuration).extensionGetSettings({ id: this.extensionId })).value;
232
+ }
233
+ getParameters() {
234
+ return JSON.parse(fs.readFileSync(path.join(".", "parameters.json"), { encoding: "utf8" }));
235
+ }
236
+ connectSocket() {
237
+ this.logger.info(`Connecting the ${this.toString()} to the server`);
238
+ const options = {
239
+ autoConnect: true,
240
+ transports: ["websocket"],
241
+ rejectUnauthorized: false
242
+ };
243
+ this.socket = io(this.parameters.webServicesBaseUrl, options);
244
+ const globalSender = new MessageSender(this.logger, this.parameters, this.socket, () => {
245
+ return this.toString();
246
+ });
247
+ this.globalCommunicator = new Communicator(this.logger, globalSender);
248
+ this.socket.on("connect", async () => {
249
+ this.logger.info(`The ${this.toString()} socket is connected`);
250
+ await this.onReady(this.globalCommunicator);
251
+ });
252
+ this.socket.on("connect_error", async () => {
253
+ this.logger.warn(`The ${this.toString()} socket connection failed"`);
254
+ });
255
+ this.socket.on("disconnect", async () => {
256
+ this.logger.info(`The ${this.toString()} socket is disconnected`);
257
+ });
258
+ this.socket.on("events", async (command, onResult) => {
259
+ const { channel, contextId, milliseconds, value } = command;
260
+ this.logger.info(`The ${this.toString()} received at ${dateToString(new Date(milliseconds))} the command '${JSON.stringify(command)}' on channel '${channel}' attached to the context with id '${contextId}'`);
261
+ const sender = new MessageSender(this.logger, this.parameters, this.socket, () => {
262
+ return this.toString();
263
+ }, contextId);
264
+ const communicator = new Communicator(this.logger, sender);
265
+ let result;
266
+ let success = false;
267
+ try {
268
+ result = await this.onEvent(communicator, channel, value);
269
+ success = true;
270
+ }
271
+ catch (error) {
272
+ // We want the process to continue even if an exception occurs
273
+ this.logger.error(`An error occurred during the handling of the event on channel '${channel}'`, error);
274
+ communicator.sendLog(`The handling of the event failed for the ${this.toString()}. Reason: '${error.message}'`, "error");
275
+ return;
276
+ }
277
+ finally {
278
+ sender.sendMessage(notificationsChannel, { acknowledgment: { success } });
279
+ }
280
+ if (result !== undefined && onResult !== undefined) {
281
+ onResult(result);
282
+ }
283
+ });
284
+ globalSender.sendMessage("connection", {
285
+ isOpen: true,
286
+ sdkVersion: PicteusExtension.getSdkVersion(),
287
+ environment: "node"
288
+ });
289
+ }
290
+ disconnectSocket() {
291
+ if (this.socket !== undefined && this.socket.disconnected === false) {
292
+ this.logger.info(`Disconnecting the ${this.toString()} from the server`);
293
+ this.socket.close();
294
+ this.socket = undefined;
295
+ }
296
+ }
297
+ getApiConfiguration() {
298
+ // This enables to discard the warning log message, caused by the fact that we are issuing HTTPS request on the localhost exposing a self-served certificate
299
+ process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
300
+ return new Configuration({ basePath: this.webServicesBaseUrl, apiKey: this.apiKey });
301
+ }
302
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@picteus/extension-sdk",
3
+ "version": "0.2.5",
4
+ "productName": "Picteus TypeScript SDK",
5
+ "description": "The Picteus extension TypeScript SDK.",
6
+ "keywords": [
7
+ "picteus",
8
+ "extension",
9
+ "sdk"
10
+ ],
11
+ "author": {
12
+ "name": "Édouard Mercier",
13
+ "email": "edouard@koppasoft.com",
14
+ "url": "https://github.com/edouardmercier"
15
+ },
16
+ "license": "Copyright (C) 2024 KoppaSoft. The code hereby is the private full property of the KoppaSoft company, Paris, France. You have no right to re-use or modify it. There are no open-source, nor free license attached to it!",
17
+ "private": false,
18
+ "engines": {
19
+ "node": ">=20"
20
+ },
21
+ "homepage": "https://github.com/picteus/Picteus.git",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/picteus/Picteus.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/picteus/Picteus/issues"
28
+ },
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org",
31
+ "access": "public"
32
+ },
33
+ "type": "module",
34
+ "main": "./dist/index.js",
35
+ "module": "./dist/index.js",
36
+ "types": "./dist/index.d.ts",
37
+ "files": [
38
+ "./dist"
39
+ ],
40
+ "scripts": {
41
+ "compile": "tsc && tsc-alias",
42
+ "build": "tsup src/index.ts --tsconfig tsconfig.json --format cjs,esm --dts --clean --minify-whitespace --minify-identifiers --minify-syntax"
43
+ },
44
+ "devDependencies": {
45
+ "typescript": "5.9.2",
46
+ "@types/node": "20.19.0",
47
+ "tsc-alias": "1.8.16",
48
+ "tsup": "8.5.0"
49
+ },
50
+ "dependencies": {
51
+ "socket.io-client": "4.8.1"
52
+ }
53
+ }