Skip to content

Commit 5d40ac7

Browse files
committed
Refactor worker creation and descriptor handling
1 parent d24d8f4 commit 5d40ac7

File tree

2 files changed

+6
-73
lines changed

2 files changed

+6
-73
lines changed

workers/main.ts

+2-52
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
Client,
1111
defaultValues,
1212
Dm,
13-
sdkVersions,
1413
type Consent,
1514
type Conversation,
1615
type DecodedMessage,
@@ -201,30 +200,7 @@ export class WorkerClient extends Worker {
201200
env,
202201
);
203202

204-
// Extract SDK version from name if it exists (e.g., ivi-100 would use v100)
205-
const nameParts = this.name.split("-");
206-
const versionSuffix =
207-
nameParts.length > 1 ? nameParts[nameParts.length - 1] : null;
208-
209-
// Determine which SDK version to use
210-
let clientClass: typeof Client;
211-
if (
212-
versionSuffix &&
213-
/^\d+$/.test(versionSuffix) &&
214-
`v${versionSuffix}` in sdkVersions
215-
) {
216-
const version = `v${versionSuffix}` as keyof typeof sdkVersions;
217-
console.log(
218-
`[${this.nameId}] Using SDK version ${version} for worker ${this.name}`,
219-
);
220-
clientClass = sdkVersions[version].Client as typeof Client;
221-
} else {
222-
// Default to latest version (v104)
223-
clientClass = sdkVersions.v104.Client;
224-
}
225-
226-
// Create the client with the appropriate version
227-
this.client = await clientClass.create(signer, encryptionKey, {
203+
this.client = await Client.create(signer, encryptionKey, {
228204
dbPath,
229205
env,
230206
loggingLevel: process.env.LOGGING_LEVEL as LogLevel,
@@ -342,18 +318,12 @@ export class WorkerClient extends Worker {
342318
*/
343319
private shouldGenerateGptResponse(message: DecodedMessage): boolean {
344320
if (!this.gptEnabled) return false;
345-
346-
// Get the conversation
347321
const conversation = this.client.conversations.getConversationById(
348322
message.conversationId,
349323
);
350-
351324
// Get the base name without installation ID
352325
const baseName = this.name.split("-")[0].toLowerCase();
353-
354-
// Check if it's a DM - need to handle different SDK versions
355-
const isDm = this.isDmConversation(conversation);
356-
326+
const isDm = conversation instanceof Dm;
357327
return ((message?.contentType?.typeId === "text" &&
358328
message.content.includes(baseName) &&
359329
!message.content.includes("/") &&
@@ -363,26 +333,6 @@ export class WorkerClient extends Worker {
363333
isDm) as boolean;
364334
}
365335

366-
// Add a helper method to check if a conversation is a DM across SDK versions
367-
private isDmConversation(conversation: any): boolean {
368-
// Extract version suffix if it exists
369-
const nameParts = this.name.split("-");
370-
const versionSuffix =
371-
nameParts.length > 1 ? nameParts[nameParts.length - 1] : null;
372-
373-
if (
374-
versionSuffix &&
375-
/^\d+$/.test(versionSuffix) &&
376-
`v${versionSuffix}` in sdkVersions
377-
) {
378-
const version = `v${versionSuffix}` as keyof typeof sdkVersions;
379-
return conversation instanceof sdkVersions[version].Dm;
380-
}
381-
382-
// Default to latest version
383-
return conversation instanceof sdkVersions.v104.Dm;
384-
}
385-
386336
/**
387337
* Handle generating and sending GPT responses
388338
*/

workers/manager.ts

+4-21
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,13 @@ export class WorkerManager {
229229
? descriptor
230230
: `${baseName}-${installationId}`;
231231

232-
// Determine if the descriptor contains a version indicator
233-
const versionMatch = providedInstallId?.match(/^(\d+)$/);
234-
const isVersioned = !!versionMatch;
235-
236-
// If the descriptor contains a version but not a folder, add one
237-
const finalDescriptor = isVersioned
238-
? `${baseName}-${providedInstallId}-${installationId}`
239-
: fullDescriptor;
240-
241-
// For version-specific workers, adjust the name vs folder
242-
const workerName = isVersioned
243-
? `${baseName}-${providedInstallId}`
244-
: baseName;
245-
const workerFolder = isVersioned
246-
? installationId
247-
: providedInstallId || installationId;
248-
249232
// Get or generate keys
250-
const { walletKey, encryptionKey } = this.ensureKeys(workerName);
233+
const { walletKey, encryptionKey } = this.ensureKeys(fullDescriptor);
251234

252235
// Create the base worker data
253236
const workerData: WorkerBase = {
254-
name: workerName,
255-
folder: workerFolder,
237+
name: baseName,
238+
folder: installationId,
256239
testName: this.testName,
257240
walletKey,
258241
encryptionKey,
@@ -281,7 +264,7 @@ export class WorkerManager {
281264
// Store the new worker for potential cleanup later
282265
this.activeWorkers.push(workerClient);
283266

284-
// Add to our internal storage - still use baseName for organization
267+
// Add to our internal storage
285268
this.addWorker(baseName, installationId, worker);
286269

287270
return worker;

0 commit comments

Comments
 (0)