@moxxy/plugin-provider-claude-code 0.44.0 → 0.46.0
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/account-limit.d.ts +8 -0
- package/dist/account-limit.d.ts.map +1 -0
- package/dist/account-limit.js +23 -0
- package/dist/account-limit.js.map +1 -0
- package/dist/agent-provider.d.ts +61 -0
- package/dist/agent-provider.d.ts.map +1 -0
- package/dist/agent-provider.js +195 -0
- package/dist/agent-provider.js.map +1 -0
- package/dist/agent-session.d.ts +145 -0
- package/dist/agent-session.d.ts.map +1 -0
- package/dist/agent-session.js +703 -0
- package/dist/agent-session.js.map +1 -0
- package/dist/background-work.d.ts +42 -0
- package/dist/background-work.d.ts.map +1 -0
- package/dist/background-work.js +140 -0
- package/dist/background-work.js.map +1 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -2
- package/dist/constants.js.map +1 -1
- package/dist/executable.d.ts +15 -0
- package/dist/executable.d.ts.map +1 -0
- package/dist/executable.js +30 -0
- package/dist/executable.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -16
- package/dist/index.js.map +1 -1
- package/dist/login.d.ts +3 -0
- package/dist/login.d.ts.map +1 -1
- package/dist/login.js +8 -1
- package/dist/login.js.map +1 -1
- package/dist/offered-models.d.ts +13 -0
- package/dist/offered-models.d.ts.map +1 -0
- package/dist/offered-models.js +48 -0
- package/dist/offered-models.js.map +1 -0
- package/dist/own-settings.d.ts +12 -0
- package/dist/own-settings.d.ts.map +1 -0
- package/dist/own-settings.js +31 -0
- package/dist/own-settings.js.map +1 -0
- package/dist/questions.d.ts +6 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +38 -0
- package/dist/questions.js.map +1 -0
- package/dist/scheduled-wakeups.d.ts +10 -0
- package/dist/scheduled-wakeups.d.ts.map +1 -0
- package/dist/scheduled-wakeups.js +30 -0
- package/dist/scheduled-wakeups.js.map +1 -0
- package/dist/sdk-install.d.ts +25 -0
- package/dist/sdk-install.d.ts.map +1 -0
- package/dist/sdk-install.js +94 -0
- package/dist/sdk-install.js.map +1 -0
- package/dist/sdk-types.d.ts +312 -0
- package/dist/sdk-types.d.ts.map +1 -0
- package/dist/sdk-types.js +8 -0
- package/dist/sdk-types.js.map +1 -0
- package/dist/subagents.d.ts +39 -0
- package/dist/subagents.d.ts.map +1 -0
- package/dist/subagents.js +114 -0
- package/dist/subagents.js.map +1 -0
- package/dist/task-list.d.ts +21 -0
- package/dist/task-list.d.ts.map +1 -0
- package/dist/task-list.js +68 -0
- package/dist/task-list.js.map +1 -0
- package/dist/translate.d.ts +66 -0
- package/dist/translate.d.ts.map +1 -0
- package/dist/translate.js +302 -0
- package/dist/translate.js.map +1 -0
- package/package.json +14 -9
- package/src/account-limit.ts +26 -0
- package/src/agent-cli.test.ts +83 -0
- package/src/agent-provider.test.ts +1176 -0
- package/src/agent-provider.ts +223 -0
- package/src/agent-session.ts +793 -0
- package/src/background-work.ts +163 -0
- package/src/constants.ts +2 -3
- package/src/event-coverage.test.ts +106 -0
- package/src/executable.test.ts +60 -0
- package/src/executable.ts +34 -0
- package/src/fake-claude.testkit.ts +376 -0
- package/src/fixtures/claude-agent-sdk-0.3.296.json +43 -0
- package/src/fixtures/claude-supported-models-0.3.296.json +218 -0
- package/src/index.test.ts +49 -0
- package/src/index.ts +61 -18
- package/src/login.ts +10 -1
- package/src/offered-models.test.ts +81 -0
- package/src/offered-models.ts +60 -0
- package/src/own-settings.test.ts +68 -0
- package/src/own-settings.ts +33 -0
- package/src/questions.test.ts +68 -0
- package/src/questions.ts +45 -0
- package/src/scheduled-wakeups.test.ts +41 -0
- package/src/scheduled-wakeups.ts +34 -0
- package/src/sdk-install.test.ts +124 -0
- package/src/sdk-install.ts +107 -0
- package/src/sdk-types.ts +278 -0
- package/src/subagents.ts +123 -0
- package/src/task-list.ts +65 -0
- package/src/translate.test.ts +474 -0
- package/src/translate.ts +306 -0
- package/dist/process.d.ts +0 -34
- package/dist/process.d.ts.map +0 -1
- package/dist/process.js +0 -197
- package/dist/process.js.map +0 -1
- package/dist/protocol.d.ts +0 -30
- package/dist/protocol.d.ts.map +0 -1
- package/dist/protocol.js +0 -368
- package/dist/protocol.js.map +0 -1
- package/dist/provider.d.ts +0 -53
- package/dist/provider.d.ts.map +0 -1
- package/dist/provider.js +0 -229
- package/dist/provider.js.map +0 -1
- package/src/process.test.ts +0 -208
- package/src/process.ts +0 -224
- package/src/protocol.ts +0 -390
- package/src/provider.test.ts +0 -671
- package/src/provider.ts +0 -267
package/src/task-list.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { stepStatusOf, stepsFromTodos, type ListedStep } from '@moxxy/sdk';
|
|
2
|
+
|
|
3
|
+
/** Claude's tools that change its list of steps. `TodoWrite` is the older one, written whole each time. */
|
|
4
|
+
export const CLAUDE_STEP_TOOLS: ReadonlySet<string> = new Set(['TodoWrite', 'TaskCreate', 'TaskUpdate', 'TaskList']);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Claude's task list for one of its sessions. Claude Code 2.1 keeps it with
|
|
8
|
+
* TaskCreate, TaskUpdate and TaskList, one task at a time, and the list lives
|
|
9
|
+
* on across turns, so it is kept here as the calls finish. Ids come from
|
|
10
|
+
* Claude (TaskCreate reports its new task's id).
|
|
11
|
+
*/
|
|
12
|
+
export class ClaudeTaskList {
|
|
13
|
+
private readonly tasks = new Map<string, ListedStep>();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The whole list after a call that finished, or undefined when the call is
|
|
17
|
+
* not one of the step tools or says nothing Moxxy can follow (a task it never
|
|
18
|
+
* saw created, from before Moxxy started this Claude).
|
|
19
|
+
*/
|
|
20
|
+
apply(name: string, input: unknown, result: unknown): ListedStep[] | undefined {
|
|
21
|
+
if (name === 'TodoWrite') return stepsFromTodos(field(input, 'todos'));
|
|
22
|
+
if (name === 'TaskList') return this.replace(field(result, 'tasks'));
|
|
23
|
+
if (name === 'TaskCreate') {
|
|
24
|
+
const created = field(result, 'task');
|
|
25
|
+
const id = text(created, 'id');
|
|
26
|
+
const subject = text(created, 'subject') ?? text(input, 'subject');
|
|
27
|
+
if (id === undefined || subject === undefined) return undefined;
|
|
28
|
+
this.tasks.set(id, { text: subject, status: 'pending' });
|
|
29
|
+
return this.list();
|
|
30
|
+
}
|
|
31
|
+
if (name !== 'TaskUpdate' || field(result, 'success') === false) return undefined;
|
|
32
|
+
const id = text(input, 'taskId') ?? text(result, 'taskId');
|
|
33
|
+
const task = id === undefined ? undefined : this.tasks.get(id);
|
|
34
|
+
if (id === undefined || task === undefined) return undefined;
|
|
35
|
+
const status = field(input, 'status');
|
|
36
|
+
if (status === 'deleted') this.tasks.delete(id);
|
|
37
|
+
else this.tasks.set(id, { text: text(input, 'subject') ?? task.text, status: stepStatusOf(status) ?? task.status });
|
|
38
|
+
return this.list();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private replace(listed: unknown): ListedStep[] | undefined {
|
|
42
|
+
if (!Array.isArray(listed)) return undefined;
|
|
43
|
+
this.tasks.clear();
|
|
44
|
+
for (const task of listed) {
|
|
45
|
+
const id = text(task, 'id');
|
|
46
|
+
const subject = text(task, 'subject');
|
|
47
|
+
const status = stepStatusOf(field(task, 'status'));
|
|
48
|
+
if (id !== undefined && subject !== undefined && status !== undefined) this.tasks.set(id, { text: subject, status });
|
|
49
|
+
}
|
|
50
|
+
return this.list();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private list(): ListedStep[] {
|
|
54
|
+
return [...this.tasks.values()];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function field(value: unknown, key: string): unknown {
|
|
59
|
+
return typeof value === 'object' && value !== null ? (value as Record<string, unknown>)[key] : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function text(value: unknown, key: string): string | undefined {
|
|
63
|
+
const found = field(value, key);
|
|
64
|
+
return typeof found === 'string' && found.trim() ? found.trim() : undefined;
|
|
65
|
+
}
|
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Claude's messages become in Moxxy. The messages are trimmed copies of
|
|
3
|
+
* what Claude Code 2.1.296 sent through the Agent SDK in the spike.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import type { ClaudeMessage } from './sdk-types.js';
|
|
7
|
+
import { ClaudeSubagents } from './subagents.js';
|
|
8
|
+
import { ClaudeTaskList } from './task-list.js';
|
|
9
|
+
import { TurnTranslator, autoApproveFor, goalStatusFrom, toClaudeContent, toTokenUsage } from './translate.js';
|
|
10
|
+
|
|
11
|
+
const init = (permissionMode: string): ClaudeMessage =>
|
|
12
|
+
({ type: 'system', subtype: 'init', session_id: 's-1', permissionMode, model: 'claude-opus-5-5' }) as ClaudeMessage;
|
|
13
|
+
const assistant = (content: unknown[], parent: string | null = null, model = 'claude-opus-5-5'): ClaudeMessage =>
|
|
14
|
+
({ type: 'assistant', message: { model, content }, parent_tool_use_id: parent }) as ClaudeMessage;
|
|
15
|
+
const toolResult = (id: string, content: unknown, isError?: boolean, parent: string | null = null): ClaudeMessage =>
|
|
16
|
+
({
|
|
17
|
+
type: 'user',
|
|
18
|
+
message: { content: [{ type: 'tool_result', tool_use_id: id, content, ...(isError === undefined ? {} : { is_error: isError }) }] },
|
|
19
|
+
parent_tool_use_id: parent,
|
|
20
|
+
}) as ClaudeMessage;
|
|
21
|
+
/** The tool's own output object, which the SDK sends beside the text the model reads. */
|
|
22
|
+
const withToolUseResult = (message: ClaudeMessage, result: unknown): ClaudeMessage => ({ ...message, tool_use_result: result }) as ClaudeMessage;
|
|
23
|
+
|
|
24
|
+
describe('Claude messages as Moxxy events', () => {
|
|
25
|
+
it('records the session and the real approval policy from the start of each turn', () => {
|
|
26
|
+
const t = new TurnTranslator();
|
|
27
|
+
|
|
28
|
+
expect(t.translate(init('default'))).toEqual([
|
|
29
|
+
{ type: 'session', sessionId: 's-1' },
|
|
30
|
+
{ type: 'auto_approve', enabled: false },
|
|
31
|
+
]);
|
|
32
|
+
expect(t.translate(init('bypassPermissions'))).toContainEqual({ type: 'auto_approve', enabled: true });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('says how full its context is from the usage of its last call, not that of a helper agent', () => {
|
|
36
|
+
const t = new TurnTranslator();
|
|
37
|
+
const withUsage = (message: ClaudeMessage, usage: Record<string, number>): ClaudeMessage =>
|
|
38
|
+
({ ...message, message: { ...(message as { message: object }).message, usage } }) as ClaudeMessage;
|
|
39
|
+
|
|
40
|
+
const main = t.translate(withUsage(assistant([{ type: 'text', text: 'Reading.' }]), { input_tokens: 2_000, cache_read_input_tokens: 150_000, cache_creation_input_tokens: 500, output_tokens: 40 }));
|
|
41
|
+
const helper = t.translate(withUsage(assistant([{ type: 'text', text: 'Found it.' }], 'toolu_task'), { input_tokens: 9_000, output_tokens: 10 }));
|
|
42
|
+
|
|
43
|
+
expect(main).toContainEqual({ type: 'context_used', tokens: 152_500 });
|
|
44
|
+
expect(helper.filter((e) => e.type === 'context_used')).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('leaves the approval policy alone in plan mode, which says nothing about it', () => {
|
|
48
|
+
expect(new TurnTranslator().translate(init('plan'))).toEqual([{ type: 'session', sessionId: 's-1' }]);
|
|
49
|
+
expect(autoApproveFor('auto')).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('follows a change of permission mode Claude reports in the middle of a session', () => {
|
|
53
|
+
const t = new TurnTranslator();
|
|
54
|
+
const status = (permissionMode?: string): ClaudeMessage =>
|
|
55
|
+
({ type: 'system', subtype: 'status', status: null, session_id: 's-1', ...(permissionMode ? { permissionMode } : {}) }) as ClaudeMessage;
|
|
56
|
+
|
|
57
|
+
expect(t.translate(status('default'))).toEqual([{ type: 'auto_approve', enabled: false }]);
|
|
58
|
+
expect(t.translate(status('bypassPermissions'))).toEqual([{ type: 'auto_approve', enabled: true }]);
|
|
59
|
+
expect(t.translate(status('plan'))).toEqual([]);
|
|
60
|
+
expect(t.translate(status())).toEqual([]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('streams text and thinking as it comes', () => {
|
|
64
|
+
const t = new TurnTranslator();
|
|
65
|
+
const delta = (d: unknown) => ({ type: 'stream_event', event: { type: 'content_block_delta', delta: d }, parent_tool_use_id: null }) as ClaudeMessage;
|
|
66
|
+
|
|
67
|
+
expect(t.translate(delta({ type: 'text_delta', text: 'ZEB' }))).toEqual([{ type: 'text_delta', delta: 'ZEB' }]);
|
|
68
|
+
expect(t.translate(delta({ type: 'thinking_delta', thinking: 'hm' }))).toEqual([{ type: 'reasoning_delta', delta: 'hm' }]);
|
|
69
|
+
expect(t.translate(delta({ type: 'signature_delta' }))).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('turns a finished message into a Moxxy message, and a call into a tool call with its result', () => {
|
|
73
|
+
expect(
|
|
74
|
+
new TurnTranslator().translate(assistant([{ type: 'text', text: 'Reading it.' }, { type: 'tool_use', id: 't', name: 'Read', input: {} }])),
|
|
75
|
+
).toEqual([
|
|
76
|
+
{ type: 'message', text: 'Reading it.', stopReason: 'tool_use' },
|
|
77
|
+
{ type: 'tool_call', callId: 't', name: 'Read', input: {} },
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const t = new TurnTranslator();
|
|
81
|
+
|
|
82
|
+
expect(t.translate(assistant([{ type: 'thinking', thinking: '' }]))).toEqual([]);
|
|
83
|
+
expect(
|
|
84
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_1', name: 'Bash', input: { command: 'echo spike-ok' } }])),
|
|
85
|
+
).toEqual([{ type: 'tool_call', callId: 'toolu_1', name: 'Bash', input: { command: 'echo spike-ok' } }]);
|
|
86
|
+
expect(t.translate(toolResult('toolu_1', 'spike-ok', false))).toEqual([
|
|
87
|
+
{ type: 'tool_result', callId: 'toolu_1', ok: true, output: 'spike-ok' },
|
|
88
|
+
]);
|
|
89
|
+
// A sentence on its own may still be followed by a call: it is marked once the turn ends.
|
|
90
|
+
expect(t.translate(assistant([{ type: 'text', text: 'The output was `spike-ok`.' }]))).toEqual([]);
|
|
91
|
+
expect(t.finish()).toEqual([{ type: 'message', text: 'The output was `spike-ok`.', stopReason: 'end_turn' }]);
|
|
92
|
+
expect(t.finish()).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Claude Code sends each block of a reply as a message of its own, with no stop reason
|
|
96
|
+
// yet: a sentence and the call after it arrive apart (seen live with Claude Code 2.1.311).
|
|
97
|
+
it('marks a sentence Claude sends apart from the call after it as part of the work', () => {
|
|
98
|
+
const t = new TurnTranslator();
|
|
99
|
+
|
|
100
|
+
expect(t.translate(assistant([{ type: 'text', text: 'Building the package.' }]))).toEqual([]);
|
|
101
|
+
expect(t.translate(assistant([{ type: 'tool_use', id: 'toolu_b', name: 'Bash', input: { command: 'pnpm build' } }]))).toEqual([
|
|
102
|
+
{ type: 'message', text: 'Building the package.', stopReason: 'tool_use' },
|
|
103
|
+
{ type: 'tool_call', callId: 'toolu_b', name: 'Bash', input: { command: 'pnpm build' } },
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('marks a sentence by how Claude says its reply stopped', () => {
|
|
108
|
+
const t = new TurnTranslator();
|
|
109
|
+
const stopped = (stopReason: string): ClaudeMessage =>
|
|
110
|
+
({ type: 'stream_event', event: { type: 'message_delta', delta: { stop_reason: stopReason } }, parent_tool_use_id: null }) as ClaudeMessage;
|
|
111
|
+
|
|
112
|
+
t.translate(assistant([{ type: 'text', text: 'Checking.' }]));
|
|
113
|
+
expect(t.translate(stopped('tool_use'))).toEqual([{ type: 'message', text: 'Checking.', stopReason: 'tool_use' }]);
|
|
114
|
+
t.translate(assistant([{ type: 'text', text: 'Done.' }]));
|
|
115
|
+
expect(t.translate(stopped('end_turn'))).toEqual([{ type: 'message', text: 'Done.', stopReason: 'end_turn' }]);
|
|
116
|
+
expect(t.translate(stopped('end_turn'))).toEqual([]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('marks a sentence before a call the approval asked about first as part of the work', () => {
|
|
120
|
+
const t = new TurnTranslator();
|
|
121
|
+
|
|
122
|
+
t.translate(assistant([{ type: 'text', text: 'Editing it.' }]));
|
|
123
|
+
expect(t.announce('toolu_e', 'Edit', { file_path: 'a.ts' })).toEqual([
|
|
124
|
+
{ type: 'message', text: 'Editing it.', stopReason: 'tool_use' },
|
|
125
|
+
{ type: 'tool_call', callId: 'toolu_e', name: 'Edit', input: { file_path: 'a.ts' } },
|
|
126
|
+
]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('keeps a refused or failed call as a failure with what Claude was told', () => {
|
|
130
|
+
const t = new TurnTranslator();
|
|
131
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_2', name: 'Bash', input: {} }]));
|
|
132
|
+
|
|
133
|
+
expect(t.translate(toolResult('toolu_2', 'PreToolUse:Bash hook error: Blocked by a rule', true))).toEqual([
|
|
134
|
+
{ type: 'tool_result', callId: 'toolu_2', ok: false, error: 'PreToolUse:Bash hook error: Blocked by a rule' },
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('reads a result made of text blocks', () => {
|
|
139
|
+
const t = new TurnTranslator();
|
|
140
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_3', name: 'WebFetch', input: {} }]));
|
|
141
|
+
|
|
142
|
+
expect(t.translate(toolResult('toolu_3', [{ type: 'text', text: 'a' }, { type: 'text', text: 'b' }]))).toEqual([
|
|
143
|
+
{ type: 'tool_result', callId: 'toolu_3', ok: true, output: 'a\nb' },
|
|
144
|
+
]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('shows no call to a Moxxy tool: Moxxy records those itself when it runs them', () => {
|
|
148
|
+
const t = new TurnTranslator();
|
|
149
|
+
|
|
150
|
+
expect(t.translate(assistant([{ type: 'tool_use', id: 'toolu_4', name: 'mcp__moxxy__browser_navigate', input: {} }]))).toEqual([]);
|
|
151
|
+
expect(t.translate(toolResult('toolu_4', [{ type: 'text', text: 'ok' }]))).toEqual([]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('announces a call once, even when the approval asked about it first', () => {
|
|
155
|
+
const t = new TurnTranslator();
|
|
156
|
+
|
|
157
|
+
expect(t.announce('toolu_5', 'Edit', { file_path: 'a.ts' })).toEqual([
|
|
158
|
+
{ type: 'tool_call', callId: 'toolu_5', name: 'Edit', input: { file_path: 'a.ts' } },
|
|
159
|
+
]);
|
|
160
|
+
expect(t.translate(assistant([{ type: 'tool_use', id: 'toolu_5', name: 'Edit', input: { file_path: 'a.ts' } }]))).toEqual([]);
|
|
161
|
+
expect(t.announce('toolu_5', 'Edit', {})).toEqual([]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('shows an edit and a new file as diffs from what Claude reports, with the path inside the project', () => {
|
|
165
|
+
const t = new TurnTranslator('/repo');
|
|
166
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_e', name: 'Edit', input: { file_path: '/repo/src/db.ts' } }]));
|
|
167
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_w', name: 'Write', input: { file_path: '/repo/notes.md' } }]));
|
|
168
|
+
const edited = {
|
|
169
|
+
filePath: '/repo/src/db.ts',
|
|
170
|
+
oldString: 'const port = 5432;',
|
|
171
|
+
newString: 'const port = 5433;',
|
|
172
|
+
originalFile: 'const host = "x";\nconst port = 5432;\n',
|
|
173
|
+
structuredPatch: [{ oldStart: 1, oldLines: 2, newStart: 1, newLines: 2, lines: [' const host = "x";', '-const port = 5432;', '+const port = 5433;'] }],
|
|
174
|
+
userModified: false,
|
|
175
|
+
replaceAll: false,
|
|
176
|
+
};
|
|
177
|
+
const created = { type: 'create', filePath: '/repo/notes.md', content: '# Notes\nport 5433\n', structuredPatch: [], originalFile: null, userModified: false };
|
|
178
|
+
|
|
179
|
+
const [edit] = t.translate(withToolUseResult(toolResult('toolu_e', 'The file /repo/src/db.ts has been updated successfully.'), edited));
|
|
180
|
+
const [write] = t.translate(withToolUseResult(toolResult('toolu_w', 'File created successfully at: /repo/notes.md'), created));
|
|
181
|
+
|
|
182
|
+
expect(edit).toMatchObject({
|
|
183
|
+
type: 'tool_result',
|
|
184
|
+
ok: true,
|
|
185
|
+
output: {
|
|
186
|
+
forModel: 'The file /repo/src/db.ts has been updated successfully.',
|
|
187
|
+
display: { kind: 'file-diff', path: 'src/db.ts', mode: 'update', added: 1, removed: 1 },
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
expect(write).toMatchObject({ output: { display: { kind: 'file-diff', path: 'notes.md', mode: 'create', added: 2, removed: 0 } } });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('keeps the image a result carries, such as a screenshot Claude looks at, with the text beside it', () => {
|
|
194
|
+
const t = new TurnTranslator('/repo');
|
|
195
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_s', name: 'mcp__computer-use__screenshot', input: {} }]));
|
|
196
|
+
const image = { type: 'image', source: { type: 'base64', media_type: 'image/png', data: 'iVBORw0K' } };
|
|
197
|
+
|
|
198
|
+
expect(t.translate(toolResult('toolu_s', [{ type: 'text', text: 'Screen 1440x900' }, image, image]))).toEqual([
|
|
199
|
+
{ type: 'tool_result', callId: 'toolu_s', ok: true, output: { forModel: 'Screen 1440x900', mediaType: 'image/png', base64: 'iVBORw0K' } },
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('keeps every different image a result carries, in order', () => {
|
|
204
|
+
const t = new TurnTranslator('/repo');
|
|
205
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_m', name: 'mcp__computer-use__computer_batch', input: {} }]));
|
|
206
|
+
const image = (data: string) => ({ type: 'image', source: { type: 'base64', media_type: 'image/png', data } });
|
|
207
|
+
|
|
208
|
+
expect(t.translate(toolResult('toolu_m', [{ type: 'text', text: 'Two screens' }, image('ONE'), image('TWO')]))).toEqual([
|
|
209
|
+
{
|
|
210
|
+
type: 'tool_result',
|
|
211
|
+
callId: 'toolu_m',
|
|
212
|
+
ok: true,
|
|
213
|
+
output: { forModel: 'Two screens', mediaType: 'image/png', base64: 'ONE', moreImages: [{ mediaType: 'image/png', base64: 'TWO' }] },
|
|
214
|
+
},
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('keeps the text of a result whose report is no file change', () => {
|
|
219
|
+
const t = new TurnTranslator('/repo');
|
|
220
|
+
t.translate(assistant([{ type: 'tool_use', id: 'toolu_b', name: 'Bash', input: {} }]));
|
|
221
|
+
|
|
222
|
+
expect(t.translate(withToolUseResult(toolResult('toolu_b', 'ok'), { stdout: 'ok', stderr: '' }))).toEqual([
|
|
223
|
+
{ type: 'tool_result', callId: 'toolu_b', ok: true, output: 'ok' },
|
|
224
|
+
]);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe("Claude's subagents", () => {
|
|
230
|
+
const launch = (id = 'toolu_agent') =>
|
|
231
|
+
assistant([{ type: 'tool_use', id, name: 'Agent', input: { description: 'List files', prompt: 'list the files', subagent_type: 'Explore' } }]);
|
|
232
|
+
const system = (fields: Record<string, unknown>): ClaudeMessage => ({ type: 'system', ...fields }) as ClaudeMessage;
|
|
233
|
+
|
|
234
|
+
it('show a subagent Claude starts as a subagent, not as a tool call', () => {
|
|
235
|
+
const t = new TurnTranslator();
|
|
236
|
+
|
|
237
|
+
expect(t.translate(launch())).toEqual([{ type: 'subagent_started', id: 'toolu_agent', label: 'List files', agentType: 'Explore', prompt: 'list the files' }]);
|
|
238
|
+
expect(t.announce('toolu_agent', 'Agent', {})).toEqual([]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("show the subagent's tools under it, once, and keep their results out of the conversation", () => {
|
|
242
|
+
const t = new TurnTranslator();
|
|
243
|
+
t.translate(launch());
|
|
244
|
+
|
|
245
|
+
expect(t.translate(assistant([{ type: 'tool_use', id: 'toolu_sub', name: 'Bash', input: { command: 'ls' } }], 'toolu_agent'))).toEqual([
|
|
246
|
+
{ type: 'subagent_tool_call', id: 'toolu_agent', name: 'Bash', input: { command: 'ls' } },
|
|
247
|
+
]);
|
|
248
|
+
// Moxxy's rules are checked for a subagent's call too; that shows nothing more.
|
|
249
|
+
expect(t.announce('toolu_sub', 'Bash', { command: 'ls' })).toEqual([]);
|
|
250
|
+
expect(t.translate(toolResult('toolu_sub', 'a.txt', false, 'toolu_agent'))).toEqual([]);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("show nothing in the main conversation for a subagent's call Moxxy checks before its message arrives", () => {
|
|
254
|
+
const t = new TurnTranslator();
|
|
255
|
+
t.translate(launch());
|
|
256
|
+
|
|
257
|
+
expect(t.announce('toolu_sub', 'Bash', { command: 'ls' }, { inSubagent: true })).toEqual([]);
|
|
258
|
+
expect(t.translate(assistant([{ type: 'tool_use', id: 'toolu_sub', name: 'Bash', input: { command: 'ls' } }], 'toolu_agent'))).toEqual([
|
|
259
|
+
{ type: 'subagent_tool_call', id: 'toolu_agent', name: 'Bash', input: { command: 'ls' } },
|
|
260
|
+
]);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('end a subagent Claude waited for with its answer and tokens', () => {
|
|
264
|
+
const t = new TurnTranslator();
|
|
265
|
+
t.translate(launch());
|
|
266
|
+
|
|
267
|
+
const done = withToolUseResult(toolResult('toolu_agent', [{ type: 'text', text: 'Two files.' }]), { status: 'completed', totalTokens: 1200 });
|
|
268
|
+
|
|
269
|
+
expect(t.translate(done)).toEqual([{ type: 'subagent_completed', id: 'toolu_agent', text: 'Two files.', tokensUsed: 1200 }]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("end a subagent Claude waited for with its own report, not Claude's hand-back wrapping, even after its task note", () => {
|
|
273
|
+
const t = new TurnTranslator();
|
|
274
|
+
t.translate(launch());
|
|
275
|
+
t.translate(system({ subtype: 'task_progress', tool_use_id: 'toolu_agent', usage: { total_tokens: 13883 } }));
|
|
276
|
+
|
|
277
|
+
expect(t.translate(system({ subtype: 'task_notification', tool_use_id: 'toolu_agent', status: 'completed', summary: 'Two files.' }))).toEqual([]);
|
|
278
|
+
|
|
279
|
+
const wrapped = '[Subagent hand-back] The text below is the final report… The report follows:\n Two files.\nagentId: a1 (use SendMessage…)\n<usage>subagent_tokens: 15846</usage>';
|
|
280
|
+
const done = withToolUseResult(toolResult('toolu_agent', [{ type: 'text', text: wrapped }]), { status: 'completed', content: [{ type: 'text', text: 'Two files.' }] });
|
|
281
|
+
expect(t.translate(done)).toEqual([{ type: 'subagent_completed', id: 'toolu_agent', text: 'Two files.', tokensUsed: 13883 }]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('end a subagent running in the background when Claude reports its task finished, with its last words', () => {
|
|
285
|
+
const subagents = new ClaudeSubagents();
|
|
286
|
+
const first = new TurnTranslator('', new ClaudeTaskList(), subagents);
|
|
287
|
+
first.translate(launch());
|
|
288
|
+
expect(first.translate(withToolUseResult(toolResult('toolu_agent', 'Async agent launched'), { isAsync: true, status: 'async_launched' }))).toEqual([]);
|
|
289
|
+
first.translate(assistant([{ type: 'text', text: 'Two files: a.txt, notes.txt.' }], 'toolu_agent'));
|
|
290
|
+
|
|
291
|
+
const later = new TurnTranslator('', new ClaudeTaskList(), subagents);
|
|
292
|
+
later.translate(system({ subtype: 'task_progress', tool_use_id: 'toolu_agent', usage: { total_tokens: 15199 } }));
|
|
293
|
+
|
|
294
|
+
expect(later.translate(system({ subtype: 'task_notification', tool_use_id: 'toolu_agent', status: 'completed' }))).toEqual([
|
|
295
|
+
{ type: 'subagent_completed', id: 'toolu_agent', text: 'Two files: a.txt, notes.txt.', tokensUsed: 15199 },
|
|
296
|
+
]);
|
|
297
|
+
expect(later.translate(system({ subtype: 'task_notification', tool_use_id: 'toolu_agent', status: 'completed' }))).toEqual([]);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("end a subagent running in the background with Claude's summary when it said nothing more", () => {
|
|
301
|
+
const t = new TurnTranslator();
|
|
302
|
+
t.translate(launch());
|
|
303
|
+
t.translate(withToolUseResult(toolResult('toolu_agent', 'Async agent launched'), { isAsync: true, status: 'async_launched' }));
|
|
304
|
+
|
|
305
|
+
expect(t.translate(system({ subtype: 'task_notification', tool_use_id: 'toolu_agent', status: 'completed', summary: 'Two files.' }))).toEqual([
|
|
306
|
+
{ type: 'subagent_completed', id: 'toolu_agent', text: 'Two files.' },
|
|
307
|
+
]);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('end a background subagent that failed with the reason', () => {
|
|
311
|
+
const t = new TurnTranslator();
|
|
312
|
+
t.translate(launch());
|
|
313
|
+
t.translate(withToolUseResult(toolResult('toolu_agent', 'Async agent launched'), { isAsync: true, status: 'async_launched' }));
|
|
314
|
+
|
|
315
|
+
expect(t.translate(system({ subtype: 'task_notification', tool_use_id: 'toolu_agent', status: 'failed', summary: 'It hit the limit.' }))).toEqual([
|
|
316
|
+
{ type: 'subagent_completed', id: 'toolu_agent', text: '', error: 'It hit the limit.' },
|
|
317
|
+
]);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe('reading Claude results', () => {
|
|
322
|
+
it('counts tokens the way Moxxy does', () => {
|
|
323
|
+
expect(
|
|
324
|
+
toTokenUsage({ input_tokens: 4, output_tokens: 97, cache_read_input_tokens: 84366, cache_creation_input_tokens: 8148 }),
|
|
325
|
+
).toEqual({ inputTokens: 4, outputTokens: 97, cacheReadTokens: 84366, cacheCreationTokens: 8148 });
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('tells an open goal from a met one by what `/goal` answers', () => {
|
|
329
|
+
expect(goalStatusFrom('Goal active: count.txt lists 1 to 400 (not yet evaluated)')).toBe('active');
|
|
330
|
+
expect(goalStatusFrom('No goal set. Usage: `/goal <condition>`')).toBe('complete');
|
|
331
|
+
expect(goalStatusFrom('Something else')).toBeUndefined();
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('the person message for Claude', () => {
|
|
336
|
+
it('passes text, pictures and documents as Messages API blocks', () => {
|
|
337
|
+
expect(
|
|
338
|
+
toClaudeContent([
|
|
339
|
+
{ type: 'text', text: 'What colours?' },
|
|
340
|
+
{ type: 'image', mediaType: 'image/png', data: 'AAA' },
|
|
341
|
+
{ type: 'document', mediaType: 'application/pdf', data: 'BBB' },
|
|
342
|
+
]),
|
|
343
|
+
).toEqual([
|
|
344
|
+
{ type: 'text', text: 'What colours?' },
|
|
345
|
+
{ type: 'image', source: { type: 'base64', media_type: 'image/png', data: 'AAA' } },
|
|
346
|
+
{ type: 'document', source: { type: 'base64', media_type: 'application/pdf', data: 'BBB' } },
|
|
347
|
+
]);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('starts the agent own goal with `/goal` in front of the text', () => {
|
|
351
|
+
expect(toClaudeContent([{ type: 'text', text: 'make goal.txt' }], '/goal ')).toEqual([{ type: 'text', text: '/goal make goal.txt' }]);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
describe("Claude's list of steps", () => {
|
|
356
|
+
const call = (t: TurnTranslator, id: string, name: string, input: unknown) => t.translate(assistant([{ type: 'tool_use', id, name, input }]));
|
|
357
|
+
const finish = (t: TurnTranslator, id: string, text: string, result: unknown) => t.translate(withToolUseResult(toolResult(id, text), result));
|
|
358
|
+
const stepsOf = (events: ReturnType<TurnTranslator['translate']>) => (events[0] as { output: { display: { steps: unknown } } }).output.display.steps;
|
|
359
|
+
|
|
360
|
+
// As Claude Code 2.1.296 with claude-haiku-4-5 sent them, live.
|
|
361
|
+
it('follows the tasks Claude creates and updates one at a time, as a whole list after each call', () => {
|
|
362
|
+
const t = new TurnTranslator('/repo');
|
|
363
|
+
call(t, 'c1', 'TaskCreate', { subject: "Create a.txt containing 'a'", description: 'Write file a.txt' });
|
|
364
|
+
call(t, 'c2', 'TaskCreate', { subject: "Create b.txt containing 'b'", description: 'Write file b.txt' });
|
|
365
|
+
finish(t, 'c1', "Task #1 created successfully: Create a.txt containing 'a'", { task: { id: '1', subject: "Create a.txt containing 'a'" } });
|
|
366
|
+
const created = finish(t, 'c2', "Task #2 created successfully: Create b.txt containing 'b'", { task: { id: '2', subject: "Create b.txt containing 'b'" } });
|
|
367
|
+
call(t, 'u1', 'TaskUpdate', { taskId: '1', status: 'completed' });
|
|
368
|
+
|
|
369
|
+
const updated = finish(t, 'u1', 'Updated task #1 status', { success: true, taskId: '1', updatedFields: ['status'], statusChange: { from: 'pending', to: 'completed' } });
|
|
370
|
+
|
|
371
|
+
expect(stepsOf(created)).toEqual([
|
|
372
|
+
{ text: "Create a.txt containing 'a'", status: 'pending' },
|
|
373
|
+
{ text: "Create b.txt containing 'b'", status: 'pending' },
|
|
374
|
+
]);
|
|
375
|
+
expect(updated).toEqual([
|
|
376
|
+
{
|
|
377
|
+
type: 'tool_result',
|
|
378
|
+
callId: 'u1',
|
|
379
|
+
ok: true,
|
|
380
|
+
output: {
|
|
381
|
+
forModel: 'Updated task #1 status',
|
|
382
|
+
display: {
|
|
383
|
+
kind: 'step-list',
|
|
384
|
+
steps: [
|
|
385
|
+
{ text: "Create a.txt containing 'a'", status: 'completed' },
|
|
386
|
+
{ text: "Create b.txt containing 'b'", status: 'pending' },
|
|
387
|
+
],
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
]);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('keeps the list across turns, drops a deleted task, and takes TaskList as the whole list', () => {
|
|
395
|
+
const tasks = new ClaudeTaskList();
|
|
396
|
+
const first = new TurnTranslator('/repo', tasks);
|
|
397
|
+
call(first, 'c1', 'TaskCreate', { subject: 'One' });
|
|
398
|
+
finish(first, 'c1', 'Task #1 created', { task: { id: '1', subject: 'One' } });
|
|
399
|
+
call(first, 'c2', 'TaskCreate', { subject: 'Two' });
|
|
400
|
+
finish(first, 'c2', 'Task #2 created', { task: { id: '2', subject: 'Two' } });
|
|
401
|
+
|
|
402
|
+
const next = new TurnTranslator('/repo', tasks);
|
|
403
|
+
call(next, 'u2', 'TaskUpdate', { taskId: '2', status: 'deleted' });
|
|
404
|
+
expect(stepsOf(finish(next, 'u2', 'Updated task #2', { success: true, taskId: '2' }))).toEqual([{ text: 'One', status: 'pending' }]);
|
|
405
|
+
call(next, 'l1', 'TaskList', {});
|
|
406
|
+
const listed = finish(next, 'l1', '#1 [in_progress] One\n#3 [pending] Three', {
|
|
407
|
+
tasks: [
|
|
408
|
+
{ id: '1', subject: 'One', status: 'in_progress' },
|
|
409
|
+
{ id: '3', subject: 'Three', status: 'pending' },
|
|
410
|
+
],
|
|
411
|
+
});
|
|
412
|
+
expect(stepsOf(listed)).toEqual([
|
|
413
|
+
{ text: 'One', status: 'in_progress' },
|
|
414
|
+
{ text: 'Three', status: 'pending' },
|
|
415
|
+
]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('leaves the text of an update to a task it never saw created, as after Moxxy started a new Claude', () => {
|
|
419
|
+
const t = new TurnTranslator('/repo');
|
|
420
|
+
call(t, 'u9', 'TaskUpdate', { taskId: '9', status: 'completed' });
|
|
421
|
+
|
|
422
|
+
expect(finish(t, 'u9', 'Updated task #9 status', { success: true, taskId: '9' })).toEqual([{ type: 'tool_result', callId: 'u9', ok: true, output: 'Updated task #9 status' }]);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('reads an older Claude TodoWrite list from what it wrote', () => {
|
|
426
|
+
const t = new TurnTranslator('/repo');
|
|
427
|
+
call(t, 'w1', 'TodoWrite', {
|
|
428
|
+
todos: [
|
|
429
|
+
{ content: 'Create a.txt', status: 'completed', activeForm: 'Creating a.txt' },
|
|
430
|
+
{ content: 'Create b.txt', status: 'in_progress', activeForm: 'Creating b.txt' },
|
|
431
|
+
],
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
expect(stepsOf(finish(t, 'w1', 'Todos have been modified successfully.', { oldTodos: [], newTodos: [] }))).toEqual([
|
|
435
|
+
{ text: 'Create a.txt', status: 'completed' },
|
|
436
|
+
{ text: 'Create b.txt', status: 'in_progress' },
|
|
437
|
+
]);
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
describe("Claude's account limit", () => {
|
|
442
|
+
/** As Claude Code 2.1.296 sent it on a Pro plan with 92% of the week used. */
|
|
443
|
+
const rateLimit = (info: Record<string, unknown>): ClaudeMessage => ({ type: 'rate_limit_event', rate_limit_info: info, session_id: 's-1' }) as unknown as ClaudeMessage;
|
|
444
|
+
const week = { rateLimitType: 'seven_day', resetsAt: 1792216800, utilization: 0.92, overageStatus: 'rejected', isUsingOverage: false };
|
|
445
|
+
|
|
446
|
+
it('reads how much of the window is used and when it resets', () => {
|
|
447
|
+
const t = new TurnTranslator();
|
|
448
|
+
expect(t.translate(rateLimit({ status: 'allowed_warning', ...week }))).toEqual([
|
|
449
|
+
{ type: 'account_limit', limit: { state: 'near', resetsAt: 1792216800_000, windowMinutes: 10080, usedPercent: 92 } },
|
|
450
|
+
]);
|
|
451
|
+
expect(t.translate(rateLimit({ status: 'allowed', rateLimitType: 'five_hour', resetsAt: 1791822600, utilization: 0.4 }))).toEqual([
|
|
452
|
+
{ type: 'account_limit', limit: { state: 'ok', resetsAt: 1791822600_000, windowMinutes: 300, usedPercent: 40 } },
|
|
453
|
+
]);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('counts a used-up window as reached unless Claude goes on with extra usage', () => {
|
|
457
|
+
expect(new TurnTranslator().translate(rateLimit({ status: 'rejected', ...week, utilization: 1 }))).toEqual([
|
|
458
|
+
{ type: 'account_limit', limit: { state: 'reached', resetsAt: 1792216800_000, windowMinutes: 10080, usedPercent: 100 } },
|
|
459
|
+
]);
|
|
460
|
+
const extra = new TurnTranslator().translate(rateLimit({ status: 'rejected', ...week, utilization: 1, overageStatus: 'allowed', isUsingOverage: true }));
|
|
461
|
+
expect(extra).toEqual([{ type: 'account_limit', limit: expect.objectContaining({ state: 'near' }) }]);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it("shows Claude's own limit message as the reached limit, with the reset Claude reported, not as its answer", () => {
|
|
465
|
+
const t = new TurnTranslator();
|
|
466
|
+
t.translate(rateLimit({ status: 'rejected', ...week, utilization: 1 }));
|
|
467
|
+
const limitMessage = { ...(assistant([{ type: 'text', text: "You've hit your limit · resets Oct 18, 6pm" }], null, '<synthetic>') as object), error: 'rate_limit' } as unknown as ClaudeMessage;
|
|
468
|
+
|
|
469
|
+
expect(t.translate(limitMessage)).toEqual([
|
|
470
|
+
{ type: 'account_limit', limit: { state: 'reached', resetsAt: 1792216800_000, windowMinutes: 10080, usedPercent: 100 } },
|
|
471
|
+
]);
|
|
472
|
+
expect(new TurnTranslator().translate(limitMessage)).toEqual([{ type: 'account_limit', limit: { state: 'reached' } }]);
|
|
473
|
+
});
|
|
474
|
+
});
|