@cmodjs/core 6.3.0 → 8.0.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/docs/commands.md +123 -65
- package/docs/dependencies.md +189 -4
- package/docs/index.md +56 -12
- package/docs/install-steps.md +20 -17
- package/docs/jobs.md +43 -16
- package/docs/mod.md +43 -27
- package/docs/options.md +2 -2
- package/docs/session.md +58 -32
- package/docs/testing.md +88 -24
- package/docs/ui.md +128 -58
- package/installer.d.ts +2 -2
- package/jobs/context.js +1 -1
- package/jobs/program.d.ts +4 -1
- package/jobs/program.d.ts.map +1 -1
- package/jobs/program.js +79 -20
- package/mod.d.ts +59 -9
- package/mod.d.ts.map +1 -1
- package/mod.js +5 -3
- package/package.json +1 -1
- package/records.d.ts +15 -2
- package/records.d.ts.map +1 -1
- package/records.js +24 -10
- package/register.d.ts.map +1 -1
- package/register.js +7 -4
- package/rpc.d.ts +7 -0
- package/rpc.d.ts.map +1 -0
- package/rpc.js +59 -0
- package/runtime/bound.d.ts +1 -0
- package/runtime/bound.d.ts.map +1 -1
- package/runtime/bound.js +10 -0
- package/runtime/bun.d.ts +20 -0
- package/runtime/bun.d.ts.map +1 -0
- package/runtime/bun.js +20 -0
- package/runtime/claude.d.ts +1 -0
- package/runtime/claude.d.ts.map +1 -1
- package/runtime/conversations.d.ts +12 -1
- package/runtime/conversations.d.ts.map +1 -1
- package/runtime/conversations.js +472 -362
- package/runtime/deadline.d.ts +4 -3
- package/runtime/deadline.d.ts.map +1 -1
- package/runtime/deadline.js +10 -4
- package/runtime/engine.d.ts +5 -4
- package/runtime/engine.d.ts.map +1 -1
- package/runtime/installer.d.ts +3 -3
- package/runtime/installer.d.ts.map +1 -1
- package/runtime/installer.js +10 -9
- package/runtime/interfaces.d.ts +262 -0
- package/runtime/interfaces.d.ts.map +1 -0
- package/runtime/interfaces.js +935 -0
- package/runtime/lifecycle.d.ts +3 -3
- package/runtime/lifecycle.d.ts.map +1 -1
- package/runtime/lifecycle.js +83 -52
- package/runtime/options.d.ts.map +1 -1
- package/runtime/options.js +2 -6
- package/runtime/parts.d.ts +64 -1
- package/runtime/parts.d.ts.map +1 -1
- package/runtime/parts.js +311 -4
- package/runtime/programs.d.ts +1 -0
- package/runtime/programs.d.ts.map +1 -1
- package/runtime/programs.js +5 -3
- package/runtime/rpc.d.ts +111 -0
- package/runtime/rpc.d.ts.map +1 -0
- package/runtime/rpc.js +387 -0
- package/runtime/sessions.d.ts +6 -4
- package/runtime/sessions.d.ts.map +1 -1
- package/runtime/sessions.js +59 -80
- package/runtime/toaster.js +1 -1
- package/runtime/ui.d.ts +23 -10
- package/runtime/ui.d.ts.map +1 -1
- package/runtime/ui.js +63 -52
- package/runtime/web.d.ts +0 -73
- package/runtime/web.d.ts.map +1 -1
- package/runtime/web.js +9 -5
- package/runtime/widgets.d.ts +4 -2
- package/runtime/widgets.d.ts.map +1 -1
- package/runtime/widgets.js +17 -16
- package/session/conversation.d.ts +7 -1
- package/session/conversation.d.ts.map +1 -1
- package/session/session.d.ts +31 -7
- package/session/session.d.ts.map +1 -1
- package/session/session.js +1 -0
- package/src/installer.ts +2 -2
- package/src/jobs/context.ts +1 -1
- package/src/jobs/program.ts +76 -21
- package/src/mod.ts +51 -10
- package/src/records.ts +33 -9
- package/src/register.ts +7 -4
- package/src/rpc.ts +67 -0
- package/src/runtime/bound.ts +10 -0
- package/src/runtime/bun.ts +40 -0
- package/src/runtime/claude.ts +1 -0
- package/src/runtime/conversations.ts +470 -352
- package/src/runtime/deadline.ts +13 -6
- package/src/runtime/engine.ts +5 -4
- package/src/runtime/installer.ts +13 -12
- package/src/runtime/interfaces.ts +1161 -0
- package/src/runtime/lifecycle.ts +102 -59
- package/src/runtime/options.ts +2 -5
- package/src/runtime/parts.ts +364 -5
- package/src/runtime/programs.ts +6 -3
- package/src/runtime/rpc.ts +457 -0
- package/src/runtime/sessions.ts +63 -83
- package/src/runtime/toaster.ts +1 -1
- package/src/runtime/ui.ts +82 -61
- package/src/runtime/web.ts +10 -39
- package/src/runtime/widgets.ts +20 -18
- package/src/session/conversation.ts +7 -1
- package/src/session/session.ts +22 -7
- package/src/testing/fake-claude.ts +38 -7
- package/src/testing/fake-serve.ts +144 -0
- package/src/testing/fake-sessions.ts +21 -6
- package/src/testing.ts +156 -68
- package/src/ui/areas.ts +2 -2
- package/src/ui/elements.ts +21 -16
- package/src/ui/panes.ts +14 -7
- package/testing/fake-claude.d.ts +6 -0
- package/testing/fake-claude.d.ts.map +1 -1
- package/testing/fake-claude.js +34 -7
- package/testing/fake-serve.d.ts +39 -0
- package/testing/fake-serve.d.ts.map +1 -0
- package/testing/fake-serve.js +110 -0
- package/testing/fake-sessions.d.ts.map +1 -1
- package/testing/fake-sessions.js +22 -6
- package/testing.d.ts +21 -4
- package/testing.d.ts.map +1 -1
- package/testing.js +138 -56
- package/ui/areas.d.ts +3 -1
- package/ui/areas.d.ts.map +1 -1
- package/ui/elements.d.ts +18 -9
- package/ui/elements.d.ts.map +1 -1
- package/ui/elements.js +9 -8
- package/ui/panes.d.ts +10 -6
- package/ui/panes.d.ts.map +1 -1
- package/ui/panes.js +5 -3
- package/runtime/pages.d.ts +0 -32
- package/runtime/pages.d.ts.map +0 -1
- package/runtime/pages.js +0 -142
- package/src/runtime/pages.ts +0 -183
package/src/ui/elements.ts
CHANGED
|
@@ -19,6 +19,7 @@ import type {
|
|
|
19
19
|
TextProps as ClaudeTextProps,
|
|
20
20
|
UiPressArgument,
|
|
21
21
|
} from 'claude-code'
|
|
22
|
+
import type { Surface } from '../mod.js'
|
|
22
23
|
|
|
23
24
|
export type BoxProps = ClaudeBoxProps
|
|
24
25
|
export type TextProps = ClaudeTextProps
|
|
@@ -26,12 +27,12 @@ export type CodeProps = ClaudeCodeProps
|
|
|
26
27
|
export type MarkdownProps = ClaudeMarkdownProps
|
|
27
28
|
export type InputProps = ClaudeInputProps
|
|
28
29
|
export type SelectProps = ClaudeSelectProps
|
|
29
|
-
export type ImageProps = ClaudeImageProps
|
|
30
|
-
export type SvgProps = ClaudeSvgProps
|
|
31
|
-
export type RasterProps = ClaudeRasterProps
|
|
30
|
+
export type ImageProps = Omit<ClaudeImageProps, 'alt'> & { readonly label: string }
|
|
31
|
+
export type SvgProps = Omit<ClaudeSvgProps, 'alt'> & { readonly label: string }
|
|
32
|
+
export type RasterProps = ClaudeRasterProps & { readonly label: string }
|
|
32
33
|
export type ClientProps = ClaudeClientProps
|
|
33
34
|
export type Color = ClaudeColor
|
|
34
|
-
export type Press = UiPressArgument
|
|
35
|
+
export type Press = Omit<UiPressArgument, 'surface'> & { readonly surface: Surface }
|
|
35
36
|
|
|
36
37
|
export type ButtonProps = Omit<ClaudeButtonProps, 'onPress'> & ({ readonly onPress: (press: Press) => void; readonly href?: never } | { readonly href: string; readonly onPress?: never })
|
|
37
38
|
|
|
@@ -100,17 +101,17 @@ const handlerKey = '$cmodHandler'
|
|
|
100
101
|
|
|
101
102
|
export type Handler = (...args: unknown[]) => unknown
|
|
102
103
|
|
|
103
|
-
export function elementData(value: unknown,
|
|
104
|
-
if (typeof value === 'function') return { [handlerKey]:
|
|
105
|
-
if (Array.isArray(value)) return value.map((each: unknown) => elementData(each,
|
|
104
|
+
export function elementData(value: unknown, numberOf: (run: Handler) => number): unknown {
|
|
105
|
+
if (typeof value === 'function') return { [handlerKey]: numberOf(value as Handler) }
|
|
106
|
+
if (Array.isArray(value)) return value.map((each: unknown) => elementData(each, numberOf))
|
|
106
107
|
if (typeof value !== 'object' || value === null) return value
|
|
107
108
|
if (unbuilt in value) {
|
|
108
109
|
const { name, props } = (value as Unbuilt)[unbuilt]
|
|
109
110
|
if (name === 'Client') throw new Error('Client draws only in panes and styles.')
|
|
110
|
-
return { [elementKey]: name, props: elementData(props,
|
|
111
|
+
return { [elementKey]: name, props: elementData(props, numberOf) }
|
|
111
112
|
}
|
|
112
113
|
if (Object.getPrototypeOf(value) !== Object.prototype) throw new Error('a widget draws only the elements of ui/elements.js and ui/components.js, text, numbers, and plain objects.')
|
|
113
|
-
return Object.fromEntries(Object.entries(value).map(([key, each]) => [key, elementData(each,
|
|
114
|
+
return Object.fromEntries(Object.entries(value).map(([key, each]) => [key, elementData(each, numberOf)]))
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
export function elementOf(data: unknown, keyPrefix: string, press: (handler: number, args: readonly unknown[]) => void): unknown {
|
|
@@ -139,17 +140,21 @@ export function drawsFocusable(data: unknown, key: string): boolean {
|
|
|
139
140
|
return Object.values(fields).some((each) => drawsFocusable(each, key))
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
export function
|
|
143
|
-
const { Button: button, Link: link } = table as CompletedTable
|
|
143
|
+
export function forClaudeCode(table: ElementTable, surface: RenderSurface, open: (href: string, press: UiPressArgument) => void): ElementTable {
|
|
144
|
+
const { Button: button, Link: link, Image: image, Svg: svg, Raster: raster } = table as CompletedTable
|
|
144
145
|
const opening = (props: Readonly<Record<string, unknown>>) => {
|
|
145
146
|
const { href, ...rest } = props
|
|
146
147
|
if (typeof href !== 'string') return button(props)
|
|
147
148
|
const { label, children } = rest
|
|
148
149
|
const hasChildren = children !== undefined && !(Array.isArray(children) && children.length === 0)
|
|
149
150
|
if (surface !== 'terminal' && href.startsWith('https:')) return link({ href, ...(typeof label === 'string' ? { label } : {}), ...(hasChildren ? { children } : {}) })
|
|
150
|
-
return button({ ...rest, onPress: (press:
|
|
151
|
+
return button({ ...rest, onPress: (press: UiPressArgument) => open(href, press) })
|
|
151
152
|
}
|
|
152
|
-
|
|
153
|
+
const named =
|
|
154
|
+
(draw: (props: object) => RenderElement) =>
|
|
155
|
+
({ label, ...rest }: Readonly<Record<string, unknown>>) =>
|
|
156
|
+
draw({ alt: label, ...rest })
|
|
157
|
+
return { ...table, Button: opening, Image: named(image), Svg: named(svg), Raster: ({ label, ...rest }: Readonly<Record<string, unknown>>) => raster(rest) }
|
|
153
158
|
}
|
|
154
159
|
|
|
155
160
|
export const Box = element('Box')
|
|
@@ -160,9 +165,9 @@ export const Code = element('Code')
|
|
|
160
165
|
export const Markdown = element('Markdown')
|
|
161
166
|
export const Input = element('Input')
|
|
162
167
|
export const Select = element('Select')
|
|
163
|
-
export const Image = element('Image')
|
|
164
|
-
export const Svg = element('Svg')
|
|
165
|
-
export const Raster = element('Raster')
|
|
168
|
+
export const Image = element('Image') as unknown as ElementConstructor<ImageProps>
|
|
169
|
+
export const Svg = element('Svg') as unknown as ElementConstructor<SvgProps>
|
|
170
|
+
export const Raster = element('Raster') as unknown as ElementConstructor<RasterProps>
|
|
166
171
|
export const Client = element('Client')
|
|
167
172
|
|
|
168
173
|
const constructors = { Box, Text, Button, Link, Code, Markdown, Input, Select, Image, Svg, Raster }
|
package/src/ui/panes.ts
CHANGED
|
@@ -39,15 +39,15 @@ export type Pane<Params = undefined> = {
|
|
|
39
39
|
remove(): void
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
export type
|
|
42
|
+
export type ViewProps<Params> = { readonly params: Params; readonly isFocused: boolean; readonly columns: number; readonly surface: Surface }
|
|
43
43
|
|
|
44
|
-
export type
|
|
44
|
+
export type ViewInit<State extends object = Record<never, never>, Params = undefined> = {
|
|
45
45
|
readonly id: string
|
|
46
46
|
readonly title: string
|
|
47
|
-
render(mod: Mod<State>, props:
|
|
47
|
+
render(mod: Mod<State>, props: ViewProps<Params>): JSX.Element
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
export type
|
|
50
|
+
export type View<Params = undefined> = {
|
|
51
51
|
readonly id: string
|
|
52
52
|
readonly isOpen: boolean
|
|
53
53
|
readonly isShown: boolean
|
|
@@ -61,17 +61,24 @@ export type Page<Params = undefined> = {
|
|
|
61
61
|
remove(): void
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
export type PageProps<Params> = ViewProps<Params>
|
|
65
|
+
|
|
66
|
+
export type PageInit<State extends object = Record<never, never>, Params = undefined> = ViewInit<State, Params>
|
|
67
|
+
|
|
68
|
+
export type Page<Params = undefined> = View<Params>
|
|
69
|
+
|
|
64
70
|
const partId = /^[A-Za-z0-9_-]{1,64}$/
|
|
65
71
|
|
|
66
72
|
export const sizeKeys = ['columns', 'rows'] as const
|
|
67
73
|
|
|
68
|
-
export function checkPart(name: string, kind: string, id: string, title: string): void {
|
|
74
|
+
export function checkPart(name: string, kind: string, id: string, title: string, takenBy: string | undefined): void {
|
|
69
75
|
if (!partId.test(id)) throw new Error(`${name}: "${id}" is not a ${kind} id. Use 1 to 64 letters, digits, "_", or "-".`)
|
|
70
76
|
if (title.trim() === '') throw new Error(`${name}: the ${kind} "${id}" needs a title, the label its tab shows.`)
|
|
77
|
+
if (takenBy !== undefined) throw new Error(`${name}: the ${takenBy} "${id}" is already added. Give the ${kind} another id.`)
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
export function checkPane<State extends object, Params>(name: string, pane: PaneInit<State, Params
|
|
74
|
-
checkPart(name, 'pane', pane.id, pane.title)
|
|
80
|
+
export function checkPane<State extends object, Params>(name: string, pane: PaneInit<State, Params>, takenBy: string | undefined): void {
|
|
81
|
+
checkPart(name, 'pane', pane.id, pane.title, takenBy)
|
|
75
82
|
for (const key of sizeKeys) {
|
|
76
83
|
const size = pane[key]
|
|
77
84
|
if (typeof size !== 'function' && size !== undefined && (!Number.isInteger(size) || size <= 0)) throw new Error(`${name}: the pane "${pane.id}" has ${key} ${size}. Use a whole number above 0, or leave it out.`)
|
package/testing/fake-claude.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { EventResult } from 'claude-code';
|
|
2
2
|
import type { Claude } from '../runtime/claude.js';
|
|
3
|
+
import type { Device } from '../session/context/turn/device.js';
|
|
4
|
+
import { type FakeServe } from './fake-serve.js';
|
|
5
|
+
import type { SessionComputer } from '../runtime/interfaces.js';
|
|
3
6
|
export type TestCall = {
|
|
4
7
|
readonly call: string;
|
|
5
8
|
readonly args: readonly unknown[];
|
|
@@ -67,7 +70,9 @@ export type Fakes = {
|
|
|
67
70
|
device: {
|
|
68
71
|
read?: Claude['device']['read'];
|
|
69
72
|
};
|
|
73
|
+
serve: Partial<SessionComputer>;
|
|
70
74
|
};
|
|
75
|
+
export declare const testDevice: Device;
|
|
71
76
|
export type ShownSchedule = {
|
|
72
77
|
readonly id: string;
|
|
73
78
|
readonly cron: string;
|
|
@@ -89,6 +94,7 @@ export type FakeClaude = {
|
|
|
89
94
|
readonly fakes: Fakes;
|
|
90
95
|
readonly shown: Shown;
|
|
91
96
|
readonly store: Map<string, unknown>;
|
|
97
|
+
readonly serve: FakeServe;
|
|
92
98
|
settle(): Promise<void>;
|
|
93
99
|
placePane(id: string): void;
|
|
94
100
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake-claude.d.ts","sourceRoot":"","sources":["../src/testing/fake-claude.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAS,MAAM,aAAa,CAAA;AAErD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"fake-claude.d.ts","sourceRoot":"","sources":["../src/testing/fake-claude.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAS,MAAM,aAAa,CAAA;AAErD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAElD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAK/D,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAE/D,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,KAAK,GAAG;IAClB,OAAO,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,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AM,EAAE,OAAO,CAAC,CAAA;IACpC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;IACzB,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACvB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAA;CAC5B,CAAA;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,WAAW,CAAC,GAAG,OAAO,CAGtF;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,UAAU,CAkN/F"}
|
package/testing/fake-claude.js
CHANGED
|
@@ -4,7 +4,8 @@ import { partMethods } from '../runtime/ui.js';
|
|
|
4
4
|
import { untagged } from '../session/context/turn/message/note.js';
|
|
5
5
|
import { elements } from './fake-elements.js';
|
|
6
6
|
import { fakeFiles } from './fake-files.js';
|
|
7
|
-
|
|
7
|
+
import { fakeServe } from './fake-serve.js';
|
|
8
|
+
export const testDevice = { id: '5f0e8c2a9b7d4e31a6c4d2b0e8f71a93', name: 'test-computer', os: 'linux', arch: 'x64' };
|
|
8
9
|
export function calledAnswer(caller, answer) {
|
|
9
10
|
if (isObject(answer) && typeof answer['deny'] === 'string')
|
|
10
11
|
throw new Error(`${caller}: $.cmod.call: ${answer['deny']}`);
|
|
@@ -12,12 +13,18 @@ export function calledAnswer(caller, answer) {
|
|
|
12
13
|
}
|
|
13
14
|
export function fakeClaude(plugin) {
|
|
14
15
|
const calls = [];
|
|
15
|
-
const fakes = { process: {}, fs: {}, http: {}, settings: {}, config: {}, command: {}, ui: {}, session: {}, prompt: {}, model: {}, agent: {}, clock: {}, cmod: {}, tool: {}, device: {} };
|
|
16
|
+
const fakes = { process: {}, fs: {}, http: {}, settings: {}, config: {}, command: {}, ui: {}, session: {}, prompt: {}, model: {}, agent: {}, clock: {}, cmod: {}, tool: {}, device: {}, serve: {} };
|
|
16
17
|
const emptyDisk = fakeFiles({});
|
|
17
18
|
const shown = { notes: [], prompts: [], logs: [], debug: [], openPanes: new Set(), commands: [], tools: [], schedules: [] };
|
|
18
19
|
const unplacedPanes = new Set();
|
|
19
20
|
let focusedPane;
|
|
20
21
|
const store = new Map();
|
|
22
|
+
const serve = fakeServe(() => fakes.serve, {
|
|
23
|
+
device: async () => {
|
|
24
|
+
const { id, name } = await (fakes.device.read ?? (async () => testDevice))();
|
|
25
|
+
return { id, name };
|
|
26
|
+
},
|
|
27
|
+
});
|
|
21
28
|
const faked = (call, answer) => {
|
|
22
29
|
return (...args) => {
|
|
23
30
|
calls.push({ call, args });
|
|
@@ -33,6 +40,8 @@ export function fakeClaude(plugin) {
|
|
|
33
40
|
return async (...args) => await run(...args);
|
|
34
41
|
};
|
|
35
42
|
const cmodCall = rejected('cmod.call', () => fakes.cmod.call ?? cmodTakesParts);
|
|
43
|
+
const fetchFake = rejected('http.fetch', () => fakes.http.fetch);
|
|
44
|
+
const spawnFake = faked('process.spawn', () => fakes.process.spawn);
|
|
36
45
|
const clonedAsClaudeCodeDoes = (args) => {
|
|
37
46
|
try {
|
|
38
47
|
return structuredClone(args);
|
|
@@ -117,7 +126,7 @@ export function fakeClaude(plugin) {
|
|
|
117
126
|
},
|
|
118
127
|
process: {
|
|
119
128
|
run: rejected('process.run', () => fakes.process.run),
|
|
120
|
-
spawn:
|
|
129
|
+
spawn: (request) => (isServeHolder(request.argv) ? servedHolder(request.argv[0] ?? '') : spawnFake(request)),
|
|
121
130
|
},
|
|
122
131
|
fs: {
|
|
123
132
|
read: rejected('fs.read', () => fakes.fs.read ?? emptyDisk.read),
|
|
@@ -126,7 +135,13 @@ export function fakeClaude(plugin) {
|
|
|
126
135
|
exists: rejected('fs.exists', () => fakes.fs.exists ?? emptyDisk.exists),
|
|
127
136
|
stat: rejected('fs.stat', () => fakes.fs.stat ?? emptyDisk.stat),
|
|
128
137
|
},
|
|
129
|
-
http: {
|
|
138
|
+
http: {
|
|
139
|
+
fetch: async (url, init) => {
|
|
140
|
+
if (fakes.http.fetch === undefined && init?.socketPath?.endsWith('/serve.sock') === true)
|
|
141
|
+
return serve.fetch(url, init);
|
|
142
|
+
return fetchFake(url, init);
|
|
143
|
+
},
|
|
144
|
+
},
|
|
130
145
|
settings: { read: rejected('settings.read', () => fakes.settings.read ?? (async () => ({}))) },
|
|
131
146
|
config: {
|
|
132
147
|
list: rejected('config.list', () => fakes.config.list ?? (async () => [])),
|
|
@@ -184,6 +199,7 @@ export function fakeClaude(plugin) {
|
|
|
184
199
|
env: {
|
|
185
200
|
home: async () => '/test/home',
|
|
186
201
|
dataHome: async () => undefined,
|
|
202
|
+
cacheHome: async () => undefined,
|
|
187
203
|
configHome: async () => undefined,
|
|
188
204
|
browser: async () => undefined,
|
|
189
205
|
sshConnection: async () => undefined,
|
|
@@ -196,15 +212,26 @@ export function fakeClaude(plugin) {
|
|
|
196
212
|
const settle = async () => {
|
|
197
213
|
let seen;
|
|
198
214
|
do {
|
|
199
|
-
seen = calls.length;
|
|
215
|
+
seen = calls.length + serve.requests;
|
|
200
216
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
201
|
-
} while (calls.length !== seen);
|
|
217
|
+
} while (calls.length + serve.requests !== seen);
|
|
202
218
|
};
|
|
203
219
|
const placePane = (id) => {
|
|
204
220
|
if (unplacedPanes.delete(id))
|
|
205
221
|
shown.openPanes.add(id);
|
|
206
222
|
};
|
|
207
|
-
return { claude, calls, fakes, shown, store, settle, placePane };
|
|
223
|
+
return { claude, calls, fakes, shown, store, serve, settle, placePane };
|
|
224
|
+
}
|
|
225
|
+
function isServeHolder(argv) {
|
|
226
|
+
return argv[1] === 'program' && argv[2] === cmodPluginName && argv.at(-2) === cmodPluginName && argv.at(-1) === 'serve';
|
|
227
|
+
}
|
|
228
|
+
function servedHolder(program) {
|
|
229
|
+
const store = program.slice(0, program.lastIndexOf('/programs/'));
|
|
230
|
+
return (async function* () {
|
|
231
|
+
yield { stream: 'stdout', text: `unix:${store}/serve.sock\n` };
|
|
232
|
+
await new Promise(() => undefined);
|
|
233
|
+
return { code: 0, signal: null };
|
|
234
|
+
})();
|
|
208
235
|
}
|
|
209
236
|
function realTimer(ms, fn) {
|
|
210
237
|
const timeout = setTimeout(fn, ms);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { HttpInit, HttpResponse } from 'claude-code';
|
|
2
|
+
import type { Claude } from '../runtime/claude.js';
|
|
3
|
+
import { type Implementation, type ProgramReach, type Provided, type ProvidedHeard, type ProviderIdentity, type ServedProviders, type SessionComputer, type SessionRouting } from '../runtime/interfaces.js';
|
|
4
|
+
import type { Device } from '../session/context/turn/device.js';
|
|
5
|
+
type Here = Pick<Device, 'id' | 'name'>;
|
|
6
|
+
export type ServeConnection = {
|
|
7
|
+
readonly provider?: ProviderIdentity;
|
|
8
|
+
readonly tell?: (caller: string, heard: ProvidedHeard, signal: AbortSignal) => Promise<unknown>;
|
|
9
|
+
readonly sessionCode?: (name: string) => readonly string[];
|
|
10
|
+
readonly device?: () => Promise<Here>;
|
|
11
|
+
readonly after: Claude['clock']['after'];
|
|
12
|
+
readonly report: (error: unknown) => void;
|
|
13
|
+
};
|
|
14
|
+
export type FakeProgram = {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly mod: string;
|
|
17
|
+
readonly program: string;
|
|
18
|
+
readonly device?: Here;
|
|
19
|
+
readonly methods: Implementation;
|
|
20
|
+
};
|
|
21
|
+
export type FakeServe = {
|
|
22
|
+
readonly routing: SessionRouting;
|
|
23
|
+
readonly requests: number;
|
|
24
|
+
fetch(url: string, init?: HttpInit): Promise<HttpResponse>;
|
|
25
|
+
connect(connection: ServeConnection): ServedProviders;
|
|
26
|
+
provide(programs: readonly FakeProgram[], connection: Pick<ServeConnection, 'after' | 'report'>): Promise<Provided[]>;
|
|
27
|
+
close(): void;
|
|
28
|
+
};
|
|
29
|
+
export type ServeClock = {
|
|
30
|
+
now(): number;
|
|
31
|
+
after(ms: number, run: () => void): unknown;
|
|
32
|
+
};
|
|
33
|
+
export declare function fakeServe(fakes: () => Partial<SessionComputer>, { device, programs, clock }: {
|
|
34
|
+
readonly device: () => Promise<Here>;
|
|
35
|
+
readonly programs?: ProgramReach;
|
|
36
|
+
readonly clock?: ServeClock;
|
|
37
|
+
}): FakeServe;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=fake-serve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-serve.d.ts","sourceRoot":"","sources":["../src/testing/fake-serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,EAAqF,KAAK,cAAc,EAAE,KAAK,YAAY,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAE/R,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAA;AAE/D,KAAK,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,CAAA;AAEvC,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,gBAAgB,CAAA;IACpC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAC/F,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAAA;IAC1D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CA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MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAA;CAAE,GAC/I,SAAS,CAoEX"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { devicesInterface, interfaceListeners, providerActs, routeSessions, serveProviders } from '../runtime/interfaces.js';
|
|
2
|
+
import { addressCall, connectRpc, RpcError, rpcCodes } from '../runtime/rpc.js';
|
|
3
|
+
const realClock = { now: () => Date.now(), after: (ms, run) => setTimeout(run, ms) };
|
|
4
|
+
const wait = (ms) => new Promise((resolve) => void setTimeout(resolve, ms));
|
|
5
|
+
const notHere = (session) => new RpcError(rpcCodes.notHeld, `No session ${session} on this computer.`);
|
|
6
|
+
const unfaked = {
|
|
7
|
+
sessions: () => [],
|
|
8
|
+
search: () => [],
|
|
9
|
+
read: (_session, part) => (part === 'goal' ? { text: '' } : part === 'state' ? null : []),
|
|
10
|
+
unheld: (session) => Promise.reject(notHere(session)),
|
|
11
|
+
visit: async () => undefined,
|
|
12
|
+
remove: async () => undefined,
|
|
13
|
+
};
|
|
14
|
+
const deviceLink = { mod: 'testMod', program: 'devices' };
|
|
15
|
+
const socketOf = (device) => `/test/devices/${device}/serve.sock`;
|
|
16
|
+
export function fakeServe(fakes, { device, programs, clock = realClock }) {
|
|
17
|
+
const answer = (key) => (fakes()[key] ?? unfaked[key]);
|
|
18
|
+
const linked = new Map();
|
|
19
|
+
let requests = 0;
|
|
20
|
+
const routing = routeSessions({
|
|
21
|
+
device,
|
|
22
|
+
programs: {
|
|
23
|
+
store: programs?.store ?? '/test/store',
|
|
24
|
+
files: programs?.files ?? { list: async () => [], read: async () => undefined },
|
|
25
|
+
fetch: (url, init) => linked.get(init.socketPath ?? '')?.fetch(url, init) ?? programs?.fetch(url, init) ?? Promise.resolve({ status: 404, text: '' }),
|
|
26
|
+
wait: programs?.wait ?? wait,
|
|
27
|
+
},
|
|
28
|
+
now: () => clock.now(),
|
|
29
|
+
after: (ms, run) => clock.after(ms, run),
|
|
30
|
+
report: (error) => {
|
|
31
|
+
throw error;
|
|
32
|
+
},
|
|
33
|
+
computer: {
|
|
34
|
+
sessions: (...args) => answer('sessions')(...args),
|
|
35
|
+
search: (...args) => answer('search')(...args),
|
|
36
|
+
read: (...args) => answer('read')(...args),
|
|
37
|
+
unheld: (...args) => answer('unheld')(...args),
|
|
38
|
+
visit: (...args) => answer('visit')(...args),
|
|
39
|
+
remove: (...args) => answer('remove')(...args),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
const serve = {
|
|
43
|
+
routing,
|
|
44
|
+
get requests() {
|
|
45
|
+
return requests + [...linked.values()].reduce((sum, each) => sum + each.requests, 0);
|
|
46
|
+
},
|
|
47
|
+
async fetch(url, init = {}) {
|
|
48
|
+
requests += 1;
|
|
49
|
+
const answered = await routing.routes.route({ method: init.method ?? 'GET', path: new URL(url).pathname, signal: new AbortController().signal, json: async () => JSON.parse(String(init.body)) });
|
|
50
|
+
if (answered === undefined)
|
|
51
|
+
return { status: 404, ok: false, headers: {}, text: '' };
|
|
52
|
+
return { status: answered.status, ok: answered.status >= 200 && answered.status < 300, headers: {}, text: answered.json === undefined ? '' : JSON.stringify(answered.json) };
|
|
53
|
+
},
|
|
54
|
+
connect: (connection) => connectTo(serve, connection).served,
|
|
55
|
+
async provide(given, connection) {
|
|
56
|
+
if (given.length === 0)
|
|
57
|
+
return [];
|
|
58
|
+
const here = await device();
|
|
59
|
+
const others = new Map(given.flatMap(({ device: on }) => (on === undefined || on.id === here.id ? [] : [[on.id, on]])));
|
|
60
|
+
for (const on of others.values())
|
|
61
|
+
if (!linked.has(socketOf(on.id)))
|
|
62
|
+
linked.set(socketOf(on.id), fakeServe(() => ({}), { device: async () => on, clock }));
|
|
63
|
+
const provided = await Promise.all(given.map(async ({ name, mod, program, device: on, methods }) => {
|
|
64
|
+
const { client, served } = connectTo((on === undefined ? undefined : linked.get(socketOf(on.id))) ?? serve, { ...connection, provider: { mod, program } });
|
|
65
|
+
const each = served.provide(name, methods);
|
|
66
|
+
await client.request(providerActs.providers, { interface: name });
|
|
67
|
+
return each;
|
|
68
|
+
}));
|
|
69
|
+
if (others.size === 0)
|
|
70
|
+
return provided;
|
|
71
|
+
const { client, served } = connectTo(serve, { ...connection, provider: deviceLink });
|
|
72
|
+
served.provide(devicesInterface, {
|
|
73
|
+
devices: () => [...others.values()].map(({ id, name }) => ({ id, name, address: socketOf(id) })),
|
|
74
|
+
send: (to) => {
|
|
75
|
+
throw new Error(`The test kit reaches device ${to} at its address, so it carries no message.`);
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
await client.request(providerActs.providers, { interface: devicesInterface });
|
|
79
|
+
return provided;
|
|
80
|
+
},
|
|
81
|
+
close: () => {
|
|
82
|
+
routing.close();
|
|
83
|
+
for (const each of linked.values())
|
|
84
|
+
each.close();
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return serve;
|
|
88
|
+
}
|
|
89
|
+
function connectTo(serve, { provider, tell = async () => null, sessionCode = () => [], device, after, report }) {
|
|
90
|
+
const listeners = interfaceListeners({ report });
|
|
91
|
+
const client = connectRpc({
|
|
92
|
+
call: addressCall((url, init) => serve.fetch(url, init), 'unix:/test/serve.sock'),
|
|
93
|
+
handle: async (method, params, signal) => {
|
|
94
|
+
const answered = await listeners.handler(method, params, signal);
|
|
95
|
+
if (answered !== undefined)
|
|
96
|
+
return answered.result;
|
|
97
|
+
const provided = served.handler(method, params, signal);
|
|
98
|
+
if (provided !== undefined)
|
|
99
|
+
return provided;
|
|
100
|
+
throw new RpcError(rpcCodes.methodNotFound, `This connection to cmod serve answers no ${method}.`);
|
|
101
|
+
},
|
|
102
|
+
onConnect: async (connected) => {
|
|
103
|
+
await served.replay();
|
|
104
|
+
await listeners.replay(connected);
|
|
105
|
+
},
|
|
106
|
+
wait,
|
|
107
|
+
});
|
|
108
|
+
const served = serveProviders({ serve: client, listeners, ...(provider === undefined ? {} : { provider }), tell, sessionCode, ...(device === undefined ? {} : { device }), after, report });
|
|
109
|
+
return { client, served };
|
|
110
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake-sessions.d.ts","sourceRoot":"","sources":["../src/testing/fake-sessions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAoB,KAAK,cAAc,EAAE,MAAM,kDAAkD,CAAA;AACtH,OAAO,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAA;AACrF,OAAO,EAAE,OAAO,EAA0D,MAAM,4CAA4C,CAAA;AAC5H,OAAO,EAAE,IAAI,EAAE,MAAM,yCAAyC,CAAA;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAA;AAClE,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAA;AAClE,OAAO,EAAE,KAAK,EAAE,MAAM,0CAA0C,CAAA;AAChE,OAAO,EAAyB,KAAK,MAAM,EAAE,MAAM,2CAA2C,CAAA;AAG9F,OAAO,EAAE,KAAK,EAAE,MAAM,0CAA0C,CAAA;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,8CAA8C,CAAA;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,gDAAgD,CAAA;AAC3E,OAAO,EAA4G,KAAK,aAAa,EAAE,KAAK,WAAW,EAAiB,KAAK,QAAQ,EAAE,KAAK,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAC1O,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"fake-sessions.d.ts","sourceRoot":"","sources":["../src/testing/fake-sessions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAoB,KAAK,cAAc,EAAE,MAAM,kDAAkD,CAAA;AACtH,OAAO,EAAE,eAAe,EAAE,MAA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S,CAAA;IAChC,QAAQ,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAC1I,CAAA;AAED,wBAAgB,YAAY,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,MAAM,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,KAAK,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,YAAY,CA+Q9Y"}
|
package/testing/fake-sessions.js
CHANGED
|
@@ -13,7 +13,8 @@ import { Skill } from '../session/context/turn/message/skill.js';
|
|
|
13
13
|
import { ToolCall } from '../session/context/turn/message/tool-call.js';
|
|
14
14
|
import { ToolResult } from '../session/context/turn/message/tool-result.js';
|
|
15
15
|
import { createConversation, emptyConversation, searchLimit, tasksWithAgents } from '../session/conversation.js';
|
|
16
|
-
import {
|
|
16
|
+
import { testDevice } from './fake-claude.js';
|
|
17
|
+
import { conversationMethods, isHeardBy, resultOf } from '../runtime/sessions.js';
|
|
17
18
|
import { receivedMatcher } from '../utils/matcher.js';
|
|
18
19
|
import { firstLine, messageOf } from '../utils/text.js';
|
|
19
20
|
export function fakeSessions({ project, cwd, given, push, callTool, listAgents, decide }) {
|
|
@@ -27,7 +28,7 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
27
28
|
let ids = 0;
|
|
28
29
|
const now = () => new Date((clock += 1000)).toISOString();
|
|
29
30
|
const keep = (data) => {
|
|
30
|
-
const session = { data, conversation: createConversation(emptyConversation(data.id)), messages: [], turns: new Map(), contexts: new Map(), tasks: new Map() };
|
|
31
|
+
const session = { data: { device: { id: testDevice.id, name: testDevice.name }, ...data }, conversation: createConversation(emptyConversation(data.id)), messages: [], turns: new Map(), contexts: new Map(), tasks: new Map() };
|
|
31
32
|
kept.set(data.id, session);
|
|
32
33
|
return session;
|
|
33
34
|
};
|
|
@@ -43,7 +44,7 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
43
44
|
session.tasks.set(change.task.id, change.task);
|
|
44
45
|
session.data = { ...session.data, lastAt: change.type === Message ? change.message.at : session.data.lastAt, ...(session.data.title === '' && change.type === Message && change.message.type === Prompt && change.message.from.type === Person ? { title: firstLine(change.message.text) } : {}) };
|
|
45
46
|
for (const [mod, sessions] of heard) {
|
|
46
|
-
if (
|
|
47
|
+
if ((sessions.get(session.data.id) ?? []).some(({ event, matcher }) => isHeardBy(event, matcher, change)))
|
|
47
48
|
push(mod, session.data.id, change);
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -80,7 +81,7 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
80
81
|
const { session, context, contextOpenedAt, turn, tasks, taskTurns, openCalls } = currentSession().conversation.state;
|
|
81
82
|
return { session, context, contextOpenedAt, ...(turn === undefined ? {} : { turn }), tasks, taskTurns, ...(openCalls === undefined ? {} : { openCalls }) };
|
|
82
83
|
};
|
|
83
|
-
const wantsBefore = (mod, event) =>
|
|
84
|
+
const wantsBefore = (mod, event) => (watches.get(mod)?.get(current) ?? []).includes(event);
|
|
84
85
|
const taskOf = (session, id) => {
|
|
85
86
|
const task = session.tasks.get(id);
|
|
86
87
|
if (task === undefined)
|
|
@@ -101,10 +102,11 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
101
102
|
.filter((session) => isInScope(session, request))
|
|
102
103
|
.map((session) => session.data)
|
|
103
104
|
.toSorted((first, second) => second.lastAt.localeCompare(first.lastAt)),
|
|
105
|
+
[conversationMethods.session]: (request) => sessionOf(String(request['session'])).data,
|
|
104
106
|
[conversationMethods.search](request) {
|
|
105
107
|
const text = String(request['text']);
|
|
106
108
|
if ([...text].length < 3)
|
|
107
|
-
throw new Error('cmod
|
|
109
|
+
throw new Error('cmod sessions search takes at least 3 characters.');
|
|
108
110
|
const inRecord = (message) => {
|
|
109
111
|
const session = kept.get(message.session);
|
|
110
112
|
const task = request['task'];
|
|
@@ -174,6 +176,16 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
174
176
|
const action = String(request['action']);
|
|
175
177
|
const input = isObject(request['input']) ? request['input'] : {};
|
|
176
178
|
const task = typeof request['task'] === 'string' ? request['task'] : undefined;
|
|
179
|
+
if (action === 'visit') {
|
|
180
|
+
session.data = { ...session.data, visitedAt: typeof request['input'] === 'string' ? request['input'] : now() };
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (action === 'delete') {
|
|
184
|
+
if (session.data.isRunning)
|
|
185
|
+
throw new Error(`session ${session.data.id} runs, so it stays. End it first.`);
|
|
186
|
+
kept.delete(session.data.id);
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
177
189
|
const isLive = task === undefined ? session.data.isRunning : session.data.isRunning && taskOf(session, task).closedAt === undefined;
|
|
178
190
|
if (action === 'usage')
|
|
179
191
|
return { model: task === undefined ? session.data.model : (taskOf(session, task).model ?? session.data.model), contextWindow: {} };
|
|
@@ -187,6 +199,10 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
187
199
|
throw new Error(`turn ${String(input['turn'])} has ended, so there is nothing to abort.`);
|
|
188
200
|
return null;
|
|
189
201
|
}
|
|
202
|
+
if (action === 'end') {
|
|
203
|
+
session.data = { ...session.data, isRunning: false };
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
190
206
|
if (action === 'send') {
|
|
191
207
|
sent.push({ session: session.data.id, ...(task === undefined ? {} : { task }), type: String(input['type']), text: String(input['text']), ...(typeof input['as'] === 'string' ? { as: input['as'] } : {}) });
|
|
192
208
|
return null;
|
|
@@ -196,7 +212,7 @@ export function fakeSessions({ project, cwd, given, push, callTool, listAgents,
|
|
|
196
212
|
if (action === 'startTask' || action === 'fork' || action === 'startBackgroundTask' || action === 'clone') {
|
|
197
213
|
if (title.trim() === '')
|
|
198
214
|
throw new Error('a task needs a title.');
|
|
199
|
-
const isClone = action === 'clone';
|
|
215
|
+
const isClone = action === 'clone' || (action === 'fork' && input['isBackground'] === true);
|
|
200
216
|
const isBackground = isClone || action === 'startBackgroundTask';
|
|
201
217
|
const forkedFrom = isClone || action === 'fork' ? session.data.id : undefined;
|
|
202
218
|
const agentType = forkedFrom === undefined ? text('agentType') : undefined;
|
package/testing.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Args, CmodDependencies, EventResult } from 'claude-code';
|
|
2
2
|
import type { CommandReply } from './jobs/slash-command.js';
|
|
3
|
-
import type { ModDefinition } from './mod.js';
|
|
3
|
+
import type { InterfaceMethods, InterfaceName, ModDefinition, Provided } from './mod.js';
|
|
4
4
|
import type { Options, OptionValues } from './options.js';
|
|
5
5
|
import type { ShownToast } from './runtime/toaster.js';
|
|
6
6
|
import { Message } from './session/context/turn/message/message.js';
|
|
@@ -29,9 +29,22 @@ export type Shown = ShownByClaude & {
|
|
|
29
29
|
readonly toasts: readonly ShownToast[];
|
|
30
30
|
readonly sent: FakeSessions['sent'];
|
|
31
31
|
readonly pane: ShownPart;
|
|
32
|
+
readonly view: ShownPart;
|
|
32
33
|
readonly page: ShownPart;
|
|
33
34
|
};
|
|
34
|
-
export type
|
|
35
|
+
export type FakeProvider = {
|
|
36
|
+
readonly [Name in InterfaceName]: {
|
|
37
|
+
readonly name: Name;
|
|
38
|
+
readonly mod: string;
|
|
39
|
+
readonly program?: string;
|
|
40
|
+
readonly device?: {
|
|
41
|
+
readonly id: string;
|
|
42
|
+
readonly name: string;
|
|
43
|
+
};
|
|
44
|
+
readonly methods: InterfaceMethods<Name>;
|
|
45
|
+
};
|
|
46
|
+
}[InterfaceName];
|
|
47
|
+
export type TestOptions<State extends object, Declared extends Options = Options, Given extends readonly FakeProvider[] = readonly FakeProvider[]> = {
|
|
35
48
|
readonly state?: {
|
|
36
49
|
readonly [Lifetime in keyof State]?: Partial<State[Lifetime]>;
|
|
37
50
|
};
|
|
@@ -46,6 +59,7 @@ export type TestOptions<State extends object, Declared extends Options = Options
|
|
|
46
59
|
readonly links?: Readonly<Record<string, string>>;
|
|
47
60
|
readonly secrets?: Readonly<Record<string, string>>;
|
|
48
61
|
readonly sessions?: readonly FakeSession[];
|
|
62
|
+
readonly providers?: Given;
|
|
49
63
|
};
|
|
50
64
|
type Placed<Payload> = Omit<Payload, keyof Placement> & Partial<Placement>;
|
|
51
65
|
type MessageBefore = Exclude<BeforeName, 'TaskStart' | 'TurnEnd' | 'ContextEnd'>;
|
|
@@ -78,7 +92,7 @@ export type Raise = {
|
|
|
78
92
|
beforeRun(run: Placed<CommandRun>): Promise<CommandRunAnswer | undefined>;
|
|
79
93
|
};
|
|
80
94
|
type FiredEvent = 'ui.render' | 'ui.scroll' | 'ui.close' | 'cmod.call' | 'tool.check' | 'prompt.context';
|
|
81
|
-
export type TestedMod<State extends object> = {
|
|
95
|
+
export type TestedMod<State extends object, Given extends readonly FakeProvider[] = readonly FakeProvider[]> = {
|
|
82
96
|
start(): Promise<void>;
|
|
83
97
|
readonly raise: Raise;
|
|
84
98
|
fire<N extends FiredEvent>(event: N, input: Args<N>, below?: EventResult<N>): Promise<EventResult<N>>;
|
|
@@ -109,6 +123,9 @@ export type TestedMod<State extends object> = {
|
|
|
109
123
|
readonly calls: readonly TestCall[];
|
|
110
124
|
readonly fakes: Fakes;
|
|
111
125
|
readonly shown: Shown;
|
|
126
|
+
readonly providers: {
|
|
127
|
+
readonly [Index in keyof Given]: Provided<Given[Index]['name']>;
|
|
128
|
+
};
|
|
112
129
|
};
|
|
113
|
-
export declare function testMod<State extends object, Declared extends Options = Options>(definition: ModDefinition<State, string, Declared>, options?: TestOptions<State, Declared>): TestedMod<State>;
|
|
130
|
+
export declare function testMod<State extends object, Declared extends Options = Options, const Given extends readonly FakeProvider[] = readonly FakeProvider[]>(definition: ModDefinition<State, string, Declared>, options?: TestOptions<State, Declared, Given>): TestedMod<State, Given>;
|
|
114
131
|
//# sourceMappingURL=testing.d.ts.map
|