Overview
Concrete utility functions that resolve context IDs to Context objects and format context arrays into human-readable text for LLM prompt augmentation.
Description
The resolveContexts function accepts a mixed array of Context objects and string IDs, resolves each string ID through a provided `getContext` function, validates Context objects structurally, and returns an array of resolved Context objects. The formatContextsForPrompt function takes an array of Context objects and produces a formatted string with each context wrapped as `[type: label]\nvalue`, joined by double newlines. Together these functions implement the context augmentation pipeline: resolution + formatting.
Usage
- Resolve context IDs: call `resolveContexts` with the context manager's `getContext` method.
- Format for prompt: call `formatContextsForPrompt` to produce the text prefix prepended to user input.
- Both are used internally by `AIPex.chat` when `options.contexts` is provided.
Code Reference
Source Location
- Repository: AIPex
- File: packages/core/src/context/utils.ts
- Lines: L29-53 (formatContextsForPrompt), L77-97 (resolveContexts)
Signature
export async function resolveContexts(
contextOrIds: (Context | string)[],
getContext: (id: string) => Promise<Context | null>,
): Promise<Context[]>
export function formatContextsForPrompt(contexts: Context[]): string
export function isContext(value: unknown): value is Context
interface Context {
id: string;
type: string;
providerId: string;
label: string;
value: string | File | Blob;
}
Import
import {
resolveContexts,
formatContextsForPrompt,
} from "@aipexstudio/aipex-core";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| contextOrIds |
string)[] |
Yes |
Mixed array of Context objects or string IDs
|
| getContext |
null> |
Yes |
Function to fetch context by ID
|
| contexts |
Context[] |
Yes |
Array of Context objects (for formatContextsForPrompt)
|
Outputs
| Name |
Type |
Description
|
| returns |
Context[] |
Resolved context objects (resolveContexts)
|
| returns |
string |
Formatted context text (formatContextsForPrompt)
|
Usage Examples
Resolve and Format Contexts
import { resolveContexts, formatContextsForPrompt } from "@aipexstudio/aipex-core";
// Resolve context IDs using context manager
const contexts = await resolveContexts(
["current-page", "open-tabs"],
contextManager.getContext.bind(contextManager),
);
// Format for prompt
const contextText = formatContextsForPrompt(contexts);
// Result: "[page: Homepage]\nWelcome to...\n\n[tabs: Open Tabs]\n1. Google\n2. GitHub"
const finalInput = `${contextText}\n\n${userInput}`;
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.