@scottwalker/claude-connector 0.1.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/LICENSE +21 -0
- package/README.md +444 -0
- package/dist/builder/args-builder.d.ts +64 -0
- package/dist/builder/args-builder.d.ts.map +1 -0
- package/dist/builder/args-builder.js +134 -0
- package/dist/builder/args-builder.js.map +1 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.d.ts.map +1 -0
- package/dist/builder/index.js +2 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/client/claude.d.ts +120 -0
- package/dist/client/claude.d.ts.map +1 -0
- package/dist/client/claude.js +182 -0
- package/dist/client/claude.js.map +1 -0
- package/dist/client/session.d.ts +64 -0
- package/dist/client/session.d.ts.map +1 -0
- package/dist/client/session.js +128 -0
- package/dist/client/session.js.map +1 -0
- package/dist/errors/errors.d.ts +52 -0
- package/dist/errors/errors.d.ts.map +1 -0
- package/dist/errors/errors.js +78 -0
- package/dist/errors/errors.js.map +1 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/executor/cli-executor.d.ts +35 -0
- package/dist/executor/cli-executor.d.ts.map +1 -0
- package/dist/executor/cli-executor.js +223 -0
- package/dist/executor/cli-executor.js.map +1 -0
- package/dist/executor/index.d.ts +4 -0
- package/dist/executor/index.d.ts.map +1 -0
- package/dist/executor/index.js +3 -0
- package/dist/executor/index.js.map +1 -0
- package/dist/executor/interface.d.ts +65 -0
- package/dist/executor/interface.d.ts.map +1 -0
- package/dist/executor/interface.js +2 -0
- package/dist/executor/interface.js.map +1 -0
- package/dist/executor/sdk-executor.d.ts +102 -0
- package/dist/executor/sdk-executor.d.ts.map +1 -0
- package/dist/executor/sdk-executor.js +256 -0
- package/dist/executor/sdk-executor.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/parser/index.d.ts +3 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +3 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/json-parser.d.ts +22 -0
- package/dist/parser/json-parser.d.ts.map +1 -0
- package/dist/parser/json-parser.js +87 -0
- package/dist/parser/json-parser.js.map +1 -0
- package/dist/parser/stream-parser.d.ts +21 -0
- package/dist/parser/stream-parser.d.ts.map +1 -0
- package/dist/parser/stream-parser.js +84 -0
- package/dist/parser/stream-parser.js.map +1 -0
- package/dist/scheduler/index.d.ts +2 -0
- package/dist/scheduler/index.d.ts.map +1 -0
- package/dist/scheduler/index.js +2 -0
- package/dist/scheduler/index.js.map +1 -0
- package/dist/scheduler/scheduler.d.ts +65 -0
- package/dist/scheduler/scheduler.d.ts.map +1 -0
- package/dist/scheduler/scheduler.js +134 -0
- package/dist/scheduler/scheduler.js.map +1 -0
- package/dist/types/client.d.ts +184 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +2 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/result.d.ts +94 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +2 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/session.d.ts +37 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +2 -0
- package/dist/types/session.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/validation.d.ts +15 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +45 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/builder/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAwB,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { ClientOptions, QueryOptions, QueryResult, StreamEvent } from '../types/index.js';
|
|
2
|
+
import type { SessionOptions } from '../types/session.js';
|
|
3
|
+
import type { IExecutor } from '../executor/interface.js';
|
|
4
|
+
import { type InitStage } from '../executor/sdk-executor.js';
|
|
5
|
+
import { Session } from './session.js';
|
|
6
|
+
import { type ScheduledJob } from '../scheduler/scheduler.js';
|
|
7
|
+
/**
|
|
8
|
+
* Main entry point for claude-connector.
|
|
9
|
+
*
|
|
10
|
+
* `Claude` is a **facade** that orchestrates the executor, argument builder,
|
|
11
|
+
* and parsers behind a clean, minimal API.
|
|
12
|
+
*
|
|
13
|
+
* ## Execution modes
|
|
14
|
+
*
|
|
15
|
+
* - **CLI mode** (default): each query spawns a new `claude -p` process.
|
|
16
|
+
* Simple, no warm-up needed, but slower for interactive use.
|
|
17
|
+
*
|
|
18
|
+
* - **SDK mode** (`useSdk: true`): creates a persistent session via the
|
|
19
|
+
* Claude Agent SDK. First query requires warm-up (~5-10s), but subsequent
|
|
20
|
+
* queries are near-instant. Best for interactive and high-throughput use.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // CLI mode (default)
|
|
25
|
+
* const claude = new Claude({ model: 'sonnet' })
|
|
26
|
+
*
|
|
27
|
+
* // SDK mode (persistent session, fast queries)
|
|
28
|
+
* const claude = new Claude({ useSdk: true, model: 'sonnet' })
|
|
29
|
+
* claude.on('init:stage', (stage, msg) => console.log(`[${stage}] ${msg}`))
|
|
30
|
+
* await claude.init() // warm up once
|
|
31
|
+
* const result = await claude.query('Fix bugs') // fast!
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class Claude {
|
|
35
|
+
/** Frozen client-level options. */
|
|
36
|
+
private readonly options;
|
|
37
|
+
/** Executor responsible for running CLI commands. */
|
|
38
|
+
private readonly executor;
|
|
39
|
+
/** SdkExecutor reference (only when useSdk: true) for lifecycle control. */
|
|
40
|
+
private readonly sdkExecutor;
|
|
41
|
+
constructor(options?: ClientOptions, executor?: IExecutor);
|
|
42
|
+
/**
|
|
43
|
+
* Initialize the SDK session (warm up).
|
|
44
|
+
*
|
|
45
|
+
* Only needed when `useSdk: true`. In CLI mode this is a no-op.
|
|
46
|
+
*
|
|
47
|
+
* Subscribe to initialization events before calling:
|
|
48
|
+
* ```ts
|
|
49
|
+
* claude.on('init:stage', (stage, message) => {
|
|
50
|
+
* console.log(`[${stage}] ${message}`)
|
|
51
|
+
* })
|
|
52
|
+
* claude.on('init:ready', () => console.log('Ready!'))
|
|
53
|
+
* claude.on('init:error', (err) => console.error(err))
|
|
54
|
+
*
|
|
55
|
+
* await claude.init()
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Safe to call multiple times — only initializes once.
|
|
59
|
+
*/
|
|
60
|
+
init(): Promise<void>;
|
|
61
|
+
/** Whether the SDK session is initialized and ready (always true for CLI mode). */
|
|
62
|
+
get ready(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Subscribe to initialization events.
|
|
65
|
+
*
|
|
66
|
+
* Events:
|
|
67
|
+
* - `init:stage` `(stage: InitStage, message: string)` — progress updates
|
|
68
|
+
* Stages: `'importing'` → `'creating'` → `'connecting'` → `'ready'`
|
|
69
|
+
* - `init:ready` — session is warm and queries will be fast
|
|
70
|
+
* - `init:error` `(error: Error)` — initialization failed
|
|
71
|
+
*
|
|
72
|
+
* Only meaningful when `useSdk: true`. In CLI mode, listeners are never called.
|
|
73
|
+
*/
|
|
74
|
+
on(event: 'init:stage', listener: (stage: InitStage, message: string) => void): this;
|
|
75
|
+
on(event: 'init:ready', listener: () => void): this;
|
|
76
|
+
on(event: 'init:error', listener: (error: Error) => void): this;
|
|
77
|
+
on(event: string, listener: (...args: never[]) => void): this;
|
|
78
|
+
/**
|
|
79
|
+
* Execute a one-shot query and return the complete result.
|
|
80
|
+
*
|
|
81
|
+
* In SDK mode, auto-initializes if `init()` hasn't been called yet.
|
|
82
|
+
*/
|
|
83
|
+
query(prompt: string, options?: QueryOptions): Promise<QueryResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Execute a query with streaming response.
|
|
86
|
+
*
|
|
87
|
+
* Returns an async iterable that yields events as they arrive.
|
|
88
|
+
* The final event is always `type: 'result'` or `type: 'error'`.
|
|
89
|
+
*/
|
|
90
|
+
stream(prompt: string, options?: QueryOptions): AsyncIterable<StreamEvent>;
|
|
91
|
+
/**
|
|
92
|
+
* Create a session for multi-turn conversation.
|
|
93
|
+
*/
|
|
94
|
+
session(sessionOptions?: SessionOptions): Session;
|
|
95
|
+
/**
|
|
96
|
+
* Schedule a recurring query (equivalent of /loop).
|
|
97
|
+
*/
|
|
98
|
+
loop(interval: string | number, prompt: string, options?: QueryOptions): ScheduledJob;
|
|
99
|
+
/**
|
|
100
|
+
* Run multiple queries in parallel.
|
|
101
|
+
*/
|
|
102
|
+
parallel(queries: readonly {
|
|
103
|
+
prompt: string;
|
|
104
|
+
options?: QueryOptions;
|
|
105
|
+
}[]): Promise<QueryResult[]>;
|
|
106
|
+
/**
|
|
107
|
+
* Abort any running execution on the underlying executor.
|
|
108
|
+
*/
|
|
109
|
+
abort(): void;
|
|
110
|
+
/**
|
|
111
|
+
* Close the SDK session and free resources.
|
|
112
|
+
* Only needed when `useSdk: true`. In CLI mode this is a no-op.
|
|
113
|
+
*/
|
|
114
|
+
close(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Access the underlying executor (for advanced use / testing).
|
|
117
|
+
*/
|
|
118
|
+
getExecutor(): IExecutor;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=claude.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude.d.ts","sourceRoot":"","s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,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,GAAG,IAAI;IAQ7D;;;;OAIG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC;IAkBzE;;;;;OAKG;IACI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,aAAa,CAAC,WAAW,CAAC;IAkBjF;;OAEG;IACH,OAAO,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO;IAIjD;;OAEG;IACH,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,YAAY;IAKrF;;OAEG;IACG,QAAQ,CACZ,OAAO,EAAE,SAAS;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,YAAY,CAAA;KAAE,EAAE,GAC7D,OAAO,CAAC,WAAW,EAAE,CAAC;IAMzB;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;OAGG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,WAAW,IAAI,SAAS;CAGzB"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { CliExecutor } from '../executor/cli-executor.js';
|
|
2
|
+
import { SdkExecutor } from '../executor/sdk-executor.js';
|
|
3
|
+
import { buildArgs, mergeOptions, resolveEnv } from '../builder/args-builder.js';
|
|
4
|
+
import { validateClientOptions, validateQueryOptions, validatePrompt } from '../utils/validation.js';
|
|
5
|
+
import { Session } from './session.js';
|
|
6
|
+
import { Scheduler } from '../scheduler/scheduler.js';
|
|
7
|
+
/**
|
|
8
|
+
* Main entry point for claude-connector.
|
|
9
|
+
*
|
|
10
|
+
* `Claude` is a **facade** that orchestrates the executor, argument builder,
|
|
11
|
+
* and parsers behind a clean, minimal API.
|
|
12
|
+
*
|
|
13
|
+
* ## Execution modes
|
|
14
|
+
*
|
|
15
|
+
* - **CLI mode** (default): each query spawns a new `claude -p` process.
|
|
16
|
+
* Simple, no warm-up needed, but slower for interactive use.
|
|
17
|
+
*
|
|
18
|
+
* - **SDK mode** (`useSdk: true`): creates a persistent session via the
|
|
19
|
+
* Claude Agent SDK. First query requires warm-up (~5-10s), but subsequent
|
|
20
|
+
* queries are near-instant. Best for interactive and high-throughput use.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // CLI mode (default)
|
|
25
|
+
* const claude = new Claude({ model: 'sonnet' })
|
|
26
|
+
*
|
|
27
|
+
* // SDK mode (persistent session, fast queries)
|
|
28
|
+
* const claude = new Claude({ useSdk: true, model: 'sonnet' })
|
|
29
|
+
* claude.on('init:stage', (stage, msg) => console.log(`[${stage}] ${msg}`))
|
|
30
|
+
* await claude.init() // warm up once
|
|
31
|
+
* const result = await claude.query('Fix bugs') // fast!
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class Claude {
|
|
35
|
+
/** Frozen client-level options. */
|
|
36
|
+
options;
|
|
37
|
+
/** Executor responsible for running CLI commands. */
|
|
38
|
+
executor;
|
|
39
|
+
/** SdkExecutor reference (only when useSdk: true) for lifecycle control. */
|
|
40
|
+
sdkExecutor = null;
|
|
41
|
+
constructor(options = {}, executor) {
|
|
42
|
+
validateClientOptions(options);
|
|
43
|
+
this.options = Object.freeze({ ...options });
|
|
44
|
+
if (executor) {
|
|
45
|
+
this.executor = executor;
|
|
46
|
+
}
|
|
47
|
+
else if (options.useSdk) {
|
|
48
|
+
const sdkOpts = {
|
|
49
|
+
model: options.model,
|
|
50
|
+
pathToClaudeCodeExecutable: options.executable,
|
|
51
|
+
permissionMode: options.permissionMode,
|
|
52
|
+
allowedTools: options.allowedTools ? [...options.allowedTools] : undefined,
|
|
53
|
+
disallowedTools: options.disallowedTools ? [...options.disallowedTools] : undefined,
|
|
54
|
+
env: options.env,
|
|
55
|
+
};
|
|
56
|
+
this.sdkExecutor = new SdkExecutor(sdkOpts);
|
|
57
|
+
this.executor = this.sdkExecutor;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.executor = new CliExecutor(options.executable);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Initialize the SDK session (warm up).
|
|
65
|
+
*
|
|
66
|
+
* Only needed when `useSdk: true`. In CLI mode this is a no-op.
|
|
67
|
+
*
|
|
68
|
+
* Subscribe to initialization events before calling:
|
|
69
|
+
* ```ts
|
|
70
|
+
* claude.on('init:stage', (stage, message) => {
|
|
71
|
+
* console.log(`[${stage}] ${message}`)
|
|
72
|
+
* })
|
|
73
|
+
* claude.on('init:ready', () => console.log('Ready!'))
|
|
74
|
+
* claude.on('init:error', (err) => console.error(err))
|
|
75
|
+
*
|
|
76
|
+
* await claude.init()
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* Safe to call multiple times — only initializes once.
|
|
80
|
+
*/
|
|
81
|
+
async init() {
|
|
82
|
+
if (this.sdkExecutor) {
|
|
83
|
+
await this.sdkExecutor.init();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Whether the SDK session is initialized and ready (always true for CLI mode). */
|
|
87
|
+
get ready() {
|
|
88
|
+
if (this.sdkExecutor)
|
|
89
|
+
return this.sdkExecutor.ready;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
on(event, listener) {
|
|
93
|
+
if (this.sdkExecutor) {
|
|
94
|
+
this.sdkExecutor.on(event, listener);
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Execute a one-shot query and return the complete result.
|
|
100
|
+
*
|
|
101
|
+
* In SDK mode, auto-initializes if `init()` hasn't been called yet.
|
|
102
|
+
*/
|
|
103
|
+
async query(prompt, options) {
|
|
104
|
+
validatePrompt(prompt);
|
|
105
|
+
if (options)
|
|
106
|
+
validateQueryOptions(options);
|
|
107
|
+
const resolved = mergeOptions(this.options, options, {
|
|
108
|
+
prompt,
|
|
109
|
+
outputFormat: 'json',
|
|
110
|
+
});
|
|
111
|
+
const args = buildArgs(resolved);
|
|
112
|
+
const env = resolveEnv(this.options, options);
|
|
113
|
+
return this.executor.execute(args, {
|
|
114
|
+
cwd: resolved.cwd,
|
|
115
|
+
env,
|
|
116
|
+
input: options?.input,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Execute a query with streaming response.
|
|
121
|
+
*
|
|
122
|
+
* Returns an async iterable that yields events as they arrive.
|
|
123
|
+
* The final event is always `type: 'result'` or `type: 'error'`.
|
|
124
|
+
*/
|
|
125
|
+
async *stream(prompt, options) {
|
|
126
|
+
validatePrompt(prompt);
|
|
127
|
+
if (options)
|
|
128
|
+
validateQueryOptions(options);
|
|
129
|
+
const resolved = mergeOptions(this.options, options, {
|
|
130
|
+
prompt,
|
|
131
|
+
outputFormat: 'stream-json',
|
|
132
|
+
});
|
|
133
|
+
const args = buildArgs(resolved);
|
|
134
|
+
const env = resolveEnv(this.options, options);
|
|
135
|
+
yield* this.executor.stream(args, {
|
|
136
|
+
cwd: resolved.cwd,
|
|
137
|
+
env,
|
|
138
|
+
input: options?.input,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Create a session for multi-turn conversation.
|
|
143
|
+
*/
|
|
144
|
+
session(sessionOptions) {
|
|
145
|
+
return new Session(this.options, this.executor, sessionOptions);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Schedule a recurring query (equivalent of /loop).
|
|
149
|
+
*/
|
|
150
|
+
loop(interval, prompt, options) {
|
|
151
|
+
const scheduler = new Scheduler(this);
|
|
152
|
+
return scheduler.schedule(interval, prompt, options);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Run multiple queries in parallel.
|
|
156
|
+
*/
|
|
157
|
+
async parallel(queries) {
|
|
158
|
+
return Promise.all(queries.map(({ prompt, options }) => this.query(prompt, options)));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Abort any running execution on the underlying executor.
|
|
162
|
+
*/
|
|
163
|
+
abort() {
|
|
164
|
+
this.executor.abort?.();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Close the SDK session and free resources.
|
|
168
|
+
* Only needed when `useSdk: true`. In CLI mode this is a no-op.
|
|
169
|
+
*/
|
|
170
|
+
close() {
|
|
171
|
+
if (this.sdkExecutor) {
|
|
172
|
+
this.sdkExecutor.close();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Access the underlying executor (for advanced use / testing).
|
|
177
|
+
*/
|
|
178
|
+
getExecutor() {
|
|
179
|
+
return this.executor;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=claude.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude.js","sourceRoot":"","sources":["../../src/client/claude.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAA2C,MAAM,6BAA6B,CAAC;AACnG,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,SAAS,EAAqB,MAAM,2BAA2B,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,MAAM;IACjB,mCAAmC;IAClB,OAAO,CAA0B;IAElD,qDAAqD;IACpC,QAAQ,CAAY;IAErC,4EAA4E;IAC3D,WAAW,GAAuB,IAAI,CAAC;IAExD,YAAY,UAAyB,EAAE,EAAE,QAAoB;QAC3D,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AAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE;YAChC,GAAG,EAAE,QAAQ,CAAC,GAAG;YACjB,GAAG;YACH,KAAK,EAAE,OAAO,EAAE,KAAK;SACtB,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,cAA+B;QACrC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,QAAyB,EAAE,MAAc,EAAE,OAAsB;QACpE,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC;QACtC,OAAO,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ,CACZ,OAA8D;QAE9D,OAAO,OAAO,CAAC,GAAG,CAChB,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAClE,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACrB,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;IACH,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ClientOptions, QueryOptions, QueryResult, StreamEvent } from '../types/index.js';
|
|
2
|
+
import type { SessionOptions } from '../types/session.js';
|
|
3
|
+
import type { IExecutor } from '../executor/interface.js';
|
|
4
|
+
/**
|
|
5
|
+
* A stateful conversation session.
|
|
6
|
+
*
|
|
7
|
+
* Sessions wrap the Claude Code `--continue` / `--resume` mechanism to
|
|
8
|
+
* provide multi-turn conversations with persistent context.
|
|
9
|
+
*
|
|
10
|
+
* ## Lifecycle
|
|
11
|
+
*
|
|
12
|
+
* 1. **First query**: runs normally, captures the `sessionId` from the result.
|
|
13
|
+
* 2. **Subsequent queries**: automatically pass `--resume <sessionId>` to continue
|
|
14
|
+
* the conversation with full context.
|
|
15
|
+
*
|
|
16
|
+
* ## Concurrency
|
|
17
|
+
*
|
|
18
|
+
* Sessions are NOT safe for concurrent queries. Each query must complete before
|
|
19
|
+
* the next one starts. For parallel work, use separate sessions or `claude.parallel()`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const session = claude.session()
|
|
24
|
+
*
|
|
25
|
+
* const r1 = await session.query('What files are in src/?')
|
|
26
|
+
* // Claude reads the directory
|
|
27
|
+
*
|
|
28
|
+
* const r2 = await session.query('Refactor the largest file')
|
|
29
|
+
* // Claude remembers the previous context
|
|
30
|
+
*
|
|
31
|
+
* console.log(session.sessionId) // 'abc-123-...'
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class Session {
|
|
35
|
+
private readonly clientOptions;
|
|
36
|
+
private readonly executor;
|
|
37
|
+
private readonly sessionOptions;
|
|
38
|
+
/** Session ID, populated after the first query completes. */
|
|
39
|
+
private _sessionId;
|
|
40
|
+
/** Number of queries executed in this session. */
|
|
41
|
+
private _queryCount;
|
|
42
|
+
constructor(clientOptions: Readonly<ClientOptions>, executor: IExecutor, sessionOptions?: SessionOptions);
|
|
43
|
+
/** Current session ID (null until the first query completes). */
|
|
44
|
+
get sessionId(): string | null;
|
|
45
|
+
/** Number of queries executed so far. */
|
|
46
|
+
get queryCount(): number;
|
|
47
|
+
/**
|
|
48
|
+
* Send a query within this session.
|
|
49
|
+
*
|
|
50
|
+
* The first query creates the session; subsequent queries resume it.
|
|
51
|
+
*/
|
|
52
|
+
query(prompt: string, options?: QueryOptions): Promise<QueryResult>;
|
|
53
|
+
/**
|
|
54
|
+
* Send a query with streaming response within this session.
|
|
55
|
+
*/
|
|
56
|
+
stream(prompt: string, options?: QueryOptions): AsyncIterable<StreamEvent>;
|
|
57
|
+
/**
|
|
58
|
+
* Abort the current running query in this session.
|
|
59
|
+
*/
|
|
60
|
+
abort(): void;
|
|
61
|
+
private buildSessionArgs;
|
|
62
|
+
private updateSessionState;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/client/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAI1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0B;IACxD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAY;IACrC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAEhD,6DAA6D;IAC7D,OAAO,CAAC,UAAU,CAAgB;IAElC,kDAAkD;IAClD,OAAO,CAAC,WAAW,CAAK;gBAGtB,aAAa,EAAE,QAAQ,CAAC,aAAa,CAAC,EACtC,QAAQ,EAAE,SAAS,EACnB,cAAc,GAAE,cAAmB;IAQrC,iEAAiE;IACjE,IAAI,SAAS,IAAI,MAAM,GAAG,IAAI,CAE7B;IAED,yCAAyC;IACzC,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;;;OAIG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC;IAqBzE;;OAEG;IACI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,aAAa,CAAC,WAAW,CAAC;IAwBjF;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb,OAAO,CAAC,gBAAgB;IAkBxB,OAAO,CAAC,kBAAkB;CAM3B"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { buildArgs, mergeOptions, resolveEnv } from '../builder/args-builder.js';
|
|
2
|
+
import { validateQueryOptions, validatePrompt } from '../utils/validation.js';
|
|
3
|
+
/**
|
|
4
|
+
* A stateful conversation session.
|
|
5
|
+
*
|
|
6
|
+
* Sessions wrap the Claude Code `--continue` / `--resume` mechanism to
|
|
7
|
+
* provide multi-turn conversations with persistent context.
|
|
8
|
+
*
|
|
9
|
+
* ## Lifecycle
|
|
10
|
+
*
|
|
11
|
+
* 1. **First query**: runs normally, captures the `sessionId` from the result.
|
|
12
|
+
* 2. **Subsequent queries**: automatically pass `--resume <sessionId>` to continue
|
|
13
|
+
* the conversation with full context.
|
|
14
|
+
*
|
|
15
|
+
* ## Concurrency
|
|
16
|
+
*
|
|
17
|
+
* Sessions are NOT safe for concurrent queries. Each query must complete before
|
|
18
|
+
* the next one starts. For parallel work, use separate sessions or `claude.parallel()`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const session = claude.session()
|
|
23
|
+
*
|
|
24
|
+
* const r1 = await session.query('What files are in src/?')
|
|
25
|
+
* // Claude reads the directory
|
|
26
|
+
*
|
|
27
|
+
* const r2 = await session.query('Refactor the largest file')
|
|
28
|
+
* // Claude remembers the previous context
|
|
29
|
+
*
|
|
30
|
+
* console.log(session.sessionId) // 'abc-123-...'
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class Session {
|
|
34
|
+
clientOptions;
|
|
35
|
+
executor;
|
|
36
|
+
sessionOptions;
|
|
37
|
+
/** Session ID, populated after the first query completes. */
|
|
38
|
+
_sessionId;
|
|
39
|
+
/** Number of queries executed in this session. */
|
|
40
|
+
_queryCount = 0;
|
|
41
|
+
constructor(clientOptions, executor, sessionOptions = {}) {
|
|
42
|
+
this.clientOptions = clientOptions;
|
|
43
|
+
this.executor = executor;
|
|
44
|
+
this.sessionOptions = sessionOptions;
|
|
45
|
+
this._sessionId = sessionOptions.resume ?? null;
|
|
46
|
+
}
|
|
47
|
+
/** Current session ID (null until the first query completes). */
|
|
48
|
+
get sessionId() {
|
|
49
|
+
return this._sessionId;
|
|
50
|
+
}
|
|
51
|
+
/** Number of queries executed so far. */
|
|
52
|
+
get queryCount() {
|
|
53
|
+
return this._queryCount;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Send a query within this session.
|
|
57
|
+
*
|
|
58
|
+
* The first query creates the session; subsequent queries resume it.
|
|
59
|
+
*/
|
|
60
|
+
async query(prompt, options) {
|
|
61
|
+
validatePrompt(prompt);
|
|
62
|
+
if (options)
|
|
63
|
+
validateQueryOptions(options);
|
|
64
|
+
const args = this.buildSessionArgs(prompt, 'json', options);
|
|
65
|
+
const env = resolveEnv(this.clientOptions, options);
|
|
66
|
+
const resolved = mergeOptions(this.clientOptions, options, {
|
|
67
|
+
prompt,
|
|
68
|
+
outputFormat: 'json',
|
|
69
|
+
});
|
|
70
|
+
const result = await this.executor.execute(args, {
|
|
71
|
+
cwd: resolved.cwd,
|
|
72
|
+
env,
|
|
73
|
+
input: options?.input,
|
|
74
|
+
});
|
|
75
|
+
this.updateSessionState(result.sessionId);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Send a query with streaming response within this session.
|
|
80
|
+
*/
|
|
81
|
+
async *stream(prompt, options) {
|
|
82
|
+
validatePrompt(prompt);
|
|
83
|
+
if (options)
|
|
84
|
+
validateQueryOptions(options);
|
|
85
|
+
const args = this.buildSessionArgs(prompt, 'stream-json', options);
|
|
86
|
+
const env = resolveEnv(this.clientOptions, options);
|
|
87
|
+
const resolved = mergeOptions(this.clientOptions, options, {
|
|
88
|
+
prompt,
|
|
89
|
+
outputFormat: 'stream-json',
|
|
90
|
+
});
|
|
91
|
+
for await (const event of this.executor.stream(args, {
|
|
92
|
+
cwd: resolved.cwd,
|
|
93
|
+
env,
|
|
94
|
+
input: options?.input,
|
|
95
|
+
})) {
|
|
96
|
+
// Capture session ID from result events
|
|
97
|
+
if (event.type === 'result' && event.sessionId) {
|
|
98
|
+
this.updateSessionState(event.sessionId);
|
|
99
|
+
}
|
|
100
|
+
yield event;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Abort the current running query in this session.
|
|
105
|
+
*/
|
|
106
|
+
abort() {
|
|
107
|
+
this.executor.abort?.();
|
|
108
|
+
}
|
|
109
|
+
// ── Private helpers ───────────────────────────────────────────────
|
|
110
|
+
buildSessionArgs(prompt, outputFormat, queryOptions) {
|
|
111
|
+
const isFirstQuery = this._queryCount === 0;
|
|
112
|
+
const resolved = mergeOptions(this.clientOptions, queryOptions, {
|
|
113
|
+
prompt,
|
|
114
|
+
outputFormat,
|
|
115
|
+
sessionId: this._sessionId ?? undefined,
|
|
116
|
+
continueSession: isFirstQuery && this.sessionOptions.continue,
|
|
117
|
+
forkSession: isFirstQuery && this.sessionOptions.fork,
|
|
118
|
+
});
|
|
119
|
+
return buildArgs(resolved);
|
|
120
|
+
}
|
|
121
|
+
updateSessionState(sessionId) {
|
|
122
|
+
if (sessionId) {
|
|
123
|
+
this._sessionId = sessionId;
|
|
124
|
+
}
|
|
125
|
+
this._queryCount++;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/client/session.ts"],"names":[],"mappings":"AAG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IAED,qEAAqE;IAE7D,gBAAgB,CACtB,MAAc,EACd,YAAoC,EACpC,YAA2B;QAE3B,MAAM,YAAY,GAAG,IAAI,CAAC,WAAW,KAAK,CAAC,CAAC;QAE5C,MAAM,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,aAAa,EAAE,YAAY,EAAE;YAC9D,MAAM;YACN,YAAY;YACZ,SAAS,EAAE,IAAI,CAAC,UAAU,IAAI,SAAS;YACvC,eAAe,EAAE,YAAY,IAAI,IAAI,CAAC,cAAc,CAAC,QAAQ;YAC7D,WAAW,EAAE,YAAY,IAAI,IAAI,CAAC,cAAc,CAAC,IAAI;SACtD,CAAC,CAAC;QAEH,OAAO,SAAS,CAAC,QAAQ,CAAC,CAAC;IAC7B,CAAC;IAEO,kBAAkB,CAAC,SAAiB;QAC1C,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC9B,CAAC;QACD,IAAI,CAAC,WAAW,EAAE,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all claude-connector errors.
|
|
3
|
+
*
|
|
4
|
+
* Consumers can catch this to handle any library error uniformly:
|
|
5
|
+
* ```ts
|
|
6
|
+
* try {
|
|
7
|
+
* await claude.query('...')
|
|
8
|
+
* } catch (e) {
|
|
9
|
+
* if (e instanceof ClaudeConnectorError) { ... }
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class ClaudeConnectorError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown when the Claude Code CLI binary cannot be found at the specified path.
|
|
18
|
+
*/
|
|
19
|
+
export declare class CliNotFoundError extends ClaudeConnectorError {
|
|
20
|
+
readonly executable: string;
|
|
21
|
+
constructor(executable: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when the CLI process exits with a non-zero code.
|
|
25
|
+
*/
|
|
26
|
+
export declare class CliExecutionError extends ClaudeConnectorError {
|
|
27
|
+
readonly exitCode: number;
|
|
28
|
+
readonly stderr: string;
|
|
29
|
+
constructor(message: string, exitCode: number, stderr: string);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Thrown when the CLI process exceeds the configured timeout.
|
|
33
|
+
*/
|
|
34
|
+
export declare class CliTimeoutError extends ClaudeConnectorError {
|
|
35
|
+
readonly timeoutMs: number;
|
|
36
|
+
constructor(timeoutMs: number);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Thrown when CLI output cannot be parsed (unexpected format).
|
|
40
|
+
*/
|
|
41
|
+
export declare class ParseError extends ClaudeConnectorError {
|
|
42
|
+
readonly rawOutput: string;
|
|
43
|
+
constructor(message: string, rawOutput: string);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Thrown when invalid options are provided to the client.
|
|
47
|
+
*/
|
|
48
|
+
export declare class ValidationError extends ClaudeConnectorError {
|
|
49
|
+
readonly field: string;
|
|
50
|
+
constructor(field: string, message: string);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,oBAAoB;IACxD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;gBAEhB,UAAU,EAAE,MAAM;CAS/B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,oBAAoB;IACzD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAM9D;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,oBAAoB;IACvD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;gBAEf,SAAS,EAAE,MAAM;CAK9B;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,oBAAoB;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;gBAEf,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAK/C;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,oBAAoB;IACvD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;gBAEX,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK3C"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all claude-connector errors.
|
|
3
|
+
*
|
|
4
|
+
* Consumers can catch this to handle any library error uniformly:
|
|
5
|
+
* ```ts
|
|
6
|
+
* try {
|
|
7
|
+
* await claude.query('...')
|
|
8
|
+
* } catch (e) {
|
|
9
|
+
* if (e instanceof ClaudeConnectorError) { ... }
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export class ClaudeConnectorError extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'ClaudeConnectorError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Thrown when the Claude Code CLI binary cannot be found at the specified path.
|
|
21
|
+
*/
|
|
22
|
+
export class CliNotFoundError extends ClaudeConnectorError {
|
|
23
|
+
executable;
|
|
24
|
+
constructor(executable) {
|
|
25
|
+
super(`Claude Code CLI not found at '${executable}'. ` +
|
|
26
|
+
`Ensure it is installed and the path is correct. ` +
|
|
27
|
+
`Install: https://docs.anthropic.com/en/docs/claude-code/overview`);
|
|
28
|
+
this.name = 'CliNotFoundError';
|
|
29
|
+
this.executable = executable;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Thrown when the CLI process exits with a non-zero code.
|
|
34
|
+
*/
|
|
35
|
+
export class CliExecutionError extends ClaudeConnectorError {
|
|
36
|
+
exitCode;
|
|
37
|
+
stderr;
|
|
38
|
+
constructor(message, exitCode, stderr) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = 'CliExecutionError';
|
|
41
|
+
this.exitCode = exitCode;
|
|
42
|
+
this.stderr = stderr;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Thrown when the CLI process exceeds the configured timeout.
|
|
47
|
+
*/
|
|
48
|
+
export class CliTimeoutError extends ClaudeConnectorError {
|
|
49
|
+
timeoutMs;
|
|
50
|
+
constructor(timeoutMs) {
|
|
51
|
+
super(`Claude Code CLI timed out after ${timeoutMs}ms`);
|
|
52
|
+
this.name = 'CliTimeoutError';
|
|
53
|
+
this.timeoutMs = timeoutMs;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Thrown when CLI output cannot be parsed (unexpected format).
|
|
58
|
+
*/
|
|
59
|
+
export class ParseError extends ClaudeConnectorError {
|
|
60
|
+
rawOutput;
|
|
61
|
+
constructor(message, rawOutput) {
|
|
62
|
+
super(message);
|
|
63
|
+
this.name = 'ParseError';
|
|
64
|
+
this.rawOutput = rawOutput;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Thrown when invalid options are provided to the client.
|
|
69
|
+
*/
|
|
70
|
+
export class ValidationError extends ClaudeConnectorError {
|
|
71
|
+
field;
|
|
72
|
+
constructor(field, message) {
|
|
73
|
+
super(`Invalid option '${field}': ${message}`);
|
|
74
|
+
this.name = 'ValidationError';
|
|
75
|
+
this.field = field;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|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