@graphorin/provider-llamacpp-node 0.6.0 → 0.7.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/CHANGELOG.md +30 -0
- package/README.md +1 -1
- package/dist/adapter.d.ts +13 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +115 -12
- package/dist/adapter.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/package.js +6 -0
- package/dist/package.js.map +1 -0
- package/dist/runtime.d.ts +27 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js.map +1 -1
- package/package.json +7 -5
- package/src/adapter.ts +543 -0
- package/src/counter.ts +79 -0
- package/src/index.ts +38 -0
- package/src/runtime.ts +202 -0
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loose structural shapes covering the slice of `node-llama-cpp` we
|
|
3
|
+
* use. Re-declared here to avoid pulling the heavy native peer at
|
|
4
|
+
* type-check time.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `Llama` engine instance (returned by `getLlama()`).
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export interface LlamaInstance {
|
|
15
|
+
loadModel(args: { modelPath: string; gpuLayers?: number | 'auto' }): Promise<LlamaModelInstance>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Loaded GGUF model.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export interface LlamaModelInstance {
|
|
24
|
+
readonly trainContextSize?: number;
|
|
25
|
+
tokenize(text: string): readonly number[] | Uint32Array | Uint8Array;
|
|
26
|
+
createContext(args?: { contextSize?: number }): Promise<{
|
|
27
|
+
getSequence(): { dispose?: () => void };
|
|
28
|
+
dispose?: () => void;
|
|
29
|
+
}>;
|
|
30
|
+
dispose?(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Structural mirror of node-llama-cpp v3's `ChatHistoryItem` (W-096).
|
|
35
|
+
* A `'model'` turn carries its text as `response: string[]`.
|
|
36
|
+
*
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export type LlamaChatHistoryItem =
|
|
40
|
+
| { readonly type: 'system'; readonly text: string }
|
|
41
|
+
| { readonly type: 'user'; readonly text: string }
|
|
42
|
+
| { readonly type: 'model'; readonly response: ReadonlyArray<string> };
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Loaded chat session capable of streaming responses.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export interface LlamaSessionInstance {
|
|
50
|
+
promptStreamingResponse(
|
|
51
|
+
prompt: string,
|
|
52
|
+
options?: {
|
|
53
|
+
readonly signal?: AbortSignal;
|
|
54
|
+
readonly maxTokens?: number;
|
|
55
|
+
readonly temperature?: number;
|
|
56
|
+
},
|
|
57
|
+
): AsyncIterable<string>;
|
|
58
|
+
/**
|
|
59
|
+
* W-096: replace the session's chat history (node-llama-cpp v3
|
|
60
|
+
* `setChatHistory`). When present, the adapter feeds multi-turn
|
|
61
|
+
* transcripts as REAL chat history + prompts only the last user turn
|
|
62
|
+
* - instead of serialising the whole conversation into one
|
|
63
|
+
* pseudo-prompt string. Optional: fixtures / custom factories without
|
|
64
|
+
* it keep the legacy render-prompt path.
|
|
65
|
+
*/
|
|
66
|
+
setChatHistory?(history: ReadonlyArray<LlamaChatHistoryItem>): void;
|
|
67
|
+
/**
|
|
68
|
+
* Release the per-request context / sequence backing this session.
|
|
69
|
+
* node-llama-cpp contexts hold KV-cache memory (hundreds of MB at
|
|
70
|
+
* large context sizes); the adapter calls this in a `finally` after
|
|
71
|
+
* every stream so long-running agents do not leak until OOM.
|
|
72
|
+
*/
|
|
73
|
+
dispose?(): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Test-only shape for injecting fixture-driven runtime behaviour.
|
|
78
|
+
*
|
|
79
|
+
* @stable
|
|
80
|
+
*/
|
|
81
|
+
export interface LlamaCppNodeRuntimeOverrides {
|
|
82
|
+
/** Returns a `LlamaInstance` (the result of `getLlama()`). */
|
|
83
|
+
readonly getLlama?: () => Promise<LlamaInstance>;
|
|
84
|
+
/**
|
|
85
|
+
* Build a streaming chat session against an already-loaded model
|
|
86
|
+
* instance. Used by the adapter to wire `model.tokenize` and
|
|
87
|
+
* `session.promptStreamingResponse` to the per-test fixture.
|
|
88
|
+
*/
|
|
89
|
+
readonly createSession?: (
|
|
90
|
+
model: LlamaModelInstance,
|
|
91
|
+
system?: string,
|
|
92
|
+
) => Promise<LlamaSessionInstance>;
|
|
93
|
+
/**
|
|
94
|
+
* Override the `LlamaChatSession` constructor used by the REAL
|
|
95
|
+
* default session factory (PS-3). Tests stub it; production loads it
|
|
96
|
+
* from the `node-llama-cpp` peer.
|
|
97
|
+
*/
|
|
98
|
+
readonly LlamaChatSession?: LlamaChatSessionCtor;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Structural slice of the peer's `LlamaChatSession` class used by the
|
|
103
|
+
* default session factory (PS-3): `prompt(text, { onTextChunk })`
|
|
104
|
+
* resolves with the full response while streaming chunks through the
|
|
105
|
+
* callback.
|
|
106
|
+
*
|
|
107
|
+
* @internal
|
|
108
|
+
*/
|
|
109
|
+
export interface LlamaChatSessionPeer {
|
|
110
|
+
prompt(
|
|
111
|
+
text: string,
|
|
112
|
+
options?: {
|
|
113
|
+
readonly signal?: AbortSignal;
|
|
114
|
+
readonly maxTokens?: number;
|
|
115
|
+
readonly temperature?: number;
|
|
116
|
+
readonly onTextChunk?: (chunk: string) => void;
|
|
117
|
+
},
|
|
118
|
+
): Promise<string>;
|
|
119
|
+
/** W-096: node-llama-cpp v3 chat-history setter (optional slice). */
|
|
120
|
+
setChatHistory?(history: ReadonlyArray<LlamaChatHistoryItem>): void;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @internal */
|
|
124
|
+
export type LlamaChatSessionCtor = new (args: {
|
|
125
|
+
readonly contextSequence: unknown;
|
|
126
|
+
readonly systemPrompt?: string;
|
|
127
|
+
}) => LlamaChatSessionPeer;
|
|
128
|
+
|
|
129
|
+
let cachedLlama: LlamaInstance | null = null;
|
|
130
|
+
let cachedChatSessionCtor: LlamaChatSessionCtor | null = null;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Lazily load the `node-llama-cpp` peer and return the `Llama` engine
|
|
134
|
+
* instance. Cached per process.
|
|
135
|
+
*
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
export async function loadLlamaModule(
|
|
139
|
+
overrides: LlamaCppNodeRuntimeOverrides | undefined,
|
|
140
|
+
): Promise<LlamaInstance> {
|
|
141
|
+
if (overrides?.getLlama !== undefined) return overrides.getLlama();
|
|
142
|
+
if (cachedLlama !== null) return cachedLlama;
|
|
143
|
+
let mod: { getLlama?: unknown };
|
|
144
|
+
try {
|
|
145
|
+
mod = (await import('node-llama-cpp')) as { getLlama?: unknown };
|
|
146
|
+
} catch (cause) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
"[graphorin/provider-llamacpp-node] missing peer dependency 'node-llama-cpp'. " +
|
|
149
|
+
'Install it with `pnpm add node-llama-cpp`.',
|
|
150
|
+
{ cause },
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (typeof mod.getLlama !== 'function') {
|
|
154
|
+
throw new Error(
|
|
155
|
+
'[graphorin/provider-llamacpp-node] installed node-llama-cpp does not expose getLlama().',
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
const instance = (await (mod.getLlama as () => Promise<LlamaInstance>)()) as LlamaInstance;
|
|
159
|
+
cachedLlama = instance;
|
|
160
|
+
return instance;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Lazily resolve the peer's `LlamaChatSession` constructor for the
|
|
165
|
+
* real default session factory (PS-3). Cached per process; the
|
|
166
|
+
* override wins for tests.
|
|
167
|
+
*
|
|
168
|
+
* @internal
|
|
169
|
+
*/
|
|
170
|
+
export async function loadLlamaChatSessionCtor(
|
|
171
|
+
overrides: LlamaCppNodeRuntimeOverrides | undefined,
|
|
172
|
+
): Promise<LlamaChatSessionCtor> {
|
|
173
|
+
if (overrides?.LlamaChatSession !== undefined) return overrides.LlamaChatSession;
|
|
174
|
+
if (cachedChatSessionCtor !== null) return cachedChatSessionCtor;
|
|
175
|
+
let mod: { LlamaChatSession?: unknown };
|
|
176
|
+
try {
|
|
177
|
+
mod = (await import('node-llama-cpp')) as { LlamaChatSession?: unknown };
|
|
178
|
+
} catch (cause) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
"[graphorin/provider-llamacpp-node] missing peer dependency 'node-llama-cpp'. " +
|
|
181
|
+
'Install it with `pnpm add node-llama-cpp`.',
|
|
182
|
+
{ cause },
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (typeof mod.LlamaChatSession !== 'function') {
|
|
186
|
+
throw new Error(
|
|
187
|
+
'[graphorin/provider-llamacpp-node] installed node-llama-cpp does not expose LlamaChatSession.',
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
cachedChatSessionCtor = mod.LlamaChatSession as LlamaChatSessionCtor;
|
|
191
|
+
return cachedChatSessionCtor;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Test-only hook that resets the cached `getLlama()` result.
|
|
196
|
+
*
|
|
197
|
+
* @internal
|
|
198
|
+
*/
|
|
199
|
+
export function __resetLlamaCache(): void {
|
|
200
|
+
cachedLlama = null;
|
|
201
|
+
cachedChatSessionCtor = null;
|
|
202
|
+
}
|