@moxxy/sdk 0.14.2 → 0.14.3
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.
- package/dist/mode/collect-stream.d.ts +68 -0
- package/dist/mode/collect-stream.d.ts.map +1 -0
- package/dist/mode/collect-stream.js +181 -0
- package/dist/mode/collect-stream.js.map +1 -0
- package/dist/mode/project-messages.d.ts +84 -0
- package/dist/mode/project-messages.d.ts.map +1 -0
- package/dist/mode/project-messages.js +392 -0
- package/dist/mode/project-messages.js.map +1 -0
- package/dist/mode/single-shot.d.ts +17 -0
- package/dist/mode/single-shot.d.ts.map +1 -0
- package/dist/mode/single-shot.js +53 -0
- package/dist/mode/single-shot.js.map +1 -0
- package/dist/mode/stable-hash.d.ts +7 -0
- package/dist/mode/stable-hash.d.ts.map +1 -0
- package/dist/mode/stable-hash.js +20 -0
- package/dist/mode/stable-hash.js.map +1 -0
- package/dist/mode/stuck-loop.d.ts +39 -0
- package/dist/mode/stuck-loop.d.ts.map +1 -0
- package/dist/mode/stuck-loop.js +51 -0
- package/dist/mode/stuck-loop.js.map +1 -0
- package/dist/mode-helpers.d.ts +15 -175
- package/dist/mode-helpers.d.ts.map +1 -1
- package/dist/mode-helpers.js +14 -666
- package/dist/mode-helpers.js.map +1 -1
- package/package.json +1 -1
- package/src/mode/collect-stream.ts +247 -0
- package/src/mode/project-messages.test.ts +121 -0
- package/src/mode/project-messages.ts +461 -0
- package/src/mode/single-shot.ts +63 -0
- package/src/mode/stable-hash.ts +20 -0
- package/src/mode/stuck-loop.ts +89 -0
- package/src/mode-helpers.ts +32 -850
- package/src/mode.test.ts +20 -0
- package/src/token-accounting.test.ts +90 -0
package/dist/mode-helpers.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export
|
|
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
|
|
1
|
+
{"version":3,"file":"mode-helpers.d.ts","sourceRoot":"","sources":["../src/mode-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,sBAAsB,EACtB,eAAe,EACf,KAAK,sBAAsB,EAC3B,KAAK,iBAAiB,GACvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,qBAAqB,EACrB,KAAK,gBAAgB,EACrB,KAAK,YAAY,GAClB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EACL,uBAAuB,EACvB,KAAK,iBAAiB,EACtB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC"}
|