Created
January 3, 2025 19:23
-
-
Save AzureFlow/7138c888f1b1773dc6475b810e2a3b85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import WebSocket from "ws"; | |
import {readFileSync} from "node:fs"; | |
import {randomUUID} from "node:crypto"; | |
import {fileURLToPath} from "url"; | |
import ora from "ora"; | |
const __dirname = fileURLToPath(new URL(".", import.meta.url)); | |
const ORBIT_VERSION = "1.2.2"; | |
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0"; | |
const ORIGIN = "moz-extension://e2b72bb8-32b5-4d87-89a9-88a496a02a0f"; | |
const TOKEN_PREFIX = "orbit_message_store:"; | |
/** | |
* @param {string} context | |
* @param {string} prompt | |
* @param {Agent} [proxyAgent] | |
* @returns {Promise<{output: string, truncated: boolean}>} | |
*/ | |
async function prompt(context, prompt, proxyAgent = undefined) { | |
/** @type {UpdatePayload} */ | |
const updatePayload = { | |
prompt: null, | |
ai_context: null, | |
context: context, | |
title: null, | |
type: "generic", | |
icon_url: null, | |
}; | |
// console.info("updatePayload:", updatePayload); | |
const updateResp = await fetch("https://orbitbymozilla.com/v1/orbit/prompt/update", { | |
method: "POST", | |
body: JSON.stringify(updatePayload), | |
headers: { | |
"User-Agent": USER_AGENT, | |
"Accept": "*/*", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br, zstd", | |
"X-Orbit-Version": ORBIT_VERSION, | |
"Content-Type": "application/json", | |
"Authorization": "undefined", | |
"Origin": ORIGIN, | |
// "DNT": "1", | |
// "Connection": "keep-alive", | |
// "Cookie": "_app_session=", | |
"Sec-Fetch-Dest": "empty", | |
"Sec-Fetch-Mode": "cors", | |
"Sec-Fetch-Site": "same-origin", | |
"Priority": "u=4", | |
"TE": "trailers", | |
}, | |
}); | |
if(!updateResp.ok) { | |
throw new Error(`Failed to init request (${updateResp.status}): ${await updateResp.text()}`); | |
} | |
const {token: tokenRaw} = await updateResp.json(); | |
const token = tokenRaw.replace(TOKEN_PREFIX, ""); | |
// console.info("chat token:", token); | |
return new Promise((resolve, reject) => { | |
const ws = new WebSocket("wss://prod.orbit-ml-front-api.fakespot.prod.webservices.mozgcp.net/ws", { | |
headers: { | |
"User-Agent": USER_AGENT, | |
"Origin": ORIGIN, | |
}, | |
agent: proxyAgent, | |
}); | |
let output = ""; | |
let isTruncated = false; | |
ws.on("error", reject); | |
ws.on("open", function open() { | |
// console.info("Websocket connected"); | |
ws.send(JSON.stringify({ | |
key: token, | |
human_input: prompt, | |
// tabId: "16", | |
tabId: randomUUID(), | |
})); | |
}); | |
ws.on("message", function message(data) { | |
// {"sender":"bot","message":"","type":"start","code":null,"tabId":"16","messageId":""} | |
// {"sender":"bot","message":"","type":"stream","code":null,"tabId":"16","messageId":""} | |
// {"sender":"bot","message":"","type":"end","code":null,"tabId":"16","messageId":""} | |
/** @type {WebSocketMessage} */ | |
const parsed = JSON.parse(data.toString()); | |
// console.log(parsed); | |
// console.info("received: %s", data); | |
if(parsed.type === "stream") { | |
output += parsed.message; | |
} | |
if(parsed.type === "info" && parsed.code === "DATA_TRUNCATED") { | |
isTruncated = true; | |
} | |
if(parsed.type === "end") { | |
resolve({ | |
output: output.trim(), | |
truncated: isTruncated, | |
}); | |
ws.close(); | |
} | |
}); | |
// ws.on("close", function close() { | |
// console.info("Websocket closed"); | |
// }); | |
}); | |
} | |
const text = readFileSync(`${__dirname}/../resources/article.txt`, "utf8"); | |
const spinner = ora({ | |
text: "Generating", | |
color: "cyan", | |
spinner: "dots", | |
}).start(); | |
/** @type {SummaryType} */ | |
const summaryType = "text_summary_detailed"; | |
const result = await prompt(text, `Provide a concise summary of the text below (${summaryType}):`); | |
spinner.stop(); | |
console.log(result.output); | |
if(result.truncated) { | |
console.warn("[WARN] Output was truncated"); | |
} | |
/** @typedef {"text_summary_bullet"|"text_summary_detailed"|"text_summary_concise"|"text_summary_medium"} SummaryType */ | |
/** | |
* @typedef {object} WebSocketMessage | |
* @property {"bot"|"system"} sender | |
* @property {string} message Message content. | |
* @property {"start"|"stream"|"info"|"end"} type Message type. | |
* @property {"DATA_TRUNCATED"|null} code Error code. | |
* @property {string} tabId Unique identifier for this session. | |
* @property {""} messageId | |
*/ | |
/** | |
* @typedef {object} UpdatePayload | |
* @property {null} prompt | |
* @property {null} ai_context | |
* @property {string|null} context website content. | |
* @property {string|null} title Website title. | |
* @property {"gmail"|"youtube"|"gdocs"|"generic"} type | |
* @property {string|null} icon_url Website ico. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment