Skip to content

Commit 74e7e9f

Browse files
committed
forgot this one
1 parent 3717377 commit 74e7e9f

1 file changed

Lines changed: 45 additions & 57 deletions

File tree

src/functions/cookies.ts

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,65 @@
11
import * as vscode from 'vscode';
2+
import z from 'zod';
23

34
import { logOutput } from '@/utils/misc';
45
import { NEURO } from '@/constants';
56
import { CONNECTION, PermissionLevel, getPermissionLevel } from '@/config';
67
import { addActions, CATEGORY_MISC } from '@/rce';
7-
import { actionHandlerFailure, actionHandlerSuccess, RCEAction } from '@/utils/neuro_client';
8-
import { RCEContext, SimplifiedStatusUpdateHandler } from '@ctx/rce';
8+
import { actionHandlerFailure, actionHandlerSuccess, defineAction } from '@/utils/neuro_client';
9+
import { SimplifiedStatusUpdateHandler } from '@ctx/rce';
910

10-
export const REQUEST_COOKIE_ACTION: RCEAction = {
11+
export const REQUEST_COOKIE_ACTION = defineAction({
1112
name: 'request_cookie',
1213
description: `Ask ${CONNECTION.userName} for a cookie. You can request a specific flavor, but it's up to ${CONNECTION.userName} to decide.`,
1314
category: CATEGORY_MISC,
14-
schema: {
15-
type: 'object',
16-
properties: {
17-
flavor: { type: 'string' },
18-
},
19-
additionalProperties: false,
15+
schema: z.object({
16+
flavor: z.string().optional(),
17+
}),
18+
handler(context) {
19+
const { data: actionData, updateStatus } = context;
20+
const permission = getPermissionLevel(actionData.name);
21+
22+
switch (permission) {
23+
case PermissionLevel.COPILOT: {
24+
giveCookie(true, actionData.params.flavor, updateStatus);
25+
return actionHandlerSuccess(`Waiting on ${CONNECTION.userName} to decide on the flavor.`, 'Waiting for cookie flavor...');
26+
}
27+
case PermissionLevel.AUTOPILOT: {
28+
logOutput('INFO', `Neuro grabbed a ${actionData.params.flavor} cookie.`);
29+
if (actionData.params?.flavor) {
30+
// Return flavor as requested
31+
return actionHandlerSuccess(`You grabbed a ${actionData.params.flavor} cookie!`, `${actionData.params.flavor} cookie grabbed`);
32+
}
33+
// Funny quotes if no flavor specified
34+
const base = 'You grabbed an undefined cookie. ';
35+
const quotes = [
36+
'Wait a second...',
37+
`Unfortunately, ${CONNECTION.userName} wasn't around to decide the flavor for you.`,
38+
'Maybe you should have defined it.',
39+
'You could maybe write the Cookie class to define it.',
40+
'Probably undefined taste as well.',
41+
'It\'s still a cookie. Probably.',
42+
'Isn\'t that just stale cookies?',
43+
'You should probably #define it.',
44+
'NullReferenceException: Flavor reference not set to an instance of a Cookie.',
45+
'TypeError: Cannot read property \'flavor\' of undefined cookie.',
46+
'Segmentation fault (core dumped).',
47+
];
48+
const randomIndex = Math.floor(Math.random() * quotes.length);
49+
return actionHandlerFailure(base + quotes[randomIndex], `${base.replace('You', CONNECTION.nameOfAPI)}${quotes[randomIndex].replace(/you|You/g, CONNECTION.nameOfAPI)} (undefined flavor)`);
50+
}
51+
default:
52+
return actionHandlerFailure('Uhhhh, something went wrong', 'Somehow, this action\'s permission wasn\'t set to Copilot or Autopilot, huh???'); // Added for type-safety I guess
53+
}
2054
},
21-
handler: handleRequestCookie,
22-
promptGenerator: (context: RCEContext) => `have a${context.data.params?.flavor ? ' ' + context.data.params.flavor : ''} cookie.`,
55+
promptGenerator: (context) => `have a${context.data.params?.flavor ? ' ' + context.data.params.flavor : ''} cookie.`,
2356
defaultPermission: PermissionLevel.COPILOT,
24-
};
57+
});
2558

2659
export function addRequestCookieAction() {
2760
addActions([REQUEST_COOKIE_ACTION]);
2861
}
2962

30-
function handleRequestCookie(context: RCEContext) {
31-
const { data: actionData, updateStatus } = context;
32-
const permission = getPermissionLevel(actionData.name);
33-
34-
switch (permission) {
35-
case PermissionLevel.COPILOT: {
36-
giveCookie(true, actionData.params.flavor, updateStatus);
37-
return actionHandlerSuccess(`Waiting on ${CONNECTION.userName} to decide on the flavor.`, 'Waiting for cookie flavor...');
38-
}
39-
case PermissionLevel.AUTOPILOT: {
40-
logOutput('INFO', `Neuro grabbed a ${actionData.params.flavor} cookie.`);
41-
if (actionData.params?.flavor) {
42-
// Return flavor as requested
43-
return actionHandlerSuccess(`You grabbed a ${actionData.params.flavor} cookie!`, `${actionData.params.flavor} cookie grabbed`);
44-
}
45-
// Funny quotes if no flavor specified
46-
const base = 'You grabbed an undefined cookie. ';
47-
const quotes = [
48-
'Wait a second...',
49-
`Unfortunately, ${CONNECTION.userName} wasn't around to decide the flavor for you.`,
50-
'Maybe you should have defined it.',
51-
'You could maybe write the Cookie class to define it.',
52-
'Probably undefined taste as well.',
53-
'It\'s still a cookie. Probably.',
54-
'Isn\'t that just stale cookies?',
55-
'You should probably #define it.',
56-
'NullReferenceException: Flavor reference not set to an instance of a Cookie.',
57-
'TypeError: Cannot read property \'flavor\' of undefined cookie.',
58-
'Segmentation fault (core dumped).',
59-
];
60-
const randomIndex = Math.floor(Math.random() * quotes.length);
61-
return actionHandlerFailure(base + quotes[randomIndex], `${base.replace('You', CONNECTION.nameOfAPI)}${quotes[randomIndex].replace(/you|You/g, CONNECTION.nameOfAPI)} (undefined flavor)`);
62-
}
63-
default:
64-
return actionHandlerFailure('Uhhhh, something went wrong', 'Somehow, this action\'s permission wasn\'t set to Copilot or Autopilot, huh???'); // Added for type-safety I guess
65-
}
66-
// Removed the try-catch because this shoud be handled by the RCE system now
67-
// catch (erm) {
68-
// const actionName = actionData.name;
69-
// notifyOnCaughtException(actionName, erm);
70-
// NEURO.client?.sendActionResult(actionData.id, true, `An error occured while executing the action "${actionName}". You may retry if you like, but it may be better to ask Vedal to check what's up.`);
71-
// return;
72-
// }
73-
}
74-
7563
export function giveCookie(isRequested = false, defaultFlavor = 'Chocolate Chip', updateStatus?: SimplifiedStatusUpdateHandler) {
7664
if (!NEURO.connected) {
7765
logOutput('ERROR', 'Attempted to give cookie while disconnected');

0 commit comments

Comments
 (0)