@moxxy/sdk 0.14.2 → 0.14.4

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.
Files changed (44) hide show
  1. package/dist/compactor-helpers.d.ts.map +1 -1
  2. package/dist/compactor-helpers.js +9 -5
  3. package/dist/compactor-helpers.js.map +1 -1
  4. package/dist/mode/collect-stream.d.ts +68 -0
  5. package/dist/mode/collect-stream.d.ts.map +1 -0
  6. package/dist/mode/collect-stream.js +181 -0
  7. package/dist/mode/collect-stream.js.map +1 -0
  8. package/dist/mode/project-messages.d.ts +84 -0
  9. package/dist/mode/project-messages.d.ts.map +1 -0
  10. package/dist/mode/project-messages.js +392 -0
  11. package/dist/mode/project-messages.js.map +1 -0
  12. package/dist/mode/single-shot.d.ts +17 -0
  13. package/dist/mode/single-shot.d.ts.map +1 -0
  14. package/dist/mode/single-shot.js +53 -0
  15. package/dist/mode/single-shot.js.map +1 -0
  16. package/dist/mode/stable-hash.d.ts +7 -0
  17. package/dist/mode/stable-hash.d.ts.map +1 -0
  18. package/dist/mode/stable-hash.js +20 -0
  19. package/dist/mode/stable-hash.js.map +1 -0
  20. package/dist/mode/stuck-loop.d.ts +39 -0
  21. package/dist/mode/stuck-loop.d.ts.map +1 -0
  22. package/dist/mode/stuck-loop.js +51 -0
  23. package/dist/mode/stuck-loop.js.map +1 -0
  24. package/dist/mode-helpers.d.ts +15 -175
  25. package/dist/mode-helpers.d.ts.map +1 -1
  26. package/dist/mode-helpers.js +14 -666
  27. package/dist/mode-helpers.js.map +1 -1
  28. package/dist/tunnel.d.ts.map +1 -1
  29. package/dist/tunnel.js +11 -1
  30. package/dist/tunnel.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/compactor-helpers.test.ts +21 -0
  33. package/src/compactor-helpers.ts +10 -5
  34. package/src/mode/collect-stream.ts +247 -0
  35. package/src/mode/project-messages.test.ts +121 -0
  36. package/src/mode/project-messages.ts +461 -0
  37. package/src/mode/single-shot.ts +63 -0
  38. package/src/mode/stable-hash.ts +20 -0
  39. package/src/mode/stuck-loop.ts +89 -0
  40. package/src/mode-helpers.ts +32 -850
  41. package/src/mode.test.ts +20 -0
  42. package/src/token-accounting.test.ts +90 -0
  43. package/src/tunnel.test.ts +21 -0
  44. package/src/tunnel.ts +10 -1
