### Install UltraContext via npm Source: https://github.com/ultracontext/ultracontext-node/blob/main/README.md This command installs the ultracontext package from npm, making it available for use in your Node.js project. Ensure you have Node.js and npm installed. ```bash npm install ultracontext ``` -------------------------------- ### Complete UltraContext Integration Example in TypeScript Source: https://context7.com/ultracontext/ultracontext-node/llms.txt This TypeScript example demonstrates a full integration of UltraContext with an LLM workflow. It covers creating a new context, appending system and user messages, retrieving the current context, updating a message, accessing historical versions (time-travel), and forking a context to create a branch. It requires the 'ultracontext' npm package. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); async function chatWithHistory() { // Create a new conversation context const ctx = await uc.create({ metadata: { user_id: 'user_123', channel: 'web' } }); // Add system message await uc.append(ctx.id, { role: 'system', content: 'You are a helpful assistant.' }); // Simulate conversation await uc.append(ctx.id, { role: 'user', content: 'What is 2+2?' }); await uc.append(ctx.id, { role: 'assistant', content: 'The answer is 4.' }); await uc.append(ctx.id, { role: 'user', content: 'Thanks!' }); // Get current context for LLM const { data: messages, version } = await uc.get(ctx.id); console.log(`Current version: ${version}, Messages: ${messages.length}`); // Use with any LLM framework // const response = await openai.chat.completions.create({ // model: 'gpt-4', // messages: messages // }); // Fix a mistake - update the assistant's response await uc.update(ctx.id, { index: 2, content: 'The answer is 4. Would you like more math help?' }); // Time-travel: get context at version 2 const { data: oldMessages } = await uc.get(ctx.id, { version: 2 }); console.log('Messages at version 2:', oldMessages.length); // Fork context to create a branch const branch = await uc.create({ from: ctx.id, version: 2 }); console.log('Forked context:', branch.id); return ctx.id; } chatWithHistory().catch(console.error); ``` -------------------------------- ### UltraContext API: Create, Get, Append, Update, Delete in JavaScript Source: https://github.com/ultracontext/ultracontext-node/blob/main/README.md Provides examples of core UltraContext API methods: creating new contexts or forking existing ones, retrieving contexts with options for version, index, or history, appending single or multiple messages, updating messages by ID or index, and deleting messages by ID or index. All operations are performed using the 'uc' client instance. ```javascript // create - new context or fork from existing const ctx = await uc.create(); const fork = await uc.create({ from: 'ctx_abc123' }); // get - retrieve context (supports version, index, timestamp) const { data } = await uc.get('ctx_abc123'); const { data } = await uc.get('ctx_abc123', { version: 2 }); const { data } = await uc.get('ctx_abc123', { at: 5 }); const { data, versions } = await uc.get('ctx_abc123', { history: true }); // append - add messages (schema-free) await uc.append(ctx.id, { role: 'user', content: 'Hi' }); await uc.append(ctx.id, [{ role: 'user', content: 'Hi' }, { foo: 'bar' }]); // update - modify by id or index (auto-versions) await uc.update(ctx.id, { id: 'msg_xyz', content: 'Fixed!' }); await uc.update(ctx.id, { index: -1, content: 'Fix last message' }); // delete - remove by id or index (auto-versions) await uc.delete(ctx.id, 'msg_xyz'); await uc.delete(ctx.id, -1); ``` -------------------------------- ### Retrieve Context Information (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Demonstrates the `get` method for retrieving context data. It covers listing all contexts, retrieving a specific context by ID, and filtering by version, index position, or timestamp. It also shows how to fetch the full version history. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); // List all contexts const allContexts = await uc.get(); console.log(allContexts); // Output: { data: [{ id: 'ctx_abc123', metadata: {}, created_at: '...' }, ...] } // List contexts with limit const limitedContexts = await uc.get({ limit: 10 }); // Get a specific context const { data, version } = await uc.get('ctx_abc123'); console.log(data); // Output: [{ id: 'msg_1', index: 0, metadata: {}, role: 'user', content: 'Hello!' }, ...] // Get context at a specific version const { data: v2Data } = await uc.get('ctx_abc123', { version: 2 }); // Get context up to a specific index (e.g., first 5 messages) const { data: partialData } = await uc.get('ctx_abc123', { at: 5 }); // Get context before a specific timestamp const { data: beforeData } = await uc.get('ctx_abc123', { before: '2024-01-15T10:30:00Z' }); // Get context with full version history const { data, versions } = await uc.get('ctx_abc123', { history: true }); console.log(versions); // Output: [{ version: 1, created_at: '...', operation: 'create', affected: null }, ...] ``` -------------------------------- ### Initialize UltraContext Client (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Demonstrates how to create an instance of the UltraContext client. It shows basic initialization with an API key and advanced initialization with optional parameters like baseUrl, headers, timeout, and a custom fetch function. ```typescript import { UltraContext } from 'ultracontext'; // Basic initialization const uc = new UltraContext({ apiKey: 'uc_live_your_api_key_here' }); // Advanced initialization with all options const ucAdvanced = new UltraContext({ apiKey: 'uc_live_your_api_key_here', baseUrl: 'https://api.ultracontext.ai', // Optional: custom API endpoint headers: { 'X-Custom-Header': 'value' }, // Optional: additional headers timeoutMs: 30000, // Optional: request timeout in ms fetch: customFetchFn // Optional: custom fetch implementation }); ``` -------------------------------- ### Initialize and Use UltraContext in JavaScript Source: https://github.com/ultracontext/ultracontext-node/blob/main/README.md Demonstrates how to initialize the UltraContext client with an API key, create a new context, append a message, and then use the context data with an LLM generation function. Requires the 'ultracontext' package and a valid API key. ```javascript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); const ctx = await uc.create(); await uc.append(ctx.id, { role: 'user', content: 'Hello!' }); // use with any LLM framework const response = await generateText({ model, messages: ctx.data }); ``` -------------------------------- ### Create New or Fork Context (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Shows how to use the `create` method to initialize a new empty context or fork an existing one. Forking can be done from a specific version, index position, or timestamp, and allows for custom metadata. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); // Create a new empty context const newContext = await uc.create(); console.log(newContext); // Output: { id: 'ctx_abc123', metadata: {}, created_at: '2024-01-15T10:30:00Z' } // Fork from an existing context const forkedContext = await uc.create({ from: 'ctx_abc123' }); // Fork from a specific version const forkedFromVersion = await uc.create({ from: 'ctx_abc123', version: 3 }); // Fork from a specific index position const forkedFromIndex = await uc.create({ from: 'ctx_abc123', at: 5 }); // Fork with custom metadata const contextWithMeta = await uc.create({ from: 'ctx_abc123', metadata: { agent: 'support-bot', session: 'session_xyz' } }); ``` -------------------------------- ### Handle UltraContextHttpError (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Demonstrates how to catch and handle `UltraContextHttpError`, a custom error class for API request failures. It provides access to the status code, URL, and response body for detailed error inspection. Requires an initialized UltraContext instance. ```typescript import { UltraContext, UltraContextHttpError } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); try { const ctx = await uc.get('ctx_nonexistent'); } catch (error) { if (error instanceof UltraContextHttpError) { console.log('Status:', error.status); // e.g., 404 console.log('URL:', error.url); // The request URL console.log('Body:', error.bodyText); // Response body text console.log('Message:', error.message); // 'UltraContext request failed: 404 https://...' // Handle specific error codes if (error.status === 404) { console.log('Context not found'); } else if (error.status === 401) { console.log('Invalid API key'); } else if (error.status === 429) { console.log('Rate limited, please retry'); } } } ``` -------------------------------- ### Append Messages to UltraContext (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Appends one or more schema-free JSON messages to an existing UltraContext. Supports single messages, batch appends, and custom data structures. Requires an initialized UltraContext instance. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); const ctx = await uc.create(); // Append a single message const result = await uc.append(ctx.id, { role: 'user', content: 'Hello, how can you help me today?' }); console.log(result); // Output: { data: [{ id: 'msg_1', index: 0, metadata: {}, role: 'user', content: '...' }], version: 1 } // Append assistant response await uc.append(ctx.id, { role: 'assistant', content: 'I can help you with various tasks. What would you like to do?' }); // Append multiple messages at once const batchResult = await uc.append(ctx.id, [ { role: 'user', content: 'Tell me about TypeScript' }, { role: 'assistant', content: 'TypeScript is a typed superset of JavaScript...' }, { role: 'user', content: 'Show me an example' } ]); // Append custom schema-free data await uc.append(ctx.id, { type: 'tool_call', tool: 'web_search', query: 'TypeScript examples', results: ['result1', 'result2'], metadata: { latency_ms: 150 } }); ``` -------------------------------- ### Update Messages in UltraContext (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Updates existing messages within an UltraContext by their ID or index. Supports negative indices for the last messages and batch updates. Each update creates a new version of the context. Requires an initialized UltraContext instance. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); // Update by message ID const updateById = await uc.update('ctx_abc123', { id: 'msg_xyz789', content: 'Updated content here' }); console.log(updateById); // Output: { data: [{ id: 'msg_xyz789', index: 2, metadata: {}, content: '...' }], version: 3 } // Update by index (0-based) await uc.update('ctx_abc123', { index: 0, content: 'Fix the first message' }); // Update the last message using negative index await uc.update('ctx_abc123', { index: -1, content: 'Fix the last message' }); // Update multiple messages at once await uc.update('ctx_abc123', [ { id: 'msg_1', content: 'Updated first' }, { id: 'msg_2', content: 'Updated second' }, { index: -1, role: 'assistant', content: 'Updated last' } ]); // Update with custom metadata await uc.update('ctx_abc123', { id: 'msg_xyz', content: 'New content' }, { metadata: { edited_by: 'admin', reason: 'correction' } } ); ``` -------------------------------- ### Delete Messages from UltraContext (TypeScript) Source: https://context7.com/ultracontext/ultracontext-node/llms.txt Removes messages from an UltraContext by their ID or index. Supports deleting single or multiple messages, including using negative indices. Each deletion creates a new version, preserving history. Requires an initialized UltraContext instance. ```typescript import { UltraContext } from 'ultracontext'; const uc = new UltraContext({ apiKey: 'uc_live_...' }); // Delete by message ID const deleteResult = await uc.delete('ctx_abc123', 'msg_xyz789'); console.log(deleteResult); // Output: { data: [{ id: 'msg_xyz789', index: 2, metadata: {}, ... }], version: 4 } // Delete by index await uc.delete('ctx_abc123', 0); // Delete first message // Delete the last message using negative index await uc.delete('ctx_abc123', -1); // Delete multiple messages by ID await uc.delete('ctx_abc123', ['msg_1', 'msg_2', 'msg_3']); // Delete multiple messages by index await uc.delete('ctx_abc123', [0, 2, 4]); // Delete with metadata (for audit purposes) await uc.delete('ctx_abc123', 'msg_xyz', { metadata: { deleted_by: 'user_123', reason: 'spam' } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.