Skip to content

Commit

Permalink
Extend Content and Chat classes to include email and createdAt. (#19)
Browse files Browse the repository at this point in the history
* Load createdAt and userEmail when looking up chats

* Load createdAt and userEmail when looking up content
  • Loading branch information
jmoseley authored Jun 29, 2024
1 parent df1f447 commit d7089a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
24 changes: 21 additions & 3 deletions chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class Chat {
readonly id: string,
readonly title: string,
readonly messages: Message[],
readonly createdAt: string,
readonly userEmail?: string,
) {}

static async create(opts: CreateChatOptsSync): Promise<Chat>;
Expand Down Expand Up @@ -102,7 +104,14 @@ export class Chat {
message: body.response,
},
];
return new Chat(client, body.id, body.title, messages);
return new Chat(
client,
body.id,
body.title,
messages,
body.createdAt,
body.userEmail,
);
}

private static async createContentStreaming(
Expand All @@ -119,6 +128,8 @@ export class Chat {

const id: string = res.headers.get("id") || "";
const title: string = res.headers.get("title") || "";
const createdAt: string = res.headers.get("createdAt") || "";
const userEmail = res.headers.get("userEmail") || undefined;

const readableStream = new Readable({
read() {},
Expand All @@ -140,7 +151,7 @@ export class Chat {
message: content,
},
];
return new Chat(client, id, title, messages);
return new Chat(client, id, title, messages, createdAt, userEmail);
});

return { responseStream: readableStream, chat: chatPromise };
Expand All @@ -152,7 +163,14 @@ export class Chat {
throw new Error(`Failed to get chat: ${res.statusText}`);
}
const body = await res.json();
return new Chat(client, id, body.title, body.messages);
return new Chat(
client,
id,
body.title,
body.messages,
body.createdAt,
body.userEmail,
);
}

static async list(
Expand Down
29 changes: 29 additions & 0 deletions content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,24 @@ export class Content {
return this._cortex;
}

get userEmail() {
return this._userEmail;
}

get createdAt() {
return this._createdAt;
}

private constructor(
private apiClient: CortexApiClient,
private _id: string,
private _title: string,
private _content: string,
private _commands: ContentCommand[],
private _version: number,
private _createdAt: string,
private _cortex?: string,
private _userEmail?: string,
) {}

static async create(opts: CreateContentOptsSync): Promise<Content>;
Expand Down Expand Up @@ -146,6 +156,9 @@ export class Content {
body.content,
body.commands,
body.version,
body.createdAt,
body.cortex,
body.userEmail,
);
}

Expand All @@ -164,6 +177,8 @@ export class Content {

const id: string = res.headers.get("id") || "";
const version: number = parseInt(res.headers.get("version") || "0");
const userEmail = res.headers.get("userEmail") || undefined;
const createdAt: string = res.headers.get("createdAt") || "";
const commands: ContentCommand[] = JSON.parse(
res.headers.get("commands") || "[]",
);
Expand All @@ -185,7 +200,9 @@ export class Content {
content,
commands,
version,
createdAt,
cortex.name,
userEmail,
);
});

Expand Down Expand Up @@ -216,7 +233,9 @@ export class Content {
body.content,
body.commands,
body.version,
body.createdAt,
body.cortex,
body.userEmail,
);
}

Expand All @@ -235,6 +254,8 @@ export class Content {
this._title = body.title;
this._version = body.version;
this._cortex = body.cortex;
this._userEmail = body.userEmail;
this._createdAt = body.createdAt;

return this;
}
Expand Down Expand Up @@ -266,6 +287,8 @@ export class Content {
this._title = body.title;
this._version = body.version;
this._cortex = body.cortex;
this._userEmail = body.userEmail;
this._createdAt = body.createdAt;

return this;
}
Expand All @@ -282,11 +305,15 @@ export class Content {
const decoder = new TextDecoder("utf-8");

const version: number = parseInt(res.headers.get("version") || "0");
const createdAt = res.headers.get("createdAt") || "";
const userEmail = res.headers.get("userEmail") || undefined;
const commands: ContentCommand[] = JSON.parse(
res.headers.get("commands") || "[]",
);
this._version = version;
this._commands = commands;
this._createdAt = createdAt;
this._userEmail = userEmail;

const readableStream = new Readable({
read() {},
Expand Down Expand Up @@ -321,6 +348,8 @@ export class Content {
this._title = body.title;
this._version = body.version;
this._cortex = body.cortex;
this._userEmail = body.userEmail;
this._createdAt = body.createdAt;

return this;
}
Expand Down

0 comments on commit d7089a8

Please sign in to comment.