• 12 Posts
  • 197 Comments
Joined 1 year ago
cake
Cake day: July 14th, 2023

help-circle






  • This is correct/expected behavior, and isn’t Perchance-specific, but I’ll admit it’s confusing. It’s basically “JavaScript labelled statements strike again”. This is valid JavaScript:

    {
      let a = 10;
      x:2
    }
    

    The curly braces define a block, so that the a variable is only defined within that block - this is very handy, and I use it quite a lot. It’s equivalent to:

    if(true) {
      let a = 10;
      x:2
    }
    

    The x:2 on the other hand is unusual and wouldn’t be written in “normal” code. It’s a labelled statement, but it’s a useless one, since there’s no way to actually “use” it.

    So TL;DR: {x:2} is being treated as a block with a labelled statement rather than a JavaScript object. The workaround is to wrap it in parentheses like [ ({ x:1 }) ] instead of [ { x:1 } ].

    I’ve added some comments to your example generator:

    b = [ {} ] // a block with nothing in it
    d = [ { x:1 } ] // a block with a labelled statement, x:1
    e = [ x = {} ] // assignment, and so right hand side is constrained to be an expression - i.e. cannot be a block
    f = [ { toString: function f() { return "hey"; } } ] // same as d
    g = [ x2 = { toString: function f() { return "hey"; } } ] // same as e
    

    You can test all of the above using eval like: eval("{x:1}") to see it’s not Perchance-specific. To add to the confusion, DevTools does some “magic” on top of eval in order to basically prioritize interpretation as an object, I think.

    I like DevTools’ approach better, and have actually considered “fixing” this, since it would be weird to use JS blocks with labelled statements inside square blocks, but it’s probably too late due to backwards-compat issues, and would need to wait for V2 of the engine.

    Even though it’s not technically a bug, I’ll link this comment from perchance.org/known-bugs as something to improve on in V2 of the engine.







  • As a general strategy for parsing stuff out of free-form/natural language, I’d definitely recommend using the AI text generation rather than regex, since natural language is so diverse that regex will fail a lot of the time. In most cases there are too many different ways that things can be said to be able to capture them all with regex.

    I assume you’re talking about perchance.org/ai-character-chat custom code, and if so, you could do something like this:

    function scanDescription(description) {
      let result = await oc.getInstructCompletion({ // oc.getInstructCompletion is the /ai-character-chat custom code equivalent to perchance's ai text plugin
        instruction: `Convert the following description into a JSON object like this: { lastName, race, politicalEntity, rank, position }\nDESCRIPTION: ${description}\n\nRespond with just the one-line JSON object, and nothing more.`,
        startWith: `{ "lastName":`, // make it start off with the correct format (to ensure it doesn't e.g. say "sure! I can help you with that...")
        stopSequences: ["}"], // force it to stop at the end of the JSON, just in case it doesn't stop by itself (e.g. tries to say "there you go!" or something)
      });
      return JSON.parse(result.text);
    }
    
    // Example usage:
    
    let info = "Nick, a 49-year-old man, is from a small farming town in Iowa. His last name is Young, he is human, and he holds the rank of Captain in the local police force. His political entity is the United States.";
    
    let result = await scanDescription(info);
    
    console.log(result.politicalEntity);
    console.log(result.lastName);
    // ...
    

    More info on custom code for perchance.org/ai-character-chat is here: https://rentry.org/82hwif




  • (Note that Perchance is actually a platform for creating generators/applications, and it sounds like you’re talking about one particular generator, made by one particular user - namely perchance.org/ai-character-chat - in this case I happen to be that ‘user’. This is just a heads up because a lot of people who land here think that perchance is an AI chat site or whatever. It’s actually a platform where anyone can create random generators, and other things like AI chat applications)

    it just… Goes “stupid”?

    when I make more advanced AIs it tells me they can’t access websites or show me the contents.

    it’ll run through the Lore Querying then it doesn’t provide its “Generated Response” in regards to the facts in the Lores.

    I can probably fix all of these things, but I need example threads where they happened. If any of them happen again and if you’re able to share (in reply here, or via DM) a snapshot of a thread using this save button:

    then I should be able to work out what’s happening and fix it. Without example threads, it’s really hard for me to fix stuff like this. But the moment someone gives me an example thread/export, or an easy way to consistently reproduce the issue, I can usually fix/improve it really fast.

    This Generator Has Errors

    Are you able to explain the steps I can take to reproduce this error? I just created a character, and added some example lore, and saved it, and it seems to work fine, so it might be related to some settings in your character, or something like that. If you’re not able to reliably reproduce it (i.e. it happens randomly), then another helpful thing would be to click that “this generator has errors” notice and copy/screenshot the error message that is shown.




  • Thanks!

    1. I’ve hackily fixed <a href="#foo">foo</a> clicks so they work properly now.

    2. I’ve also added a fix so if you visit perchance.org/whatever#foo it’ll correctly try to scroll to the id="foo" element if it exists: https://perchance.org/dkp9npcidc#foo - pinging @VioneT@lemmy.world

    3. You can also type a hash into the address bar of an already loaded page and press enter, and it’ll scroll to the element, like a normal top-level page does.

    4. I’ve also fixed history.replaceState so you can now change the query string of the top-level frame as shown below. It’ll throw an error if you try to change the pathname, since that could allow for malicious ‘spoofing’ of other generators as mentioned above.

    history.replaceState({}, "", `/${generatorName}?foo=1`)
    
    1. You can change the hash with window.location.hash = "#foo" (you can also do this with replaceState IIUC).
    2. You can change page/tab title with document.title = "foo"; and it’ll propagate to the top-level frame.
    3. You can change the favicon like you usually would (add a <link rel="icon" href="https://exampe.com/image.png"> to <head>) and it’ll propagate to the top-level frame.

    Let me know if you find any bugs 👍

    And I’ve added a hideGalleryButtons to the text to image plugin.


    • I want to remove the gallery buttons in my plugin pages for example
    • that would reach across into the main perchance.org domain it’s disallowed

    Allowing general-purpose cross-domain access like this is not possible, since it would allow very easy login credential stealing, and stuff like that, but these particular things that you’ve mentioned are good suggestions.

    RE gallery button: You’re talking about the “send to gallery” and the “open gallery” buttons in the menu after clicking the heart button, right? Would something like this work?

    promptData
      prompt = ...
      hideGalleryButtons = true
    

    I have needed history.replaceState before myself, but haven’t gotten around to properly thinking about it. I think it should be fine to allow a generator to change the query parameters and hash (i.e. everything other than the pathname - since otherwise could ‘spoof’ other generators/pages). Can you let me know your specific use case to help me triangulate on a good approach here? Do you specifically need pushState? Or will replaceState do? I’m reluctant to add pushState because it can be used ~maliciously - like the spammy sites that effectively hijack your back button to prevent you from leaving the page (or at least, make it require an extra click or two).