Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
continue work on link preload prediction
Browse files Browse the repository at this point in the history
adamsilverstein committed Aug 29, 2024
1 parent 0c151d5 commit 5bbb6e5
Showing 2 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugins/speculation-rules/hooks.php
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ function plsr_load_predict_script(): void {
wp_enqueue_script(
'plsr-predict',
plugin_dir_url( __FILE__ ) . 'predict.js', // @todo switch to build version.
array( 'genai_bundle' ),
array(),
SPECULATION_RULES_VERSION,
array(
'strategy' => 'defer',
71 changes: 57 additions & 14 deletions plugins/speculation-rules/predict.js
Original file line number Diff line number Diff line change
@@ -3,28 +3,70 @@
// eslint-disable-next-line no-console -- For testing only.
console.log( 'Starting...' );

async function predictLinks( { close } ) {
close();

async function predictLinks() {
const session = await window.ai.assistant.create();

const stream = session.promptStreaming(
`You will predict the links on a web page that visitors are most likely to visit. We will pre-render those links to make them load instantly for users. Please provide the five links users are most likely to visit as a JSON object. Here is the HTML: ${ document.documentElement.outerHTML }`
);
const body = document.getElementsByTagName( 'body' )[ 0 ].innerHTML;

//const prompt = `Predict the five links users are most likely to visit on this page, returning ONLY the 5 full urls as a JSON object: ${ body }`;
// const prompt = `Given a web page's HTML, find the links and predict which is most likely to be clicked by a user. Return the top 5 links as a JSON object. Only return the JSON object as your complete answer. Here is the HTML: ${ body }`;
const prompt = `In order to prerender links so users get instant navigations, can you predict the three links users are most likely to visit on this page, returning ONLY the 3 full urls as a JSON object in the format ['url','url2','url3']. Here is the HTML: ${ body }`;

// eslint-disable-next-line no-console -- For testing only.
console.log( `The size of the prompt is ${ prompt.length }.` );

let result = false;
try {
result = await session.prompt( prompt );
} catch ( error ) {
// eslint-disable-next-line no-console -- For testing only.
console.error( error );
return;
}

if ( ! result ) {
// eslint-disable-next-line no-console -- For testing only.
console.log( 'No result.' );
return;
}

// Log result so far
// eslint-disable-next-line no-console -- For testing only.
console.log( result );

let result = '';
// Grab everything after "Output:" or "```json" strings etc...
const output =
result.split( 'Output:' )[ 1 ] ||
result.split( '```json' )[ 1 ] ||
result.split( 'Answer:' )[ 1 ] ||
result.split( 'Solution:' )[ 1 ];

for await ( const value of stream ) {
result = value;
// If there is no output, return.
if ( ! output ) {
// eslint-disable-next-line no-console -- For testing only.
console.log( 'No output.' );
return;
}

// Remove the two "```" formatting strings.
result = output.replace( /```JSON/g, '' );
result = output.replace( /```/g, '' );

// Remove any newlines.
result = result.replace( /\n/g, '' );

// Remove any whitespace.
result = result.replace( / /g, '' );

// eslint-disable-next-line no-console -- For testing only.
console.log( result );

// Add a prerender link for each URL using the Speculation Rules API
const links = JSON.parse( result );
const resultData = JSON.parse( result );

for ( const link of links ) {
for ( const link of resultData.links ) {
// eslint-disable-next-line no-console -- For testing only.
console.log( `Link: ${ link }` );
/**
* Add speculation rules API prerendering.
*
@@ -44,11 +86,12 @@
prerenderLink.type = 'speculationrules';
const rules =
'{ "prerender": [ { "where": { "href_matches": "URL" }, "eagerness": "moderate" }] }';
prerenderLink.textContent = rules.replace( 'URL', link );
prerenderLink.textContent = rules.replace( 'URL', link.href );
document.head.appendChild( prerenderLink );
}

close();
// eslint-disable-next-line no-console -- For testing only.
console.log( `Prerendering link: ${ link }` );
}
}
await predictLinks();
} )();

0 comments on commit 5bbb6e5

Please sign in to comment.