This commit is contained in:
Brent Larsen
2026-10-07 09:27:20 -05:00
parent cf1ab0a7af
commit bf02eff6ea
12 changed files with 808 additions and 91 deletions
+149 -12
View File
@@ -1,23 +1,160 @@
# Perry
Perry is a small utility for working with persistent conversational agents.
Perry is a small provider-neutral utility for forming and operating persistent conversational agents through LLMs.
It grew out of Chat2. Perry is not a scheduler, artifact graph, translation pipeline, or full-featured agent framework. Applications own why work happens and what the work means. Perry owns the conversational agent mechanics around a local model.
Perry is named for an agent who does not do much but has a knack for doing exactly enough to get the job done and avoid catastrophe. Applications own why work happens and what the work means. Perry owns the mechanics of talking to the requested LLM provider and maintaining the conversation needed to do it.
Current API:
## Where Lego Towers came from
Perry grew out of a simple question while building an Ollama chat client: what does an LLM need in order to persist a session that can be resumed, branched, or have information injected into it?
The answer was already present in the chat API: conversation history.
Persist the conversation history and the session is persistent. Copy it and the session branches. Compose useful subsets of history and those subsets become reusable Lego pieces. Stack the pieces in order and the result is a Lego Tower: a conversational history that forms an agent's awareness before repeated work is placed in front of it.
Perry deliberately keeps that observation simple. A Lego is conversation data, not a framework object, registry entry, graph node, or special session type.
## Provider-neutral conversations
A Perry conversation has a Perry-defined shape. It is not an LM Studio message or any other provider's native request object.
The basic message shape is:
```js
{ role: "user", content: "..." }
```
Perry accepts the fields it understands and provider implementations transform that conversation into whatever their backend expects. A provider may happen to need little or no transformation, but consumers must not rely on provider-specific fields passing through Perry.
Reasoning may be retained on replies for diagnostics, but it is not normally fed back into active conversation history.
## Configuration
Create a lightweight Perry instance with an explicit provider and exact model name:
```js
const Perry = require("perry");
const perry = Perry({
provider: "lm-studio",
model: "gemma-4-31b-it"
});
```
Those are currently the only Perry settings.
Perry instances are intentionally cheap and independent. An application can pass one instance around, create one per pipeline stage, create one per agent, or create one per request. Perry does not assume application-global LLM configuration.
Current provider: `lm-studio`.
Ollama was supported in the older Chat2 lineage, but no active Ollama implementation survived into the extracted Perry source. It is intentionally not being recreated until there is a real use for it again.
## Preparing an agent
```js
const agent = perry.prepare("Edith", systemPrompt, [
identityLego,
valuesLego,
translatorLego
]);
```
Each Lego is simply a conversation subset. Perry assembles the system prompt and Lego histories into the agent's formative conversation.
`prepare()` does not contact or load a model.
## Asking for work
```js
const reply = await perry.ask(agent, prompt, {
onReasoning(text) {
// The consumer decides whether and where to display this.
},
onContent(text) {
// The consumer decides whether and where to display this.
}
});
console.log(reply.content);
```
Perry itself never writes user-facing output to the console. It streams through callbacks, returns replies and diagnostics, and throws errors. UI and logging belong to the consumer.
A normal reply is shaped roughly like:
```js
{
role: "assistant",
content: "...",
reasoning: "...", // when available
provider: {
name: "lm-studio",
model: "gemma-4-31b-it",
continuationPages: 0,
pages: [/* provider diagnostics */]
}
}
```
## Follow-up conversations
`ask()` starts from the agent's prepared formation each time. It does not mutate the prepared agent.
For an intentional follow-up to a previous exchange:
```js
const nextReply = await perry.followUp(
agent,
originalPrompt,
previousReply,
followUpPrompt
);
```
This keeps branching ordinary: conversation history is data, and a branch is simply a different history.
## Optional text cleanup
`Perry.cleanText(text)` is a pure opt-in helper for common LLM presentation artifacts. It does not run automatically and knows nothing about an application's envelopes, labels, artifacts, or document structure.
It normalizes a small observed set of generic presentation habits into portable text / mini-markdown conventions, such as TeX `\text{...}` wrappers, common TeX arrow symbols, and ordinary Markdown emphasis. Unknown TeX is left visible rather than interpreted.
Consumers decide whether a particular semantic text boundary should use it.
## Availability and exact model identity
`warmUp()` asks the configured provider to establish whatever certainty it can about the requested model.
LM Studio can inspect and load models, so failure to make the exact requested model available is fatal. Perry does not silently choose a substitute model and does not understand application-level model aliases.
`ask()` performs this check before each attempt. If LM Studio explicitly reports a response model and it differs from the exact configured model name, Perry rejects the response with `PERRY_MODEL_MISMATCH`. If a provider cannot supply model identity evidence, uncertainty by itself is not treated as failure.
Applications that want friendly aliases such as `translator-large` should resolve those aliases to the exact provider/model settings before constructing Perry.
## Continuation and retries
Perry automatically continues LM Studio responses that explicitly end as incomplete. If an agent produces reasoning but no final answer and provider-native continuation is unavailable, Perry can continue through ordinary conversation history.
Transient transport failures are retried up to ten times using the existing backoff policy. Partial failed responses are not appended to the agent's conversation.
This retry policy is intentionally unchanged in 0.2. A better recovery sequence has been observed and is recorded in `docs/ideas.md`; it will be changed only after that behavior is discussed and deliberately implemented.
## Repetition self-destruct
The LM Studio provider watches reasoning and final-content streams independently for exact contiguous periodic repetition. If the tail becomes the same token sequence repeated seven complete times with no novel tokens between repetitions, it aborts that response and raises a retryable `PERRY_REPETITION_LOOP` error.
The detector is deliberately literal. Similar ideas with changing token sequences are allowed to continue; Perry intervenes only after the stream locks into an exact repeated orbit. Pattern periods from one through 256 tokens are checked.
This remains LM Studio-specific today because that is the implementation reality we have. It should not be generalized into a provider interface until repeated provider implementations demonstrate a real common shape.
## API
- `Perry({ provider, model })`
- `Perry.cleanText(text)`
- `warmUp()`
- `prepare(name, systemPrompt, segments)`
- `ask(agent, input, options)`
- `followUp(agent, originalInput, previousReply, input, options)`
- `model()`
Current backend: LM Studio Responses API. The extracted source contained no active Ollama backend, so none was resurrected.
## Repetition self-destruct
Perry watches reasoning and final-content streams independently for exact contiguous periodic repetition. If the tail becomes the same token sequence repeated seven complete times with no novel tokens between repetitions, Perry aborts that response and raises a retryable `PERRY_REPETITION_LOOP` error.
The detector is deliberately literal. Similar ideas with changing token sequences are allowed to continue; Perry intervenes only after the stream locks into an exact repeated orbit. Pattern periods from one through 256 tokens are checked.
The normal retry layer then starts the response again without appending the pathological partial response to the conversation.
See `AGENTS.md` for development philosophy and scope, and `docs/` for intentional behavior, project history, and deferred ideas.