Skip to content

Commit

Permalink
feat: caching improvements & assistant messaging improvements (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy authored Feb 2, 2025
2 parents 35330da + 18370d5 commit 6ffec24
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 21 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"ts-jest": "29.1.2",
"ts-loader": "^9.5.2",
"ts-node": "~10.9.1",
"tsconfig-paths-webpack-plugin": "^4.2.0",
"typescript": "5.5.4",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
Expand Down
11 changes: 11 additions & 0 deletions packages/frontend/src/app/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,14 @@ ion-button.button-outline {
--max-width: 300px;
--max-height: 300px;
}

.textarea-autogrow-max400-40vh {
.native-wrapper {
max-height: min(400px, 40vh) !important;

.native-textarea {
max-height: min(400px, 40vh) !important;
overflow-y: auto !important;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ <h2 class="ion-text-wrap">
<ion-item>
<ion-textarea
id="assistant-message-textarea"
class="textarea-autogrow-max400-40vh"
[autoGrow]="true"
[counter]="true"
[maxlength]="maxMessageLength"
autocomplete="true"
spellcheck="true"
autocorrect="on"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type {
export class AssistantPage {
@ViewChild("content", { static: true }) content: any;

maxMessageLength = 1500;

messages: (AssistantMessageSummary & {
formattedDate?: string;
deservesDateDiff?: boolean;
Expand Down Expand Up @@ -205,6 +207,26 @@ export class AssistantPage {

async sendMessage() {
const pendingMessage = this.pendingMessage;

if (pendingMessage.length > this.maxMessageLength) {
const message = await this.translate
.get("pages.assistant.messageSize")
.toPromise();
const close = await this.translate.get("generic.okay").toPromise();

const toast = await this.toastCtrl.create({
message,
buttons: [
{
text: close,
role: "cancel",
},
],
});
await toast.present();
return;
}

if (this.processing || !pendingMessage) return;

this.processing = true;
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/app/utils/SyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const broadcastChannel = new BroadcastChannel(SW_BROADCAST_CHANNEL_NAME);

const ENABLE_VERBOSE_SYNC_LOGGING = false;

const SYNC_BATCH_SIZE = 50;
const SYNC_BATCH_SIZE = 200;

/**
* How long to wait between syncing each recipe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ import superjson from "superjson";

export const getTrpcResponseBodyForFetchResponse = async <T>(
response: Response,
) => {
const text = await response.clone().text();
): Promise<T | undefined> => {
if (response.status !== 200 && response.status !== 201) {
return undefined;
}

const output = superjson.parse<T>(text);
try {
const json = await response.clone().json();

return output;
if (!json.result?.data) {
return undefined;
}

const output = superjson.deserialize<T | undefined>(json.result.data);

return output;
} catch (e) {
console.error(e);

return undefined;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export const registerCreateRecipeRoute = (
ReturnType<typeof trpc.recipes.createRecipe.mutate>
>(response);

const syncManager = await syncManagerP;
syncManager.syncRecipe(output.id);
if (output) {
const syncManager = await syncManagerP;
syncManager.syncRecipe(output.id);
}

return response;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { RecipeSummary } from "@recipesage/prisma";
import { getTrpcInputForEvent } from "../../getTrpcInputForEvent";
import { trpcClient as trpc } from "../../../trpcClient";
import { encodeCacheResultForTrpc } from "../../encodeCacheResultForTrpc";
import { stripNumberedRecipeTitle } from "@recipesage/util/shared";

export const registerGetSimilarRecipesRoute = () => {
registerRoute(
Expand Down Expand Up @@ -40,9 +41,15 @@ export const registerGetSimilarRecipesRoute = () => {
);

if (recipe) {
originRecipeTitles.add(recipe.title);
originRecipeIngredients.add(recipe.ingredients);
originRecipeInstructions.add(recipe.instructions);
originRecipeTitles.add(stripNumberedRecipeTitle(recipe.title));

if (recipe.ingredients.trim().length) {
originRecipeIngredients.add(recipe.ingredients);
}

if (recipe.instructions.trim().length) {
originRecipeInstructions.add(recipe.instructions);
}
}
}

Expand All @@ -52,7 +59,7 @@ export const registerGetSimilarRecipesRoute = () => {

const similarRecipes = recipes.filter((recipe) => {
return (
originRecipeTitles.has(recipe.title) ||
originRecipeTitles.has(stripNumberedRecipeTitle(recipe.title)) ||
originRecipeIngredients.has(recipe.ingredients) ||
originRecipeInstructions.has(recipe.instructions)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export const registerUpdateRecipeRoute = (
ReturnType<typeof trpc.recipes.updateRecipe.mutate>
>(response);

const syncManager = await syncManagerP;
syncManager.syncRecipe(output.id);
if (output) {
const syncManager = await syncManagerP;
syncManager.syncRecipe(output.id);
}

return response;
},
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/assets/i18n/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
"pages.assistant.welcome.4": "This limit only applies to messages sent by you, and not to the AI's responses.",
"pages.assistant.welcome.documentation": "Please read the assistant documentation",
"pages.assistant.messageLimit": "Maximum messages reached (sorry, it's costly!). Message count resets at 0:00GMT.",
"pages.assistant.messageSize": "Your message is too long. Please send a shorter message.",

"pages.messageThread.title": "Conversation with {{name}}",
"pages.messageThread.clickToOpen": "Click to open",
Expand Down
6 changes: 6 additions & 0 deletions packages/frontend/sw-webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require("path");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");

module.exports = {
entry: "./src/service-worker.ts",
Expand All @@ -14,6 +15,11 @@ module.exports = {
},
resolve: {
extensions: [".ts", ".js"],
plugins: [
new TsconfigPathsPlugin({
configFile: path.resolve(__dirname, "tsconfig.json"),
}),
],
},
output: {
filename: "service-worker.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const assistant = new Assistant();
export const sendAssistantMessage = publicProcedure
.input(
z.object({
content: z.string().max(500),
content: z.string().max(1500),
}),
)
.query(async ({ ctx, input }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/trpc/src/procedures/recipes/getRecipesByIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { prisma, RecipeSummary, recipeSummary } from "@recipesage/prisma";
export const getRecipesByIds = publicProcedure
.input(
z.object({
ids: z.array(z.string()).max(50),
ids: z.array(z.string()).max(200),
}),
)
.query(async ({ ctx, input }): Promise<RecipeSummary[]> => {
Expand Down
6 changes: 2 additions & 4 deletions packages/trpc/src/procedures/recipes/getUniqueRecipeTitle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { publicProcedure } from "../../trpc";
import { z } from "zod";
import { prisma } from "@recipesage/prisma";
import {
validateTrpcSession,
stripNumberedRecipeTitle,
} from "@recipesage/util/server/general";
import { validateTrpcSession } from "@recipesage/util/server/general";
import { recipeSummaryLite } from "@recipesage/prisma";
import { stripNumberedRecipeTitle } from "@recipesage/util/shared";

/**
* An arbitrary upper limit for rename attempts so we don't spin forever
Expand Down
2 changes: 1 addition & 1 deletion packages/util/server/src/db/getSimilarRecipes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prisma, recipeSummaryLite } from "@recipesage/prisma";
import { stripNumberedRecipeTitle } from "../general/stripNumberedRecipeTitle";
import { stripNumberedRecipeTitle } from "@recipesage/util/shared";

export const getSimilarRecipes = async (
userId: string,
Expand Down
1 change: 0 additions & 1 deletion packages/util/server/src/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export * from "./email/sendPasswordResetEmail";
export * from "./clip";
export * from "./sortRecipeImages";
export * from "./fileTransformer";
export * from "./stripNumberedRecipeTitle";
export * from "./config";
export * from "./grip";
export * from "./fetch";
Expand Down
1 change: 1 addition & 0 deletions packages/util/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./cleanLabelTitle";
export * from "./preferences";
export * from "./JOB_RESULT_CODES";
export * from "./ImmediateDebouncer";
export * from "./stripNumberedRecipeTitle";

0 comments on commit 6ffec24

Please sign in to comment.