@theoremjs/agents 0.3.5-next.901.g4c6ea22 → 0.3.5-next.902.g334d23a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,9 +22,10 @@ declare function profileCanary(profileId: string, system: readonly SystemPiece[]
22
22
  declare function requestGivesCanary(request: Pick<ModelRequest, 'input' | 'history'>, canary: string, scanned: WeakSet<object>): boolean;
23
23
  /**
24
24
  * Removes pre-existing user-data fences, trims the input, and encloses the result
25
- * in the canonical user-data fence used by the guardrail prompt contract.
25
+ * in the canonical user-data fence used by the guardrail prompt contract, with
26
+ * the message's id on the opening tag when there is one.
26
27
  */
27
- declare function wrapUserData(text: string): string;
28
+ declare function wrapUserData(text: string, messageId?: string): string;
28
29
  /**
29
30
  * Encloses context in the fence that names its sender, with any fence already in it removed, so
30
31
  * neither the page nor the person can pass text off as the host's.
@@ -62,10 +62,12 @@ function stripUserFences(text) {
62
62
  }
63
63
  /**
64
64
  * Removes pre-existing user-data fences, trims the input, and encloses the result
65
- * in the canonical user-data fence used by the guardrail prompt contract.
65
+ * in the canonical user-data fence used by the guardrail prompt contract, with
66
+ * the message's id on the opening tag when there is one.
66
67
  */
67
- function wrapUserData(text) {
68
- return `${USER_OPEN}\n${stripUserFences(text)}\n${USER_CLOSE}`;
68
+ function wrapUserData(text, messageId) {
69
+ const open = messageId === undefined ? USER_OPEN : `<user_data id="${messageId}">`;
70
+ return `${open}\n${stripUserFences(text)}\n${USER_CLOSE}`;
69
71
  }
70
72
  /**
71
73
  * Encloses context in the fence that names its sender, with any fence already in it removed, so
@@ -53,6 +53,14 @@ function sanitizeContext(context, readerFor) {
53
53
  return { context: read, events, blocked };
54
54
  }
55
55
  const PROJECT_ID_OK = /^[A-Za-z0-9._-]+$/;
56
+ /** Short enough that a model echoing the whole opening tag still reads as a fence to strip. */
57
+ const MESSAGE_ID_OK = /^[A-Za-z0-9._:-]{1,32}$/;
58
+ /** The message id as given; throws when it would not sit safely on the fence. */
59
+ function checkMessageId(id) {
60
+ if (id === undefined || MESSAGE_ID_OK.test(id))
61
+ return id;
62
+ throw new TheoremError('request', 'input.messageId must be 1 to 32 letters, digits, ".", "_", ":" or "-"');
63
+ }
56
64
  /** The project id trimmed, or `undefined` when it is empty or has illegal characters. */
57
65
  function sanitizeProjectId(id) {
58
66
  const trimmed = id?.trim();
@@ -107,6 +115,7 @@ function sanitizeTurnRequestWithEvents(req, profile) {
107
115
  const { detect } = resolveGuardrailPolicy(profile.guardrails);
108
116
  const at = recordOf(REQUEST_BOUNDARIES, (boundary) => boundaryReader(boundary, detect));
109
117
  const input = req.input ?? {};
118
+ const messageId = checkMessageId(input.messageId);
110
119
  const text = input.text === undefined ? undefined : at.user.read(input.text);
111
120
  const system = req.system === undefined
112
121
  ? undefined
@@ -139,6 +148,7 @@ function sanitizeTurnRequestWithEvents(req, profile) {
139
148
  input: {
140
149
  ...input,
141
150
  text,
151
+ ...(messageId === undefined ? {} : { messageId }),
142
152
  options,
143
153
  ...(told.context ? { context: told.context } : {}),
144
154
  repair,
@@ -52,6 +52,10 @@ export const REQUEST_FIELDS = withFieldDocs({
52
52
  'runTurn.input.repair': {
53
53
  type: 'TurnRepairRequest',
54
54
  },
55
+ 'runTurn.input.messageId': {
56
+ type: 'string',
57
+ unset: 'The fence carries no id',
58
+ },
55
59
  'runTurn.input.historyTokens': {
56
60
  type: 'number',
57
61
  unset: "Theorem's own estimate",
@@ -40,7 +40,8 @@ export async function historyHash(history) {
40
40
  };
41
41
  const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(stable(history.map((message) => {
42
42
  const canonicalText = (text) => message.role === 'user'
43
- ? (text.match(/^<user_data>\n([\s\S]*)\n<\/user_data>$/)?.[1] ?? text)
43
+ ? (text.match(/^<user_data(?: id="[^"\n]*")?>\n([\s\S]*)\n<\/user_data>$/)?.[1] ??
44
+ text)
44
45
  : text;
45
46
  const parts = [
46
47
  ...(message.content === undefined
@@ -374,6 +374,7 @@ export const FIELD_DOCS = {
374
374
  'runTurn.input.historyTokens': "Your count of the history's tokens, used by compaction that meters history.",
375
375
  'runTurn.input.inputTokens': "Your count of the whole prompt's tokens, used by compaction that meters input before the call.",
376
376
  'runTurn.input.lateResults': 'Background tool calls that settled after their turn, as the host stored them. The model reads each as a tool result, and this reply counts it as a remote read when the call was remote. Pass each one once: the turn emits it as a late_results event, so a history built from events holds it.',
377
+ 'runTurn.input.messageId': 'Your id for the user\'s message, shown to the model on its fence as `<user_data id="…">`: 1 to 32 letters, digits, `.`, `_`, `:` or `-`; any other id is refused.',
377
378
  'runTurn.input.options': "The value a person picked for each option in the profile's inputs.options; an option or a value the profile does not declare is refused.",
378
379
  'runTurn.input.repair': 'A reply your host rejected and why, so that the model writes it again.',
379
380
  'runTurn.input.text': "The user's message.",
@@ -113,7 +113,10 @@ function extractTextPart(profile, req) {
113
113
  if (profile.type === 'speech') {
114
114
  return promptText ? { type: 'text', text: promptText } : null;
115
115
  }
116
- const blocks = [...contextBlocks(req), ...(promptText ? [wrapUserData(promptText)] : [])];
116
+ const blocks = [
117
+ ...contextBlocks(req),
118
+ ...(promptText ? [wrapUserData(promptText, req.input?.messageId)] : []),
119
+ ];
117
120
  return blocks.length > 0 ? { type: 'text', text: blocks.join('\n\n') } : null;
118
121
  }
119
122
  /** Each sender's context in its own fence, the host's first: the model reads it before the user's message. */
@@ -1020,6 +1020,11 @@ export interface TurnRepairRequest {
1020
1020
  export interface TurnInput {
1021
1021
  /** The user's message. */
1022
1022
  text?: string;
1023
+ /**
1024
+ * Your id for the user's message, shown to the model on its fence as `<user_data id="…">`: 1 to
1025
+ * 32 letters, digits, `.`, `_`, `:` or `-`; any other id is refused.
1026
+ */
1027
+ messageId?: string;
1023
1028
  /**
1024
1029
  * The value a person picked for each option in the profile's inputs.options; an option or a value
1025
1030
  * the profile does not declare is refused.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.5-next.901.g4c6ea22",
3
+ "version": "0.3.5-next.902.g334d23a",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",