Skip to content

Add the ai package with rules, defaults, and attachments - #339

Merged
lahfir merged 41 commits into
laf/ai-core-settingsfrom
laf/ai-rules
Nov 17, 2026
Merged

lahfir merged 41 commits into
laf/ai-core-settingsfrom
laf/ai-rules

Conversation

@lahfir

@lahfir lahfir commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor
  1. There was no ai package, so rules, defaults, and attachments had nowhere to live.
  2. Adds @emulators/ai with rule validation and matching, deterministic defaults, and attachment decoding, and registers nothing yet.
  3. Builds on Add opt-in service settings to core #338.

@vercel

vercel Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
emulate Ready Ready Preview, v0 Nov 17, 2026 4:09am UTC

@lahfir
lahfir added this pull request to stack #344 November 5, 2026 09:43
@socket-security

socket-security Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​ai-sdk/​openai@​3.0.52731008898100
Addednpm/​@​ai-sdk/​openai@​4.0.102731008698100
Addednpm/​openai@​7.43.075100100100100
Addednpm/​@​ai-sdk/​openai-compatible@​3.0.78981008498100
Addednpm/​zod@​4.6.710010010091100
Addednpm/​ai@​7.0.1539910010099100

View full report

New @emulators/ai package with the pieces that need no server yet:
- shared types and error classes
- rule validation and matching, with per-rule budgets
- deterministic defaults: echo text, schema-valid objects, forced tool calls
- attachment decoding that keeps metadata only and strips URL credentials

Nothing registers the service yet. index.ts only exports types and errors.
Rules can declare variants instead of one reply, and ai.random.seed sets the seed that picks between them. chunkDelayMs also accepts a list so frames can be paced unevenly. Parsing only; the engine picks and paces in the next change in the stack.
A variant is now either a reply or an error, and the parser requires exactly one of the two. This lets a seeded sequence mix normal replies with failures.
A variant is a full alternative body, so it can differ in how it ends and how it is delivered, not only in its content. Rule-level values stay as the defaults.
Strict response_format and text.format schemas are validated before generation with OpenAI's own error text and param. The keyword sets live per provider in schema-dialects.ts; Anthropic's rules and message are encoded there too. The generator now accepts format, pattern, and oneOf, and fills common string formats with stable sample values instead of failing.
Anthropic's OpenAI compatibility layer keeps OpenAI's error envelope but applies Claude's schema rules, so the provider rules follow the model rather than the endpoint. schemaFailure returns the provider's wording and the codec wraps it in its own envelope.
The package shipped at the previous release version while the repo moved to 0.13.0, so the version check failed for every branch above it.
Use tsc-ts for type-check, oxlint, declaration emit through emit-types, the
build tsconfig, and the shared 0.13.1 version, matching the other packages.
Schema sampling now bounds the size of generated strings and refuses const and enum values nested past the depth limit, so a hostile schema cannot make the default reply blow up.
A string of encoded JSON is now rejected at startup and on PUT /_ai/rules with a message pointing at the fix.
@lahfir
lahfir marked this pull request as ready for review November 9, 2026 21:16
Comment thread packages/@emulators/ai/src/defaults.ts Resolved
Comment thread packages/@emulators/ai/src/defaults.ts Resolved

@Railly Railly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just a few comments

Comment thread packages/@emulators/ai/src/defaults.ts Resolved
Comment thread packages/@emulators/ai/src/defaults.ts Resolved
mergeSchema let the branch override its siblings, so a stricter sibling
bound next to $ref could be dropped and the sampled value was rejected.
It now takes the tighter bound, intersects type and enum, unions
required, and merges properties, items and additionalProperties
recursively. Keywords that cannot be intersected keep the branch value
and the existing matches() check rejects bad samples.
Pattern checks used a shared wall-clock budget and passed the remaining
milliseconds as the vm timeout, so scheduling noise could turn a valid
patterned schema into a 400. Each check now costs a fixed amount of the
existing work budget and every vm run uses the same constant timeout. A
catastrophic expression is still stopped by that timeout.
A forced tool whose parameters had no type, such as {} or only
properties, sampled as null and produced arguments "null". The schema
now gets type object when it has none, the same as a missing parameters
field already did.
A sibling multipleOf next to a $ref target with its own multipleOf was
overridden, so a satisfiable schema returned 400. It now uses the least
common multiple for integers, or the larger value when it is a multiple
of the smaller.

Two different known formats on both sides cannot both be sampled and
matches() does not check format, so the sample could violate the
sibling. The merge now yields an unsatisfiable schema for that case.
Each pattern check had its own timeout and the anyOf and oneOf loop
treated that as a failed branch, so N catastrophic branches cost N
timeouts and blocked the server. A timeout now aborts the whole sample,
so one request pays it once.

An invalid regular expression now says so instead of reporting a time
limit, and a pattern failure inside nested schemas names the property
path instead of the root.
A forced tool call sampled whatever its parameters schema allowed, so
true, nullable, null, string and array schemas gave null, a string or an
array. The schema is now narrowed to an object (nullable and array types
are dropped), and a schema that cannot be an object is rejected with a
400 naming tools.<name>.parameters.
sampleAlternative gave up on a oneOf branch when its first sample also
matched another branch, even though a value matching only that branch
existed (for example "" for minLength 1 versus maxLength 5, or 1.5 for
integer versus number).

When that happens it now retries the branch with a small fixed set of
variants that step outside the other branches: optional properties made
required, bounds flipped past the other branch's bounds, enum values,
and a half step for numbers. Every candidate still goes through the
existing matches check, so a wrong value is never returned, and the
failure when no value exists is unchanged. anyOf is untouched.
nullable only widens the type it sits next to. matches() returned true
for null as soon as the node had nullable, and mergeSchema copied
nullable onto a typed $ref or branch target, so a sibling nullable next
to a string target sampled null. matches() now skips only the type check
for null, and the merge drops nullable when the other side has a
non-nullable type.
# Conflicts:
#	pnpm-lock.yaml

@Railly Railly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, one small nit

Comment thread packages/@emulators/ai/src/defaults.ts Resolved
Comment thread packages/@emulators/ai/src/defaults.ts Resolved
Each pattern check already had a timeout and a work cost, but nothing bounded their sum, so many slow checks could block the server for a minute. A sampling now aborts once its pattern checks total more than 2 seconds, through the same path as a timed-out check.
A chat file was rejected by its filename before its data URL type was read, so a declared PDF with no .pdf name failed. The PDF check now runs once, on the type inline attachments already resolve, with the declared type taking precedence over the filename.
The hint always suggested matching-reply, so applying it for a second unmatched model hit a duplicate rule id error. strictMissFailure now takes the configured rule ids and suggests the first free matching-reply id.
PUT /_ai/rules replaces the whole configuration, and the suggested body had no strict field, so sending it turned strict mode off and later misses got default replies. The body now sets strict to true and the message says the request replaces the configuration.
@lahfir
lahfir merged commit fe36b7e into main Nov 17, 2026
14 checks passed

This branch was successfully deployed

1 active deployment
Preview — 4aea41ed Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants