@centerforagenticai/pi-multi-account 0.1.5 → 0.1.7

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.
@@ -1,136 +1,26 @@
1
- import {
2
- isContextOverflow,
3
- isRetryableAssistantError,
4
- type AssistantMessage,
5
- } from "@earendil-works/pi-ai";
6
- import { sanitizeDiagnosticText } from "./diagnostics.js";
1
+ import { isContextOverflow, isRetryableAssistantError, type AssistantMessage } from "@earendil-works/pi-ai";
7
2
 
8
- /** Fixed fallbacks for a structured provider stop; none matches a host re-dispatch pattern. */
3
+ /** Fixed public reasons: provider prose and unknown reason payloads are never retained. */
9
4
  export const REFUSAL_FALLBACK_MESSAGE = "The model refused to complete the request";
10
- export const UNKNOWN_STOP_FALLBACK_MESSAGE =
11
- "Provider stopped with an unrecognized stop reason";
12
-
13
- /**
14
- * Appended when a reworded unknown stop reason omits words a host predicate acts
15
- * on, so the published reason does not read as complete. Neither pinned
16
- * predicate matches it, and the marked message is probed again before use.
17
- */
5
+ export const UNKNOWN_STOP_FALLBACK_MESSAGE = "Provider stopped with an unrecognized stop reason";
6
+ // Kept for source compatibility; reasons are now entirely withheld, not reworded.
18
7
  export const PARTLY_WITHHELD_MARKER = " (partly withheld)";
19
8
 
20
- /** Upper bound on the normalized stop reason named in a reworded message. */
21
- const MAX_NAMED_REASON_LENGTH = 64;
22
-
23
- /**
24
- * Whether the pinned host would re-dispatch an error terminal carrying `text`.
25
- *
26
- * The host re-dispatches when either retry predicate matches the prose:
27
- * `AgentSession._isRetryableError` runs pi-ai `isRetryableAssistantError`, and
28
- * `_checkCompaction` runs `isContextOverflow` before compacting and retrying once
29
- * (pi-coding-agent dist/core/agent-session.js, both from `@earendil-works/pi-ai`).
30
- * An unreadable predicate result counts as a match, so the caller keeps looking
31
- * for a safer form.
32
- */
9
+ /** Probe pinned host predicates without publishing the provider DTO. */
33
10
  export function hostWouldRedispatch(message: AssistantMessage, text: string): boolean {
34
- try {
35
- const probe = { ...message, errorMessage: text };
36
- return isRetryableAssistantError(probe) || isContextOverflow(probe, 0);
37
- } catch {
38
- return true;
39
- }
40
- }
41
-
42
- function namedReasonMessage(reason: string): string {
43
- return `Provider stopped (reason: ${reason})`;
44
- }
45
-
46
- /**
47
- * Normalizes a bounded, sanitized raw stop reason into plain words: every run of
48
- * non-alphanumeric characters (underscores included) becomes one space, and the
49
- * result is capped at a word boundary where possible.
50
- */
51
- function normalizedReasonWords(rawStopReason: unknown): string[] {
52
- if (typeof rawStopReason !== "string") return [];
53
- const words = sanitizeDiagnosticText(rawStopReason)
54
- .replace(/[^A-Za-z0-9]+/g, " ")
55
- .trim()
56
- .split(" ")
57
- .filter((word) => word.length > 0);
58
- const kept: string[] = [];
59
- let length = 0;
60
- for (const word of words) {
61
- const next = length === 0 ? word.length : length + 1 + word.length;
62
- if (next > MAX_NAMED_REASON_LENGTH) {
63
- if (kept.length === 0) kept.push(word.slice(0, MAX_NAMED_REASON_LENGTH));
64
- break;
65
- }
66
- kept.push(word);
67
- length = next;
68
- }
69
- return kept;
70
- }
71
-
72
- /**
73
- * Names an unknown stop reason in a message neither host predicate acts on.
74
- *
75
- * The first form keeps every normalized word. When that still matches a host
76
- * predicate (a reason containing "overloaded" or "timeout", say), the second
77
- * form admits words in order and drops each one whose addition would make the
78
- * message match. When any word was dropped the result ends with
79
- * `PARTLY_WITHHELD_MARKER`; that marked text is probed too, and an unsafe or
80
- * empty result yields `undefined`.
81
- */
82
- function unknownStopNamingReason(
83
- message: AssistantMessage,
84
- rawStopReason: unknown,
85
- ): string | undefined {
86
- const words = normalizedReasonWords(rawStopReason);
87
- if (words.length === 0) return undefined;
88
- const reworded = namedReasonMessage(words.join(" "));
89
- if (!hostWouldRedispatch(message, reworded)) return reworded;
90
- const kept: string[] = [];
91
- for (const word of words) {
92
- const tentative = namedReasonMessage([...kept, word].join(" "));
93
- if (!hostWouldRedispatch(message, tentative)) kept.push(word);
94
- }
95
- if (kept.length === 0) return undefined;
96
- const named = namedReasonMessage(kept.join(" "));
97
- if (kept.length === words.length) return named;
98
- const marked = `${named}${PARTLY_WITHHELD_MARKER}`;
99
- return hostWouldRedispatch(message, marked) ? undefined : marked;
11
+ try {
12
+ // Private predicate probe only; this object is never published or retained.
13
+ const probe = { ...message, errorMessage: text };
14
+ return isRetryableAssistantError(probe) || isContextOverflow(probe, 0);
15
+ } catch { return true; }
100
16
  }