@@ -1,178 +1,18 @@
1
- import type { ProviderMessage, TokenUsage } from './provider.js';
2
- import type { ModeContext } from './mode.js';
3
- import type { StopReason } from './provider-utils.js';
4
- import type { Skill } from './skill.js';
5
1
  /**
6
- * Shared bits used by every loop strategy: a typed tool-use struct and a
7
- * common stream-collection helper that runs `onBeforeProviderCall` hooks
8
- * and reduces a provider stream down to `{text, toolUses, stopReason}`.
2
+ * Barrel for the shared mode/loop helpers. The implementations now live in
3
+ * focused single-responsibility modules under `./mode/`; this file re-exports
4
+ * them so every existing `from './mode-helpers.js'` import (and the
5
+ * `@moxxy/sdk` index barrel) keeps working byte-identically.
9
6
  *
10
- * Lives in core (not in each loop package) so a new loop strategy stays
11
- * consistent and so behavioral fixes here propagate. Previously
12
- * loop-plan-execute had its own copy that skipped the hook (audit bug).
13
- */
14
- export interface CollectedToolUse {
15
- readonly id: string;
16
- readonly name: string;
17
- readonly input: unknown;
18
- }
19
- /** Appended to the system prompt while elision is active (see projection). */
20
- export declare const ELISION_SYSTEM_NOTE: string;
21
- /**
22
- * Compose a model-facing system prompt that includes any base prompt
23
- * plus a COMPACT skill index (name + description + triggers only).
24
- *
25
- * Lazy-loading design: the body is intentionally NOT inlined. The model
26
- * matches user intent against the description/triggers, then calls the
27
- * `load_skill` tool to fetch the body of the skill it picked. This keeps
28
- * the system prompt small even with many skills installed and avoids
29
- * paying for skill bodies the model never actually follows.
30
- */
31
- export declare function buildSystemPromptWithSkills(baseSystemPrompt: string | undefined, skills: ReadonlyArray<Skill>): string | undefined;
32
- export interface ProjectMessagesOptions {
33
- /** Optional system prompt; emitted as the first message when set. */
34
- readonly systemPrompt?: string;
35
- /** Optional trailing user message — useful for plan-execute's "Focus on this step now: X". */
36
- readonly trailingUserText?: string;
37
- }
38
- /**
39
- * Project the session's event log to a flat list of ProviderMessages
40
- * suitable for handing to `provider.stream`. Used by every loop strategy.
41
- *
42
- * Handles user_prompt, assistant_message, tool_call_requested (grouped
43
- * into a single assistant message of tool_use blocks), and tool_result.
44
- * Other event types are passed through as a no-op.
45
- *
46
- * This is THE projection every loop strategy uses; it honors compaction
47
- * events, turn-boundary elision, and the orphan-tool_use fallback. It lives in
48
- * the SDK so loop plugins stay independent of core.
49
- */
50
- export interface ProjectedMessages {
51
- readonly messages: ProviderMessage[];
52
- /**
53
- * Index (into `messages`) of the last message belonging to the stable,
54
- * byte-identical prefix — i.e. produced entirely from events at or below the
55
- * elision high-water mark (which only advances on whole-turn boundaries, so
56
- * the cut never splits a message). -1 when no elision is active. The
57
- * `stable-prefix` cache strategy places its long-lived cross-turn breakpoint
58
- * here; see {@link collectProviderStream}'s `stablePrefixIndex` option.
59
- */
60
- readonly stablePrefixIndex: number;
61
- }
62
- export declare function projectMessagesFromLog(ctx: Pick<ModeContext, 'log'>, opts?: ProjectMessagesOptions): ProviderMessage[];
63
- /**
64
- * Same projection as {@link projectMessagesFromLog} but also reports the
65
- * stable-prefix boundary so the active cache strategy can place a cross-turn
66
- * breakpoint. Modes that build messages this way should thread the returned
67
- * `stablePrefixIndex` into {@link collectProviderStream}.
68
- */
69
- export declare function projectMessages(ctx: Pick<ModeContext, 'log'>, opts?: ProjectMessagesOptions): ProjectedMessages;
70
- export interface StreamResult {
71
- readonly text: string;
72
- readonly toolUses: ReadonlyArray<CollectedToolUse>;
73
- readonly stopReason: StopReason;
74
- readonly error: {
75
- readonly message: string;
76
- readonly retryable: boolean;
77
- } | null;
78
- /** Token usage reported by the provider on `message_end`, including cache hits/writes. */
79
- readonly usage?: TokenUsage;
80
- /**
81
- * Reasoning/thinking summary for this provider call, when the model emitted
82
- * any. The mode emits it as a `reasoning_message` event (so it persists and
83
- * round-trips). `signature`/`encrypted` carry Anthropic's signed thinking
84
- * block / redacted blob; `redacted` marks display-suppressed reasoning.
85
- */
86
- readonly reasoning?: {
87
- readonly text: string;
88
- readonly signature?: string;
89
- readonly redacted?: boolean;
90
- readonly encrypted?: string;
91
- };
92
- }
93
- /**
94
- * Pulls a provider stream, emits `assistant_chunk` events for text deltas,
95
- * collects tool_use blocks, and returns the final `{text, toolUses, stopReason}`.
96
- * Runs `onBeforeProviderCall` lifecycle hooks before the call.
97
- */
98
- export declare function collectProviderStream(ctx: ModeContext, messages: ReadonlyArray<ProviderMessage>, opts?: {
99
- iteration?: number;
100
- includeTools?: boolean;
101
- maxTokens?: number;
102
- /**
103
- * Index (into `messages`) of the last stable-prefix message, from
104
- * {@link projectMessages}. Passed to the active cache strategy as
105
- * `stablePrefixMessageIndex` so it can place a long-lived cross-turn
106
- * breakpoint at the elision boundary. Omit (or -1) when unknown — the
107
- * strategy then falls back to its tools/system/tail breakpoints only.
108
- */
109
- stablePrefixIndex?: number;
110
- /**
111
- * Number of trailing messages in `messages` that are volatile — injected
112
- * for this call only (e.g. goal mode's `trailingUserText` nudge) and
113
- * absent from the append-only log, so they won't recur at the same
114
- * position next call. Forwarded to the cache strategy as
115
- * `volatileTailMessageCount` so it keeps its rolling tail breakpoint
116
- * before them instead of paying a guaranteed-wasted cache write.
117
- */
118
- volatileTailCount?: number;
119
- }): Promise<StreamResult>;
120
- /**
121
- * Run a single-shot (no-tools) provider turn — the shape every planner /
122
- * synthesis phase shares. Runs context management (compaction + elision),
123
- * emits the `provider_request` bookend, streams the response with tools
124
- * disabled, then emits either an `error` event (returning `null`) or the
125
- * `provider_response` bookend (returning the collected text).
126
- *
127
- * Replaces the ~40-line block each mode phase used to inline; centralizing it
128
- * keeps event emission uniform and means a fix here (e.g. always running
129
- * elision) lands for every loop strategy at once.
130
- */
131
- export declare function runSingleShotTurn(ctx: ModeContext, messages: ReadonlyArray<ProviderMessage>, opts?: {
132
- maxTokens?: number;
133
- }): Promise<string | null>;
134
- /**
135
- * Sliding-window detector for "model keeps making the same tool call".
136
- *
137
- * When the same `(toolName, input)` pair appears `repeatThreshold` times in
138
- * the last `windowSize` calls, the model is almost certainly stuck — polling a
139
- * tool that returns the same thing, mis-handling an error, etc. Bail early
140
- * instead of burning through the iteration cap.
141
- *
142
- * Shared across every loop strategy so detection is uniform — previously each
143
- * mode re-rolled this, and one copy used a non-canonical `JSON.stringify`
144
- * signature that silently missed key-reordered repeats.
145
- */
146
- export interface StuckSignal {
147
- /** True when the loop guard should trip. */
148
- readonly stuck: boolean;
149
- /** Repeat count behind the trip — for the error message. */
150
- readonly count: number;
151
- /**
152
- * `exact` = the same (tool, full-input) repeated `repeatThreshold` times.
153
- * `near` = the same (tool, identity arg — url / file_path / command / …)
154
- * repeated `nearThreshold` times while only volatile args (maxBytes,
155
- * timeoutMs) varied. Catches the "refetch the same URL with a bigger
156
- * maxBytes over and over" loop the exact check sails past.
157
- */
158
- readonly kind: 'exact' | 'near';
159
- }
160
- export interface StuckLoopDetector {
161
- readonly windowSize: number;
162
- readonly repeatThreshold: number;
163
- /** Record the call and report whether the loop guard should trip. */
164
- record(toolName: string, input: unknown): StuckSignal;
165
- }
166
- export declare function createStuckLoopDetector(opts?: {
167
- windowSize?: number;
168
- repeatThreshold?: number;
169
- nearWindowSize?: number;
170
- nearThreshold?: number;
171
- }): StuckLoopDetector;
172
- /**
173
- * Stable, key-order-canonical hash of a tool call's input, so `{a:1,b:2}` and
174
- * `{b:2,a:1}` produce the same key. Use for any "have I seen this call before"
175
- * comparison — a raw `JSON.stringify` is NOT order-stable.
176
- */
177
- export declare function stableHash(input: unknown): string;
7
+ * - `./mode/project-messages.ts` event-log ProviderMessage projection
8
+ * - `./mode/collect-stream.ts` provider-stream collection
9
+ * - `./mode/single-shot.ts` — single-shot (no-tools) provider turn
10
+ * - `./mode/stuck-loop.ts` — sliding-window stuck-tool-call detector
11
+ * - `./mode/stable-hash.ts` — key-order-canonical input hash util
12
+ */
13
+ export { ELISION_SYSTEM_NOTE, buildSystemPromptWithSkills, projectMessagesFromLog, projectMessages, type ProjectMessagesOptions, type ProjectedMessages, } from './mode/project-messages.js';
14
+ export { collectProviderStream, type CollectedToolUse, type StreamResult, } from './mode/collect-stream.js';
15
+ export { runSingleShotTurn } from './mode/single-shot.js';
16
+ export { createStuckLoopDetector, type StuckLoopDetector, type StuckSignal, } from './mode/stuck-loop.js';
17
+ export { stableHash } from './mode/stable-hash.js';
178
18
  //# sourceMappingURL=mode-helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mode-helpers.d.ts","sourceRoot":"","sources":["../src/mode-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA+B,eAAe,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC9F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAgBxC;;;;;;;;GAQG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,QAKb,CAAC;AAEpB;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,gBAAgB,EAAE,MAAM,GAAG,SAAS,EACpC,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,GAC3B,MAAM,GAAG,SAAS,CAoBpB;AAED,MAAM,WAAW,sBAAsB;IACrC,qEAAqE;IACrE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,8FAA8F;IAC9F,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CACpC;AAsFD;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,iB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