Avatar

xkonti

Joined August 11, 2023
Public vals
26
xkonti avatar
weekday
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
extractHttpEndpoint
@xkonti
Script
Slimmed down version of pomdtr/extractValInfo that focuses on extracting the HTTP endpoint URL of the Val. Example usage: const apiUrl = extractHttpEndpoint(import.meta.url);
xkonti avatar
gptApiTemplate
@xkonti
HTTP
This Val is a template for creating GPT actions. To have a full explanation on how to use it or create your own, you can check out the Creating GPT Actions with ValTown tutorial: Video on YouTube Article on xkonti.tech The GPT using it: Game Idea Exchange GPT
xkonti avatar
gameIdeaApi
@xkonti
HTTP
This Val is a part of Creating GPT Actions with ValTown tutorial: Video on YouTube Article on xkonti.tech The GPT using it: Game Idea Exchange GPT
xkonti avatar
httpUtils
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
gptApiSchemaBuilder
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
gptApiFramework
@xkonti
Script
Allows for automatic generation of Hono API compatible with GPTs. Endpoints' inputs and outputs need to be specified via types from which the Open API spec is generated automatically and available via /gpt/schema endpoint. ⚠️ Breaking changes introduced in v23 & 24: nothingToJson doesn't take the generic TResponse anymore. The type is inferred from the endpoint definition. The endpoint definition doesn't need the requestSchema , requestDesc and responseDesc defined anymore. The descriptions are inferred from the schema description. jsonToJson doesn't take the generic TRequest and TResponse anymore. Types are inferred from the endpoint definition. The endpoint definition doesn't need the requestDesc and responseDesc defined anymore. The descriptions are inferred from the schema description. Usage example: import { GptApi } from "https://esm.town/v/xkonti/gptApiFramework"; import { z } from "npm:zod"; /** * COMMON TYPES */ const ResponseCommandSchema = z.object({ feedback: z.string().describe("Feedback regarding submitted action"), command: z.string().describe("The command for the Mediator AI to follow strictly"), data: z.string().optional().describe("Additional data related to the given command"), }).describe("Contains feedback and further instructions to follow"); /** * INITIALIZE API */ const api = new GptApi({ url: "https://xkonti-planoverseerai.web.val.run", title: "Overseer AI API", description: "The API for interacting with the Overseer AI", version: "1.0.0", policyGetter: async () => { const { markdownToPrettyPage } = await import("https://esm.town/v/xkonti/markdownToHtmlPage?v=5"); return await markdownToPrettyPage("# Privacy Policy\n\n## Section 1..."); }, }); /** * REQUIREMENTS GATHERING ENDPOINTS */ api.nothingToJson({ verb: "POST", path: "/newproblem", operationId: "new-problem", desc: "Endpoint for informing Overseer AI about a new problem presented by the User", responseSchema: ResponseCommandSchema, responseDesc: "Instruction on how to proceed with the new problem", }, async (ctx) => { return { feedback: "User input downloaded. Problem analysis is required.", command: await getPrompt("analyze-problem"), data: "", }; }); export default api.serve();
xkonti avatar
milligramCss
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
htmlTags
@xkonti
Script
// CONTENT SECTIONING
xkonti avatar
htmlBuilder
@xkonti
Script
* Renders the provided tag into HTML string.
xkonti avatar
htmlInterfaces
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
htmlUtils
@xkonti
Script
* Wraps `content` between `open` and `close` content. If the content is a list, it puts each in a new line.
xkonti avatar
cache
@xkonti
Script
Implementation of Redis-like cache - a key-value store with expiring keys. Data is stored in the Val Town SQLite database and shared between all your vals. Setup First you should decide on a name of a SQL table that will be used for storing cache data. It could something like cacheData or kv . Set that value to a new Environment Variable CACHE_TABLE_NAME . Optionally you might add a new CACHE_DEFAULT_TTL Environment Variable. It's value should be set to a number of seconds that will be used when saving new values to the cache without providing the expiration time. By default it's 24h. The setup() function should be ran before using the cache for the first time. You can do that by creating a small temporary Val: import { setup } from "https://esm.town/v/xkonti/cache"; await setup(); Optionally create a scheduled val that will delete expired keys on some interval - 15 minutes can be a good start. import { deleteExpired } from "https://esm.town/v/xkonti/cache"; export default async function cacheCleaner(interval: Interval) { await deleteExpired(); } Usage After setting your cache up you can use it simply by importing functions from https://esm.town/v/xkonti/cache . set(key, value, ttl): Promise Set a value in the cache. key - the key to set the value for value - the value to set - it can be any value that can be serialized to JSON ttl - the time to live in seconds. In other words, after how many seconds the key will expire. If not set, the default TTL is used. returns the number of keys set: 1 if the key was inserted/updated, 0 if the ttl was 0 or invalid // Set a value in the cache with a default TTL await set("luckyNumber", 13); // Set a value that will expire in 5 minutes await set("product:344798", { name: "Audio Interface", price: 209.99}, 5 * 60); setUntil(key, value, expiresAt): Promise Set a value in the cache until a specific date and time. key - the key to set the value for value - the value to set - it can be any value that can be serialized to JSON expiresAt - the expiration time as a UTC date string returns the number of keys set: 1 if the key was inserted/updated, 0 if the `expiresAt`` was in the past // Set a value in the cache until 2024-01-01 16:23:05 UTC await setUntil( "product:155392", { name: "Audio Interface", price: 209.99 }, new Date('2024-01-01T16:23:05Z').toISOString() ); setExpiration(key, ttl): Promise Update the expiration date of a cache entry based on TTL. If the key does not exist or is expired, nothing happens. key - the key of the cache entry to update ttl - the time to live in seconds from now. In other words, after how many seconds the key will expire. If not set, the default TTL is used. returns the number of keys updated: 1 if updated, 0 if not found or ttl was 0 // Set the expiration date in the cache with a default TTL await setExpiration("luckyNumber"); // Set the expiration date in the cache for 5 minutes from now. await setExpiration("luckyNumber", 5 * 60); setExpirationUntil(key, expiresAt): Promise Update the expiration date of a cache entry to a specific UTC date and time. If the key does not exist or is expired, nothing happens. key - the key of the cache entry to update expiresAt - the expiration time as a UTC date string returns the number of keys updated: 1 if updated, 0 if not found or expiresAt was in the past // Set the expiration date in the cache until 2024-01-01 16:23:05 UTC await setExpirationUntil( "product:155392", new Date('2024-01-01T16:23:05Z').toISOString() ); exists(key): Promise Checks if the provided key exists (has value) in the cache. If the key is expired, it's considered non-existent. key - the key to check for existence // Check if the key is present in the cache const hasLuckyNumber: Boolean = await exists("luckyNumber"); get (key): Promise<T | null> Get a value from the cache by key. You can provide a type of the return value or it will default to unknown . If there is no value for the key or the value has expired, null is returned. key - the key to get the value for // Get a value from the cache const luckyNumber: number = await get<number>("luckyNumber"); const luckyNumber: number = await get("luckyNumber") as number; // same as above listKeys(prefix): Promise<string[]> Gets a list of all non-expired keys in the cache that match the prefix. If no prefix is provided, all keys are returned. prefix - the optional prefix to match keys against // Get all keys from the cache const keys: string[] = await listKeys(); // Get all keys from the cache that start with "product:" const keys: string[] = await listKeys("product:"); getMany (prefix, limit): Promise<Array<{ key: string, value: T }>> Get many key-value pairs from the cache that match the given prefix. prefix - the optional prefix to match keys against. If not provided, all keys are considered. limit - the optional maximum number of key-value pairs to return. If 0 , no limit is applied. Defaults to 0 . returns An array of key-value pairs. Each pair is an object with key and value properties. // Get all non-expired keys and their values const everything = await getMany(); // Get all keys and values with a matching prefix const allProducts = await getMany("product:"); // Get 5 keys and values with a matching prefix const discountedProducts = await getMany("discounts:", 5); deleteKey(key): Promise Delete a key from the cache. key - the key to delete returns the number of keys deleted: 1 if the key was deleted, 0 if the key did not exist. // Delete a key from the cache await deleteKey("luckyNumber"); deleteKeys(prefix): Promise Delete all keys from the cache that match the prefix. If no prefix is provided, all keys in the cache are deleted. prefix - the optional prefix to match keys against returns the number of keys deleted // Delete all keys from the cache await deleteKeys(); // Delete all keys from the cache that start with "product:" await deleteKeys("product:"); deleteExpired(): Promise Delete all expired keys from the cache. Perfect for running on a schedule to keep the cache small and fast. returns the number of keys deleted // Delete all expired keys from the cache await deleteExpired();
xkonti avatar
memoryApiExample
@xkonti
HTTP
An interactive, runnable TypeScript val by xkonti
xkonti avatar
makeHtmlPage
@xkonti
Script
An interactive, runnable TypeScript val by xkonti
xkonti avatar
markdownToHtml
@xkonti
Script
Converts markdown string into HTML string. Usage example: import { markdownToHtml } from "https://esm.town/v/xkonti/markdownToHtml"; const markdown = ` # Hello there How are you doing *these* days? `; const html = await markdownToHtml(markdown); console.log(html)