wherever-dev 0.10.10 → 0.10.12
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/conversation-mode-hint.d.ts +89 -8
- package/dist/conversation-mode-hint.d.ts.map +1 -1
- package/dist/conversation-mode-hint.js +164 -12
- package/dist/conversation-mode-hint.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +1 -0
- package/dist/protocol.d.ts.map +1 -1
- package/package.json +1 -1
- package/public/_app/immutable/assets/{0.D-PmZM9S.css → 0.CC0hR-g4.css} +1 -1
- package/public/_app/immutable/chunks/C802ERXP.js +2 -0
- package/public/_app/immutable/chunks/{hcaa3__z.js.map → C802ERXP.js.map} +1 -1
- package/public/_app/immutable/chunks/{DSfDn_BZ.js → DHIEbea_.js} +2 -2
- package/public/_app/immutable/chunks/{DSfDn_BZ.js.map → DHIEbea_.js.map} +1 -1
- package/public/_app/immutable/chunks/{DcvdMKCm.js → wvFikTuW.js} +2 -2
- package/public/_app/immutable/chunks/{DcvdMKCm.js.map → wvFikTuW.js.map} +1 -1
- package/public/_app/immutable/entry/{app.CjmqJzTe.js → app.CDq0HLUN.js} +3 -3
- package/public/_app/immutable/entry/{app.CjmqJzTe.js.map → app.CDq0HLUN.js.map} +1 -1
- package/public/_app/immutable/entry/start.DVPKM4ve.js +2 -0
- package/public/_app/immutable/entry/start.DVPKM4ve.js.map +1 -0
- package/public/_app/immutable/nodes/{0.Qlby4yfd.js → 0.BX0-WNRM.js} +2 -2
- package/public/_app/immutable/nodes/{0.Qlby4yfd.js.map → 0.BX0-WNRM.js.map} +1 -1
- package/public/_app/immutable/nodes/{1.BK1p_QJs.js → 1.WAaKe5sz.js} +2 -2
- package/public/_app/immutable/nodes/{1.BK1p_QJs.js.map → 1.WAaKe5sz.js.map} +1 -1
- package/public/_app/immutable/nodes/2.q5b1Ictx.js +95 -0
- package/public/_app/immutable/nodes/2.q5b1Ictx.js.map +1 -0
- package/public/_app/version.json +1 -1
- package/public/index.html +11 -11
- package/public/service-worker.js +1 -1
- package/public/_app/immutable/chunks/hcaa3__z.js +0 -2
- package/public/_app/immutable/entry/start.DA0ZJlQ1.js +0 -2
- package/public/_app/immutable/entry/start.DA0ZJlQ1.js.map +0 -1
- package/public/_app/immutable/nodes/2.BCBJ63Bm.js +0 -89
- package/public/_app/immutable/nodes/2.BCBJ63Bm.js.map +0 -1
|
@@ -11,11 +11,15 @@ import type { InlineExtension } from '@earendil-works/pi-coding-agent';
|
|
|
11
11
|
*
|
|
12
12
|
* The signal rides the EXISTING `message` WS payload as an optional
|
|
13
13
|
* `conversationMode` boolean (no new WS message type, no new chat role), and is
|
|
14
|
-
* turned into agent-visible context here
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* turned into agent-visible context here, for that turn only, in TWO places:
|
|
15
|
+
* - `before_agent_start` APPENDS one line (CONVERSATION_MODE_HINT) to the
|
|
16
|
+
* assembled system prompt, and
|
|
17
|
+
* - `context` adds a TAIL reminder (CONVERSATION_MODE_REMINDER) before EVERY LLM
|
|
18
|
+
* call of the turn, because the system-prompt line alone is too far from the
|
|
19
|
+
* tail for smaller models to act on (measurements in the reminder's doc).
|
|
20
|
+
*
|
|
21
|
+
* Both are EPHEMERAL: neither is stored in the user message and neither renders
|
|
22
|
+
* as a chat line (only their effect, the resulting `say` tool call, is visible).
|
|
19
23
|
*
|
|
20
24
|
* KEEP IN LOCKSTEP with the CLI-bridge twin,
|
|
21
25
|
* `extension/src/conversation-mode-hint.ts`. The extension is a separate published
|
|
@@ -24,6 +28,28 @@ import type { InlineExtension } from '@earendil-works/pi-coding-agent';
|
|
|
24
28
|
* BOTH modules and fails if the hint text or the latch behaviour drifts.
|
|
25
29
|
*/
|
|
26
30
|
export declare const CONVERSATION_MODE_HINT: string;
|
|
31
|
+
/**
|
|
32
|
+
* The marker that opens the TAIL REMINDER, and the reminder itself.
|
|
33
|
+
*
|
|
34
|
+
* WHY A SECOND, TAIL-PLACED COPY OF THE SAME INSTRUCTION: the system-prompt hint
|
|
35
|
+
* alone measured as a coin flip on smaller local models. Probing a real local
|
|
36
|
+
* 35B (the model the reported session ran on) with a realistic wherever-shaped
|
|
37
|
+
* system prompt: with the hint 3/6 turns called `say`, without it 1/6 - and at
|
|
38
|
+
* the moment that matters most, the post-tool-result synthesis call (the answer
|
|
39
|
+
* after a `web_search`), the hint was ignored almost every time. Two things
|
|
40
|
+
* defeat it: the hint sits far from the tail of a long prompt, and once the
|
|
41
|
+
* transcript contains earlier assistant turns that did NOT speak, the model
|
|
42
|
+
* imitates its own history. A reminder placed at the very TAIL of the message
|
|
43
|
+
* list, re-applied before EVERY LLM call of the turn, is what actually lands.
|
|
44
|
+
*
|
|
45
|
+
* The reminder is EPHEMERAL in the strongest sense: it is added by the `context`
|
|
46
|
+
* event, which the SDK applies to a structuredClone of the messages on the way to
|
|
47
|
+
* the provider (see `transformContext`), so it is never stored in the session
|
|
48
|
+
* file, never rendered in the web transcript or the CLI TUI, and never survives
|
|
49
|
+
* the call it was built for.
|
|
50
|
+
*/
|
|
51
|
+
export declare const CONVERSATION_MODE_REMINDER_MARKER = "[spoken conversation active]";
|
|
52
|
+
export declare const CONVERSATION_MODE_REMINDER: string;
|
|
27
53
|
/**
|
|
28
54
|
* Append the conversation-mode hint to an assembled system prompt.
|
|
29
55
|
*
|
|
@@ -32,6 +58,45 @@ export declare const CONVERSATION_MODE_HINT: string;
|
|
|
32
58
|
* handlers' results) and returns base + hint. Never replaces, never re-fetches.
|
|
33
59
|
*/
|
|
34
60
|
export declare function appendConversationModeHint(systemPrompt: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* The minimal STRUCTURAL shape of a pi context message this module needs. Kept
|
|
63
|
+
* structural (not an SDK type import) so the CLI-bridge twin can be byte-identical
|
|
64
|
+
* and so both copies stay unit-testable without standing up a pi runtime.
|
|
65
|
+
*/
|
|
66
|
+
export interface ContextMessageLike {
|
|
67
|
+
role: string;
|
|
68
|
+
content?: unknown;
|
|
69
|
+
[key: string]: unknown;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether the agent has ALREADY called `say` in the turn the tail of `messages`
|
|
73
|
+
* belongs to (i.e. since the last user message).
|
|
74
|
+
*
|
|
75
|
+
* This is the loop guard AND the anti-nag rule: the reminder asks for a `say`
|
|
76
|
+
* call, `say` is itself a tool, and a tool call means another LLM call with the
|
|
77
|
+
* reminder re-applied. Without this, a model that answers with ONLY a `say` call
|
|
78
|
+
* could be nudged into speaking again, and again. Once the turn has spoken, the
|
|
79
|
+
* reminder has done its job and is not repeated.
|
|
80
|
+
*/
|
|
81
|
+
export declare function saidThisTurn(messages: readonly ContextMessageLike[]): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Return the messages for ONE upcoming LLM call with the spoken-conversation
|
|
84
|
+
* reminder at the TAIL, where a smaller model actually reads it.
|
|
85
|
+
*
|
|
86
|
+
* Placement is deliberately ROLE-SAFE rather than "always append a message":
|
|
87
|
+
* Anthropic's own API merges consecutive same-role turns, but Bedrock (and some
|
|
88
|
+
* compatibility proxies) reject them outright, and after a tool result the tail
|
|
89
|
+
* IS a user-role turn. So:
|
|
90
|
+
* - tail is a `user` or `toolResult` message -> the reminder is added as an extra
|
|
91
|
+
* text block INSIDE a clone of it (no new turn, no role sequence change);
|
|
92
|
+
* - anything else (assistant tail, empty context) -> the reminder is appended as
|
|
93
|
+
* its own `custom` message, which converts to a user turn cleanly.
|
|
94
|
+
*
|
|
95
|
+
* Pure and non-mutating: the input array and its messages are never touched, only
|
|
96
|
+
* the returned array (and the single cloned tail) differ. Idempotent: a tail that
|
|
97
|
+
* already carries the marker is left alone, so re-entry cannot stack reminders.
|
|
98
|
+
*/
|
|
99
|
+
export declare function withConversationModeReminder(messages: readonly ContextMessageLike[]): ContextMessageLike[];
|
|
35
100
|
/**
|
|
36
101
|
* A per-session, per-turn arming latch plus the inline pi extension that acts on
|
|
37
102
|
* it.
|
|
@@ -40,20 +105,36 @@ export declare function appendConversationModeHint(systemPrompt: string): string
|
|
|
40
105
|
* same):
|
|
41
106
|
* - `arm(active)` is called for EVERY user message with that message's flag, so
|
|
42
107
|
* the latest message always wins (a stale value can never outlive it).
|
|
43
|
-
* - the `before_agent_start` handler CONSUMES the
|
|
108
|
+
* - the `before_agent_start` handler CONSUMES the arming (one turn only), so a
|
|
44
109
|
* turn that was not started by a flagged message (an auto-retry, or a message
|
|
45
|
-
* typed straight into the terminal in the bridge case) gets no hint.
|
|
110
|
+
* typed straight into the terminal in the bridge case) gets no hint. Consuming
|
|
111
|
+
* it OPENS the turn: whatever the arming said (on or off) becomes this turn's
|
|
112
|
+
* spoken state, so an unflagged turn also explicitly CLOSES a previous one.
|
|
113
|
+
* - while the turn is open, `applyToContext` adds the TAIL reminder before every
|
|
114
|
+
* LLM call of that turn (the system-prompt hint alone is too weak and too far
|
|
115
|
+
* from the tail for smaller models; see CONVERSATION_MODE_REMINDER).
|
|
116
|
+
* - `endTurn()` (wired to `agent_end`) closes it, so nothing leaks past the turn.
|
|
46
117
|
*/
|
|
47
118
|
export interface ConversationModeSignal {
|
|
48
119
|
/** Arm (true) or disarm (false) the hint for the turn the next message starts. */
|
|
49
120
|
arm(active: boolean): void;
|
|
50
121
|
/** Whether the latch is currently armed (test/diagnostic use). */
|
|
51
122
|
isArmed(): boolean;
|
|
123
|
+
/** Whether a spoken turn is currently OPEN, i.e. the reminder applies (test/diagnostic use). */
|
|
124
|
+
isTurnActive(): boolean;
|
|
52
125
|
/**
|
|
53
126
|
* The system prompt to use for this turn, or undefined to leave it untouched.
|
|
54
|
-
* CONSUMES the arming.
|
|
127
|
+
* CONSUMES the arming and opens/closes the turn accordingly.
|
|
55
128
|
*/
|
|
56
129
|
applyToSystemPrompt(systemPrompt: string): string | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* The messages to send for ONE upcoming LLM call, or undefined to leave them
|
|
132
|
+
* untouched (no spoken turn open). Does NOT consume anything: a turn can make
|
|
133
|
+
* many LLM calls and each one gets the reminder until the agent has spoken.
|
|
134
|
+
*/
|
|
135
|
+
applyToContext(messages: readonly ContextMessageLike[]): ContextMessageLike[] | undefined;
|
|
136
|
+
/** Close the spoken turn (wired to `agent_end`). */
|
|
137
|
+
endTurn(): void;
|
|
57
138
|
/** The inline pi extension to hand to DefaultResourceLoader's extensionFactories. */
|
|
58
139
|
inlineExtension: InlineExtension;
|
|
59
140
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversation-mode-hint.d.ts","sourceRoot":"","sources":["../src/conversation-mode-hint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"conversation-mode-hint.d.ts","sourceRoot":"","sources":["../src/conversation-mode-hint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,eAAe,EAChB,MAAM,iCAAiC,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,sBAAsB,QAKC,CAAC;AAErC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,iCAAiC,iCAAiC,CAAC;AAEhF,eAAO,MAAM,0BAA0B,QAGmD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAYD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GAAG,OAAO,CAY7E;AAyCD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,4BAA4B,CAC1C,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GACtC,kBAAkB,EAAE,CActB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,sBAAsB;IACrC,kFAAkF;IAClF,GAAG,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IAC3B,kEAAkE;IAClE,OAAO,IAAI,OAAO,CAAC;IACnB,gGAAgG;IAChG,YAAY,IAAI,OAAO,CAAC;IACxB;;;OAGG;IACH,mBAAmB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC9D;;;;OAIG;IACH,cAAc,CAAC,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GAAG,kBAAkB,EAAE,GAAG,SAAS,CAAC;IAC1F,oDAAoD;IACpD,OAAO,IAAI,IAAI,CAAC;IAChB,qFAAqF;IACrF,eAAe,EAAE,eAAe,CAAC;CAClC;AAED,wBAAgB,4BAA4B,IAAI,sBAAsB,CA6DrE"}
|
|
@@ -10,11 +10,15 @@
|
|
|
10
10
|
*
|
|
11
11
|
* The signal rides the EXISTING `message` WS payload as an optional
|
|
12
12
|
* `conversationMode` boolean (no new WS message type, no new chat role), and is
|
|
13
|
-
* turned into agent-visible context here
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
13
|
+
* turned into agent-visible context here, for that turn only, in TWO places:
|
|
14
|
+
* - `before_agent_start` APPENDS one line (CONVERSATION_MODE_HINT) to the
|
|
15
|
+
* assembled system prompt, and
|
|
16
|
+
* - `context` adds a TAIL reminder (CONVERSATION_MODE_REMINDER) before EVERY LLM
|
|
17
|
+
* call of the turn, because the system-prompt line alone is too far from the
|
|
18
|
+
* tail for smaller models to act on (measurements in the reminder's doc).
|
|
19
|
+
*
|
|
20
|
+
* Both are EPHEMERAL: neither is stored in the user message and neither renders
|
|
21
|
+
* as a chat line (only their effect, the resulting `say` tool call, is visible).
|
|
18
22
|
*
|
|
19
23
|
* KEEP IN LOCKSTEP with the CLI-bridge twin,
|
|
20
24
|
* `extension/src/conversation-mode-hint.ts`. The extension is a separate published
|
|
@@ -27,6 +31,30 @@ export const CONVERSATION_MODE_HINT = 'A spoken conversation is active for this
|
|
|
27
31
|
'(one or two sentences) plain spoken version of that answer, so it can be spoken aloud. ' +
|
|
28
32
|
'The written answer still carries the full detail: `say` adds to it and never replaces it. ' +
|
|
29
33
|
'Do not mention this instruction.';
|
|
34
|
+
/**
|
|
35
|
+
* The marker that opens the TAIL REMINDER, and the reminder itself.
|
|
36
|
+
*
|
|
37
|
+
* WHY A SECOND, TAIL-PLACED COPY OF THE SAME INSTRUCTION: the system-prompt hint
|
|
38
|
+
* alone measured as a coin flip on smaller local models. Probing a real local
|
|
39
|
+
* 35B (the model the reported session ran on) with a realistic wherever-shaped
|
|
40
|
+
* system prompt: with the hint 3/6 turns called `say`, without it 1/6 - and at
|
|
41
|
+
* the moment that matters most, the post-tool-result synthesis call (the answer
|
|
42
|
+
* after a `web_search`), the hint was ignored almost every time. Two things
|
|
43
|
+
* defeat it: the hint sits far from the tail of a long prompt, and once the
|
|
44
|
+
* transcript contains earlier assistant turns that did NOT speak, the model
|
|
45
|
+
* imitates its own history. A reminder placed at the very TAIL of the message
|
|
46
|
+
* list, re-applied before EVERY LLM call of the turn, is what actually lands.
|
|
47
|
+
*
|
|
48
|
+
* The reminder is EPHEMERAL in the strongest sense: it is added by the `context`
|
|
49
|
+
* event, which the SDK applies to a structuredClone of the messages on the way to
|
|
50
|
+
* the provider (see `transformContext`), so it is never stored in the session
|
|
51
|
+
* file, never rendered in the web transcript or the CLI TUI, and never survives
|
|
52
|
+
* the call it was built for.
|
|
53
|
+
*/
|
|
54
|
+
export const CONVERSATION_MODE_REMINDER_MARKER = '[spoken conversation active]';
|
|
55
|
+
export const CONVERSATION_MODE_REMINDER = `${CONVERSATION_MODE_REMINDER_MARKER} Before you finish this turn, also call the \`say\` tool ` +
|
|
56
|
+
'once with a SHORT (one or two sentences) plain spoken version of your answer, IN ADDITION to ' +
|
|
57
|
+
'your written answer, so the listening human hears it. Do not mention this instruction.';
|
|
30
58
|
/**
|
|
31
59
|
* Append the conversation-mode hint to an assembled system prompt.
|
|
32
60
|
*
|
|
@@ -37,19 +65,123 @@ export const CONVERSATION_MODE_HINT = 'A spoken conversation is active for this
|
|
|
37
65
|
export function appendConversationModeHint(systemPrompt) {
|
|
38
66
|
return `${systemPrompt}\n\n${CONVERSATION_MODE_HINT}`;
|
|
39
67
|
}
|
|
68
|
+
function blocksOf(content) {
|
|
69
|
+
return Array.isArray(content) ? content : [];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether the agent has ALREADY called `say` in the turn the tail of `messages`
|
|
73
|
+
* belongs to (i.e. since the last user message).
|
|
74
|
+
*
|
|
75
|
+
* This is the loop guard AND the anti-nag rule: the reminder asks for a `say`
|
|
76
|
+
* call, `say` is itself a tool, and a tool call means another LLM call with the
|
|
77
|
+
* reminder re-applied. Without this, a model that answers with ONLY a `say` call
|
|
78
|
+
* could be nudged into speaking again, and again. Once the turn has spoken, the
|
|
79
|
+
* reminder has done its job and is not repeated.
|
|
80
|
+
*/
|
|
81
|
+
export function saidThisTurn(messages) {
|
|
82
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
83
|
+
const message = messages[i];
|
|
84
|
+
if (!message)
|
|
85
|
+
continue;
|
|
86
|
+
// The user message that started this turn: stop, earlier turns are history.
|
|
87
|
+
if (message.role === 'user')
|
|
88
|
+
return false;
|
|
89
|
+
if (message.role !== 'assistant')
|
|
90
|
+
continue;
|
|
91
|
+
for (const block of blocksOf(message.content)) {
|
|
92
|
+
if (block?.type === 'toolCall' && block.name === 'say')
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
function carriesReminder(message) {
|
|
99
|
+
if (!message)
|
|
100
|
+
return false;
|
|
101
|
+
if (typeof message.content === 'string') {
|
|
102
|
+
return message.content.includes(CONVERSATION_MODE_REMINDER_MARKER);
|
|
103
|
+
}
|
|
104
|
+
return blocksOf(message.content).some((block) => typeof block?.text === 'string' && block.text.includes(CONVERSATION_MODE_REMINDER_MARKER));
|
|
105
|
+
}
|
|
106
|
+
/** A clone of `message` with the reminder added as one more text block. */
|
|
107
|
+
function withReminderBlock(message) {
|
|
108
|
+
if (typeof message.content === 'string') {
|
|
109
|
+
return { ...message, content: `${message.content}\n\n${CONVERSATION_MODE_REMINDER}` };
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
...message,
|
|
113
|
+
content: [...blocksOf(message.content), { type: 'text', text: CONVERSATION_MODE_REMINDER }],
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The reminder as its own message, for the case where the tail cannot absorb it.
|
|
118
|
+
*
|
|
119
|
+
* A `custom` message is pi's own "context that is not a real chat message" shape:
|
|
120
|
+
* `convertToLlm` turns it into a plain user text message for the provider, and
|
|
121
|
+
* `display: false` keeps it out of any UI that might see it.
|
|
122
|
+
*/
|
|
123
|
+
function reminderMessage() {
|
|
124
|
+
return {
|
|
125
|
+
role: 'custom',
|
|
126
|
+
customType: 'wherever-conversation-mode',
|
|
127
|
+
content: CONVERSATION_MODE_REMINDER,
|
|
128
|
+
display: false,
|
|
129
|
+
timestamp: Date.now(),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Return the messages for ONE upcoming LLM call with the spoken-conversation
|
|
134
|
+
* reminder at the TAIL, where a smaller model actually reads it.
|
|
135
|
+
*
|
|
136
|
+
* Placement is deliberately ROLE-SAFE rather than "always append a message":
|
|
137
|
+
* Anthropic's own API merges consecutive same-role turns, but Bedrock (and some
|
|
138
|
+
* compatibility proxies) reject them outright, and after a tool result the tail
|
|
139
|
+
* IS a user-role turn. So:
|
|
140
|
+
* - tail is a `user` or `toolResult` message -> the reminder is added as an extra
|
|
141
|
+
* text block INSIDE a clone of it (no new turn, no role sequence change);
|
|
142
|
+
* - anything else (assistant tail, empty context) -> the reminder is appended as
|
|
143
|
+
* its own `custom` message, which converts to a user turn cleanly.
|
|
144
|
+
*
|
|
145
|
+
* Pure and non-mutating: the input array and its messages are never touched, only
|
|
146
|
+
* the returned array (and the single cloned tail) differ. Idempotent: a tail that
|
|
147
|
+
* already carries the marker is left alone, so re-entry cannot stack reminders.
|
|
148
|
+
*/
|
|
149
|
+
export function withConversationModeReminder(messages) {
|
|
150
|
+
const out = messages.slice();
|
|
151
|
+
// Already spoken this turn: nothing to nudge, and re-nudging risks a say loop.
|
|
152
|
+
if (saidThisTurn(out))
|
|
153
|
+
return out;
|
|
154
|
+
const tail = out[out.length - 1];
|
|
155
|
+
if (carriesReminder(tail))
|
|
156
|
+
return out;
|
|
157
|
+
if (tail && (tail.role === 'user' || tail.role === 'toolResult')) {
|
|
158
|
+
out[out.length - 1] = withReminderBlock(tail);
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
out.push(reminderMessage());
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
40
164
|
export function createConversationModeSignal() {
|
|
41
165
|
let armed = false;
|
|
166
|
+
let turnActive = false;
|
|
42
167
|
const applyToSystemPrompt = (systemPrompt) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
|
|
168
|
+
// One turn only: consume the arming so it cannot leak into a later, unflagged
|
|
169
|
+
// turn, and let it decide whether THIS turn is a spoken one (an unflagged turn
|
|
170
|
+
// therefore also closes a turn a missing agent_end might have left open).
|
|
171
|
+
turnActive = armed;
|
|
47
172
|
armed = false;
|
|
173
|
+
if (!turnActive)
|
|
174
|
+
return undefined;
|
|
48
175
|
return appendConversationModeHint(systemPrompt);
|
|
49
176
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
177
|
+
const applyToContext = (messages) => {
|
|
178
|
+
if (!turnActive)
|
|
179
|
+
return undefined;
|
|
180
|
+
return withConversationModeReminder(messages);
|
|
181
|
+
};
|
|
182
|
+
// The SDK-supported way to get these hooks on a server-created session: an
|
|
183
|
+
// INLINE extension, i.e. the same pi.on(...) surface the CLI-bridge extension
|
|
184
|
+
// uses, so both session types run the same shape of handler.
|
|
53
185
|
const factory = (pi) => {
|
|
54
186
|
pi.on('before_agent_start', (event) => {
|
|
55
187
|
const systemPrompt = applyToSystemPrompt(event.systemPrompt);
|
|
@@ -57,6 +189,19 @@ export function createConversationModeSignal() {
|
|
|
57
189
|
return;
|
|
58
190
|
return { systemPrompt };
|
|
59
191
|
});
|
|
192
|
+
// Fired before EVERY LLM call (including the post-tool-result synthesis call,
|
|
193
|
+
// which is exactly where the system-prompt hint gets ignored). The SDK hands
|
|
194
|
+
// us a structuredClone and passes our result straight to convertToLlm, so this
|
|
195
|
+
// never touches session state.
|
|
196
|
+
pi.on('context', (event) => {
|
|
197
|
+
const messages = applyToContext(event.messages);
|
|
198
|
+
if (messages === undefined)
|
|
199
|
+
return;
|
|
200
|
+
return { messages: messages };
|
|
201
|
+
});
|
|
202
|
+
pi.on('agent_end', () => {
|
|
203
|
+
turnActive = false;
|
|
204
|
+
});
|
|
60
205
|
};
|
|
61
206
|
return {
|
|
62
207
|
arm(active) {
|
|
@@ -65,7 +210,14 @@ export function createConversationModeSignal() {
|
|
|
65
210
|
isArmed() {
|
|
66
211
|
return armed;
|
|
67
212
|
},
|
|
213
|
+
isTurnActive() {
|
|
214
|
+
return turnActive;
|
|
215
|
+
},
|
|
68
216
|
applyToSystemPrompt,
|
|
217
|
+
applyToContext,
|
|
218
|
+
endTurn() {
|
|
219
|
+
turnActive = false;
|
|
220
|
+
},
|
|
69
221
|
inlineExtension: { name: 'wherever-conversation-mode', factory },
|
|
70
222
|
};
|
|
71
223
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversation-mode-hint.js","sourceRoot":"","sources":["../src/conversation-mode-hint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"conversation-mode-hint.js","sourceRoot":"","sources":["../src/conversation-mode-hint.ts"],"names":[],"mappings":"AAOA;;;;;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YAAY,GAAG,mBAAmB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YAC7D,IAAI,YAAY,KAAK,SAAS;gBAAE,OAAO;YACvC,OAAO,EAAE,YAAY,EAAE,CAAC;QAC1B,CAAC,CAAC,CAAC;QACH,8EAA8E;QAC9E,6EAA6E;QAC7E,+EAA+E;QAC/E,+BAA+B;QAC/B,EAAE,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,KAAmB,EAAE,EAAE;YACvC,MAAM,QAAQ,GAAG,cAAc,CAAC,KAAK,CAAC,QAA2C,CAAC,CAAC;YACnF,IAAI,QAAQ,KAAK,SAAS;gBAAE,OAAO;YACnC,OAAO,EAAE,QAAQ,EAAE,QAA+C,EAAE,CAAC;QACvE,CAAC,CAAC,CAAC;QACH,EAAE,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;YACtB,UAAU,GAAG,KAAK,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO;QACL,GAAG,CAAC,MAAe;YACjB,KAAK,GAAG,MAAM,CAAC;QACjB,CAAC;QACD,OAAO;YACL,OAAO,KAAK,CAAC;QACf,CAAC;QACD,YAAY;YACV,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,mBAAmB;QACnB,cAAc;QACd,OAAO;YACL,UAAU,GAAG,KAAK,CAAC;QACrB,CAAC;QACD,eAAe,EAAE,EAAE,IAAI,EAAE,4BAA4B,EAAE,OAAO,EAAE;KACjE,CAAC;AACJ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1366,7 +1366,7 @@ async function main() {
|
|
|
1366
1366
|
ws.on('pong', () => {
|
|
1367
1367
|
liveSockets.add(ws);
|
|
1368
1368
|
});
|
|
1369
|
-
sendWS(ws, { type: 'connected', clientId });
|
|
1369
|
+
sendWS(ws, { type: 'connected', clientId, serverVersion: getVersion() });
|
|
1370
1370
|
ws.on('message', async (data) => {
|
|
1371
1371
|
let msg;
|
|
1372
1372
|
try {
|