101
17
 
102
- /**
103
- * The public error text for a structured refusal or unknown provider stop.
104
- *
105
- * The pinned host re-dispatches an error terminal from its prose alone (see
106
- * `hostWouldRedispatch`). A refusal explanation or stop reason is
107
- * provider-authored, so text such as "overloaded", "prompt is too long", or
108
- * "model_context_window_exceeded" would make the host send the stopped request
109
- * again. The structured code alone selects this path; the provider text is kept
110
- * only when neither host predicate would act on it. An unknown stop is then
111
- * reworded so it still names its reason, and only when no reworded form is safe
112
- * is a fixed fallback published. A refusal whose explanation is unsafe publishes
113
- * its fixed fallback directly. A message without the structured code, or that is
114
- * not an error terminal, yields `undefined` and must be published unchanged.
115
- */
116
- export function hostFinalStopMessage(
117
- message: AssistantMessage,
118
- ): { readonly errorMessage: string } | undefined {
119
- const code = (message as { code?: unknown }).code;
120
- if (message.stopReason !== "error") return undefined;
121
- if (code !== "refusal" && code !== "unknown_stop") return undefined;
122
- const candidate = message.errorMessage;
123
- if (
124
- typeof candidate === "string" &&
125
- candidate.length > 0 &&
126
- !hostWouldRedispatch(message, candidate)
127
- ) {
128
- return { errorMessage: candidate };
129
- }
130
- if (code === "refusal") return { errorMessage: REFUSAL_FALLBACK_MESSAGE };
131
- return {
132
- errorMessage:
133
- unknownStopNamingReason(message, message.rawStopReason) ??
134
- UNKNOWN_STOP_FALLBACK_MESSAGE,
135
- };
18
+ /** A structured stop is an answer, never retry/overflow evidence or a prose retention permission. */
19
+ export function hostFinalStopMessage(message: AssistantMessage): {
20
+ readonly errorMessage: string;
21
+ readonly rawStopReason: "refusal" | "unknown_stop";
22
+ } | undefined {
23
+ const code = Object.getOwnPropertyDescriptor(message, "code")?.value;
24
+ if (message.stopReason !== "error" || (code !== "refusal" && code !== "unknown_stop")) return undefined;
25
+ return { errorMessage: code === "refusal" ? REFUSAL_FALLBACK_MESSAGE : UNKNOWN_STOP_FALLBACK_MESSAGE, rawStopReason: code };
136
26
  }