@aphexcms/ai-ollama 0.0.0-stage → 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/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Benjamin Sinidol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AIProviderAdapter, AIChatRequest, AIStreamEvent } from '@aphexcms/cms-core/server';
|
|
2
|
+
export interface OllamaAdapterConfig {
|
|
3
|
+
/** Defaults to Ollama's standard local port. */
|
|
4
|
+
host?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class OllamaProviderAdapter implements AIProviderAdapter {
|
|
7
|
+
readonly name = "ollama";
|
|
8
|
+
private client;
|
|
9
|
+
constructor(config?: OllamaAdapterConfig);
|
|
10
|
+
chatStream(request: AIChatRequest): AsyncIterable<AIStreamEvent>;
|
|
11
|
+
}
|
|
12
|
+
export declare function createOllamaAdapter(config?: OllamaAdapterConfig): AIProviderAdapter;
|
|
13
|
+
export type { AIProviderAdapter, AIChatRequest, AIStreamEvent, AIMessage, AIToolSpec, AIToolCall } from '@aphexcms/cms-core/server';
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,iBAAiB,EAEjB,aAAa,EACb,aAAa,EAIb,MAAM,2BAA2B,CAAC;AAEnC,MAAM,WAAW,mBAAmB;IACnC,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;AA0CD,qBAAa,qBAAsB,YAAW,iBAAiB;IAC9D,QAAQ,CAAC,IAAI,YAAY;IACzB,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,GAAE,mBAAwB;IAIrC,UAAU,CAAC,OAAO,EAAE,aAAa,GAAG,aAAa,CAAC,aAAa,CAAC;CAoDvE;AAED,wBAAgB,mBAAmB,CAAC,MAAM,GAAE,mBAAwB,GAAG,iBAAiB,CAEvF;AAED,YAAY,EACX,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,SAAS,EACT,UAAU,EACV,UAAU,EACV,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Ollama } from 'ollama';
|
|
2
|
+
function toOllamaMessages(messages) {
|
|
3
|
+
return messages.map((m) => {
|
|
4
|
+
if (m.role === 'tool') {
|
|
5
|
+
// Ollama has no distinct tool-result role — the content goes back as a plain
|
|
6
|
+
// message; there's no tool_call_id linkage in its wire format.
|
|
7
|
+
return { role: 'tool', content: m.content };
|
|
8
|
+
}
|
|
9
|
+
if (m.role === 'assistant' && m.toolCalls?.length) {
|
|
10
|
+
return {
|
|
11
|
+
role: 'assistant',
|
|
12
|
+
content: m.content,
|
|
13
|
+
tool_calls: m.toolCalls.map((tc) => ({
|
|
14
|
+
function: { name: tc.name, arguments: tc.arguments }
|
|
15
|
+
}))
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return { role: m.role, content: m.content };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function toOllamaTool(tool) {
|
|
22
|
+
return {
|
|
23
|
+
type: 'function',
|
|
24
|
+
function: {
|
|
25
|
+
name: tool.name,
|
|
26
|
+
description: tool.description,
|
|
27
|
+
parameters: tool.parameters
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function mapDoneReason(reason) {
|
|
32
|
+
switch (reason) {
|
|
33
|
+
case 'length':
|
|
34
|
+
return 'length';
|
|
35
|
+
default:
|
|
36
|
+
return 'stop';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class OllamaProviderAdapter {
|
|
40
|
+
name = 'ollama';
|
|
41
|
+
client;
|
|
42
|
+
constructor(config = {}) {
|
|
43
|
+
this.client = new Ollama(config.host ? { host: config.host } : undefined);
|
|
44
|
+
}
|
|
45
|
+
async *chatStream(request) {
|
|
46
|
+
let finishReason = 'stop';
|
|
47
|
+
let sawToolCalls = false;
|
|
48
|
+
try {
|
|
49
|
+
const response = await this.client.chat({
|
|
50
|
+
model: request.model,
|
|
51
|
+
messages: toOllamaMessages(request.messages),
|
|
52
|
+
...(request.tools?.length ? { tools: request.tools.map(toOllamaTool) } : {}),
|
|
53
|
+
stream: true
|
|
54
|
+
});
|
|
55
|
+
// Ollama's abort signal is a controller passed at the client level, not a
|
|
56
|
+
// per-request option — bridge the caller's AbortSignal to it.
|
|
57
|
+
request.signal?.addEventListener('abort', () => this.client.abort());
|
|
58
|
+
for await (const chunk of response) {
|
|
59
|
+
if (chunk.message?.content) {
|
|
60
|
+
yield { type: 'text', delta: chunk.message.content };
|
|
61
|
+
}
|
|
62
|
+
// Unlike OpenAI, Ollama's local-model tool calls arrive fully formed (arguments
|
|
63
|
+
// already a parsed object, not a streamed partial-JSON string), so each one is
|
|
64
|
+
// emitted directly rather than buffered across chunks.
|
|
65
|
+
if (chunk.message?.tool_calls?.length) {
|
|
66
|
+
sawToolCalls = true;
|
|
67
|
+
for (const tc of chunk.message.tool_calls) {
|
|
68
|
+
const toolCall = {
|
|
69
|
+
id: crypto.randomUUID(),
|
|
70
|
+
name: tc.function.name,
|
|
71
|
+
arguments: tc.function.arguments
|
|
72
|
+
};
|
|
73
|
+
yield { type: 'toolCall', toolCall };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (chunk.done) {
|
|
77
|
+
finishReason = sawToolCalls ? 'tool_calls' : mapDoneReason(chunk.done_reason);
|
|
78
|
+
yield {
|
|
79
|
+
type: 'usage',
|
|
80
|
+
promptTokens: chunk.prompt_eval_count ?? 0,
|
|
81
|
+
completionTokens: chunk.eval_count ?? 0
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
yield { type: 'done', finishReason };
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
yield { type: 'error', message: err instanceof Error ? err.message : String(err) };
|
|
89
|
+
yield { type: 'done', finishReason: 'error' };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function createOllamaAdapter(config = {}) {
|
|
94
|
+
return new OllamaProviderAdapter(config);
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 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mBAAmB,CAAC,SAA8B,EAAE;IACnE,OAAO,IAAI,qBAAqB,CAAC,MAAM,CAAC,CAAC;AAC1C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aphexcms/ai-ollama",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"provenance": true
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/IcelandicIcecream/aphex.git",
|
|
12
|
+
"directory": "packages/ai-ollama"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"ollama": "^0.6.3"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@aphexcms/cms-core": ">=11.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"@aphexcms/cms-core": "^11.3.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc",
|
|
39
|
+
"dev:local": "node ../../scripts/swap-package-paths.js ./package.json src"
|
|
40
|
+
}
|
|
6
41
|
}
|
package/README.md
DELETED