Skip to content

Commit

Permalink
Merge branch 'master' into reduce_memory_usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 20, 2024
2 parents b994f0f + e27be5e commit 324f460
Show file tree
Hide file tree
Showing 53 changed files with 691 additions and 186 deletions.
1 change: 1 addition & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Change bot cmd number param values from u16 to f64 ([#7095](https://github.com/open-chat-labs/open-chat/pull/7095))
- Rename access token `parameters` to `command_args` ([#7104](https://github.com/open-chat-labs/open-chat/pull/7104))

## [[2.0.1530](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1530-local_user_index)] - 2024-12-19

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ async fn access_token(args: Args) -> Response {
thread_root_message_index: bc.thread_root_message_index,
message_id: bc.message_id,
command_name: bc.command_name,
parameters: bc.parameters,
version: bc.version,
command_args: bc.command_args,
command_text: bc.command_text,
bot_api_gateway: state.env.canister_id(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ fn validate(args: Args, state: &RuntimeState) -> Result<c2c_handle_bot_action::A
let claims = verify_jwt::<Claims<BotCommandClaims>>(&args.jwt, state.data.oc_key_pair.public_key_pem())
.map_err(|error| format!("Access token invalid: {error:?}"))?;

if claims.exp() < state.env.now() {
return Err("Access token expired".to_string());
}

let bot_command_claims = claims.custom();
let calling_user = bot.user_id;
let jwt_user = bot_command_claims.bot;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ fn main() {
generate_ts_method!(user_index, submit_proof_of_unique_personhood);
generate_ts_method!(user_index, suspend_user);
generate_ts_method!(user_index, unsuspend_user);
generate_ts_method!(user_index, update_bot);
generate_ts_method!(user_index, update_diamond_membership_subscription);
}
3 changes: 1 addition & 2 deletions backend/integration_tests/src/bot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ fn e2e_bot_test() {
thread_root_message_index: None,
message_id,
command_name: command_name.clone(),
parameters: "".to_string(),
version: 1, // TODO: Remove this
command_args: "".to_string(),
command_text: command_name.clone(),
}),
chat,
Expand Down
3 changes: 1 addition & 2 deletions backend/libraries/types/src/access_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ pub struct BotCommandArgs {
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub command_name: String,
pub parameters: String,
pub version: u32,
pub command_args: String,
pub command_text: String,
}

Expand Down
3 changes: 1 addition & 2 deletions backend/libraries/types/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ pub struct BotCommandClaims {
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub command_name: String,
pub parameters: String,
pub version: u32,
pub command_args: String,
pub command_text: String,
pub bot_api_gateway: CanisterId,
}
6 changes: 6 additions & 0 deletions frontend/app/src/components/Input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
children?: Snippet;
onblur?: () => void;
onfocus?: () => void;
oninput?: () => void;
onenter?: () => void;
}
</script>

Expand All @@ -40,6 +42,8 @@
pattern = undefined,
onblur = undefined,
onfocus = undefined,
oninput = undefined,
onenter = undefined,
children,
}: InputProps = $props();
Expand All @@ -61,6 +65,7 @@
value = parseInt(e.currentTarget.value, 10);
}
dispatch("change", value);
oninput?.();
};
export function setValue(text: string) {
Expand All @@ -70,6 +75,7 @@
function keyDown(e: KeyboardEvent) {
if (e.key === "Enter") {
dispatch("enter");
onenter?.();
}
}
Expand Down
45 changes: 38 additions & 7 deletions frontend/app/src/components/bots/AutoBotBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import Reload from "svelte-material-icons/Reload.svelte";
import {
validEndpoint,
emptyBotInstance,
OpenChat,
validateBot,
ValidationErrors,
type ExternalBot,
type SlashCommandSchema,
type ValidationErrorMessages,
userStore,
} from "openchat-client";
import { i18nKey } from "../../i18n/i18n";
import Input from "../Input.svelte";
Expand All @@ -23,19 +23,21 @@
import HoverIcon from "../HoverIcon.svelte";
import { iconSize } from "../../stores/iconSize";
import CommandViewer from "./CommandViewer.svelte";
import SingleUserSelector from "../home/SingleUserSelector.svelte";
const client = getContext<OpenChat>("client");
interface Props {
valid: boolean;
onUpdate: (bot: ExternalBot) => void;
candidate: ExternalBot;
nameDirty: boolean;
}
let { valid = $bindable(), onUpdate }: Props = $props();
let { valid = $bindable(), onUpdate, candidate, nameDirty }: Props = $props();
let selectedCommand = $state<SlashCommandSchema | undefined>(undefined);
let selectedCommandIndex = $state<number | undefined>(undefined);
let debug = $state(false);
let candidate = $state<ExternalBot>(emptyBotInstance());
let schemaLoaded = $state(false);
let schemaLoading = $state(false);
let showNext = $derived(
Expand All @@ -49,7 +51,7 @@
() => [$state.snapshot(candidate)],
async () => {
const errors = validateBot(candidate);
if (errors.get("bot_name").length == 0) {
if (errors.get("bot_name").length == 0 && nameDirty) {
errors.addErrors("bot_name", await checkUsername(candidate.name));
}
return errors;
Expand Down Expand Up @@ -100,7 +102,7 @@
// let editing = $derived(bot !== undefined);
$effect(() => {
const isValid = errors.size === 0;
const isValid = errors.size === 0 && schemaLoaded;
if (isValid !== valid) {
valid = isValid;
}
Expand All @@ -123,6 +125,15 @@
selectedCommandIndex = index;
}
function endpointChanged() {
schemaLoaded = false;
candidate.definition = {
kind: "bot_definition",
description: "",
commands: [],
};
}
function loadDefinition() {
if (!schemaLoading && validEndpoint(candidate.endpoint)) {
schemaLoading = true;
Expand Down Expand Up @@ -173,6 +184,22 @@
bind:value={candidate.id}>
</ValidatingInput>

<Legend
required
label={i18nKey("bots.builder.ownerLabel")}
rules={i18nKey("bots.builder.ownerRules")}></Legend>
<SingleUserSelector
invalid={errors.has("bot_owner")}
error={errors.get("bot_owner")}
border={false}
direction={"up"}
mentionSelf
on:userSelected={(ev) => (candidate.ownerId = ev.detail.userId)}
on:userRemoved={(_) => (candidate.ownerId = "")}
selectedReceiver={$userStore.get(candidate.ownerId)}
placeholder={"bots.builder.ownerLabel"}
autofocus={false} />

<Legend
required
label={i18nKey("bots.builder.nameLabel")}
Expand All @@ -197,13 +224,17 @@
maxlength={200}
invalid={errors.has("bot_endpoint")}
error={errors.get("bot_endpoint")}
oninput={endpointChanged}
onenter={loadDefinition}
placeholder={i18nKey("https://my_openchat_bot")}
bind:value={candidate.endpoint} />
</div>
<div class="icon">
{#if !errors.has("bot_endpoint")}
<HoverIcon onclick={loadDefinition}>
<Reload size={$iconSize} color={"var(--icon-txt)"}></Reload>
<HoverIcon title={"load definition"} onclick={loadDefinition}>
<Reload
size={$iconSize}
color={schemaLoaded ? "var(--icon-txt)" : "var(--accent)"}></Reload>
</HoverIcon>
{/if}
</div>
Expand Down
12 changes: 12 additions & 0 deletions frontend/app/src/components/bots/BotAvatar.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import { AvatarSize } from "openchat-client";
import Avatar from "../Avatar.svelte";
interface Props {
bot: { avatarUrl?: string };
}
let { bot }: Props = $props();
</script>

<Avatar url={bot.avatarUrl ?? "/assets/bot_avatar.svg"} size={AvatarSize.Default} />
3 changes: 2 additions & 1 deletion frontend/app/src/components/bots/BotBuilder.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import {
currentUser,
emptyBotInstance,
emptySlashCommandPermissions,
OpenChat,
Expand Down Expand Up @@ -33,7 +34,7 @@
let selectedCommand = $state<SlashCommandSchema | undefined>(undefined);
let selectedCommandIndex = $state<number | undefined>(undefined);
let debug = $state(false);
let candidate = $state<ExternalBot>(emptyBotInstance());
let candidate = $state<ExternalBot>(emptyBotInstance($currentUser.userId));
let errors = $derived.by(
debouncedDerived(
Expand Down
Loading

0 comments on commit 324f460

Please sign in to comment.