@kybernesis/brain-llm-claude 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/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kybernesis/brain-llm-claude
|
|
3
|
+
*
|
|
4
|
+
* Subprocess-based LLM provider wrapping the `claude` CLI.
|
|
5
|
+
*
|
|
6
|
+
* Port of cortex-appydave cortex-provider-llm-claude-code, adapted to
|
|
7
|
+
* the brain-core LLMProvider interface (ADR-0009).
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { createSubprocessProvider } from '@kybernesis/brain-llm-claude';
|
|
11
|
+
* import { setLLMProvider } from '@kybernesis/brain-core';
|
|
12
|
+
* setLLMProvider(createSubprocessProvider());
|
|
13
|
+
*/
|
|
14
|
+
export interface ClaudeCallOpts {
|
|
15
|
+
model?: string;
|
|
16
|
+
maxTokens?: number;
|
|
17
|
+
system?: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
export interface LLMProvider {
|
|
21
|
+
call(prompt: string, opts?: ClaudeCallOpts): Promise<string | null>;
|
|
22
|
+
}
|
|
23
|
+
export interface SubprocessProviderOptions {
|
|
24
|
+
/** Claude CLI binary name or path. Defaults to 'claude'. */
|
|
25
|
+
binary?: string;
|
|
26
|
+
/** Working directory for the spawned subprocess. */
|
|
27
|
+
cwd?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a subprocess-backed LLMProvider.
|
|
31
|
+
*
|
|
32
|
+
* Spawns `claude --print - --dangerously-skip-permissions --model <id>`
|
|
33
|
+
* and pipes the prompt via stdin. Returns trimmed stdout on exit 0,
|
|
34
|
+
* null on any failure (spawn error, non-zero exit, empty output).
|
|
35
|
+
*/
|
|
36
|
+
export declare function createSubprocessProvider(opts?: SubprocessProviderOptions): LLMProvider;
|
|
37
|
+
/** Returns true when the claude binary is reachable on PATH. */
|
|
38
|
+
export declare function isClaudeAvailable(binary?: string): Promise<boolean>;
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,MAAM,WAAW,cAAc;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CACrE;AAED,MAAM,WAAW,yBAAyB;IACxC,4DAA4D;IAC5D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,oDAAoD;IACpD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAmBD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,GAAE,yBAA8B,GAAG,WAAW,CAsC1F;AAED,gEAAgE;AAChE,wBAAsB,iBAAiB,CAAC,MAAM,SAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAM3E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kybernesis/brain-llm-claude
|
|
3
|
+
*
|
|
4
|
+
* Subprocess-based LLM provider wrapping the `claude` CLI.
|
|
5
|
+
*
|
|
6
|
+
* Port of cortex-appydave cortex-provider-llm-claude-code, adapted to
|
|
7
|
+
* the brain-core LLMProvider interface (ADR-0009).
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { createSubprocessProvider } from '@kybernesis/brain-llm-claude';
|
|
11
|
+
* import { setLLMProvider } from '@kybernesis/brain-core';
|
|
12
|
+
* setLLMProvider(createSubprocessProvider());
|
|
13
|
+
*/
|
|
14
|
+
import { spawn } from 'node:child_process';
|
|
15
|
+
const MODEL_ALIASES = {
|
|
16
|
+
haiku: 'claude-haiku-4-5-20251001',
|
|
17
|
+
haiku4: 'claude-haiku-4-5-20251001',
|
|
18
|
+
'haiku-4': 'claude-haiku-4-5-20251001',
|
|
19
|
+
sonnet: 'claude-sonnet-4-6',
|
|
20
|
+
sonnet4: 'claude-sonnet-4-6',
|
|
21
|
+
'sonnet-4-6': 'claude-sonnet-4-6',
|
|
22
|
+
opus: 'claude-opus-4-7',
|
|
23
|
+
opus4: 'claude-opus-4-7',
|
|
24
|
+
'opus-4-7': 'claude-opus-4-7',
|
|
25
|
+
};
|
|
26
|
+
function resolveModel(raw) {
|
|
27
|
+
if (!raw)
|
|
28
|
+
return 'claude-haiku-4-5-20251001';
|
|
29
|
+
return MODEL_ALIASES[raw.toLowerCase()] ?? raw;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a subprocess-backed LLMProvider.
|
|
33
|
+
*
|
|
34
|
+
* Spawns `claude --print - --dangerously-skip-permissions --model <id>`
|
|
35
|
+
* and pipes the prompt via stdin. Returns trimmed stdout on exit 0,
|
|
36
|
+
* null on any failure (spawn error, non-zero exit, empty output).
|
|
37
|
+
*/
|
|
38
|
+
export function createSubprocessProvider(opts = {}) {
|
|
39
|
+
const binary = opts.binary ?? 'claude';
|
|
40
|
+
const cwd = opts.cwd;
|
|
41
|
+
return {
|
|
42
|
+
call(prompt, callOpts = {}) {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const model = resolveModel(callOpts.model);
|
|
45
|
+
const args = ['--print', '-', '--dangerously-skip-permissions', '--model', model];
|
|
46
|
+
if (callOpts.system) {
|
|
47
|
+
args.push('--system-prompt', String(callOpts.system));
|
|
48
|
+
}
|
|
49
|
+
const proc = spawn(binary, args, {
|
|
50
|
+
env: { ...process.env, CLAUDECODE: '', CLAUDE_CODE_ENTRYPOINT: '' },
|
|
51
|
+
cwd,
|
|
52
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
53
|
+
});
|
|
54
|
+
const out = [];
|
|
55
|
+
const err = [];
|
|
56
|
+
if (proc.stdin) {
|
|
57
|
+
proc.stdin.write(prompt);
|
|
58
|
+
proc.stdin.end();
|
|
59
|
+
}
|
|
60
|
+
if (proc.stdout)
|
|
61
|
+
proc.stdout.on('data', (c) => out.push(c));
|
|
62
|
+
if (proc.stderr)
|
|
63
|
+
proc.stderr.on('data', (c) => err.push(c));
|
|
64
|
+
proc.on('error', () => { out.length = 0; err.length = 0; resolve(null); });
|
|
65
|
+
proc.on('close', (code) => {
|
|
66
|
+
const text = Buffer.concat(out).toString().trim();
|
|
67
|
+
out.length = 0;
|
|
68
|
+
err.length = 0;
|
|
69
|
+
resolve(code === 0 && text ? text : null);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Returns true when the claude binary is reachable on PATH. */
|
|
76
|
+
export async function isClaudeAvailable(binary = 'claude') {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
const proc = spawn(binary, ['--version'], { stdio: 'ignore' });
|
|
79
|
+
proc.on('error', () => resolve(false));
|
|
80
|
+
proc.on('close', (code) => resolve(code === 0));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAoB3C,MAAM,aAAa,GAA2B;IAC5C,KAAK,EAAW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QACL,CAAC;KACF,CAAC;AACJ,CAAC;AAED,gEAAgE;AAChE,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,MAAM,GAAG,QAAQ;IACvD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC/D,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACvC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kybernesis/brain-llm-claude",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude subprocess LLM provider for brain-core",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "David Cruwys (AppyDave)",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/KybernesisAI/cortex.git",
|
|
10
|
+
"directory": "packages/brain-llm-claude"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/KybernesisAI/cortex/tree/main/packages/brain-llm-claude#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/KybernesisAI/cortex/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@kybernesis/brain-contracts": "0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -b",
|
|
37
|
+
"clean": "tsc -b --clean",
|
|
38
|
+
"typecheck": "tsc -b"
|
|
39
|
+
}
|
|
40
|
+
}
|