@axium/ai 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.md +157 -0
- package/README.md +2 -2
- package/db.json +114 -0
- package/dist/client/cli.d.ts +1 -0
- package/dist/client/cli.js +224 -0
- package/dist/client/config.d.ts +196 -0
- package/dist/client/config.js +55 -0
- package/dist/client/hooks.d.ts +3 -0
- package/dist/client/hooks.js +32 -0
- package/dist/client/local.d.ts +17 -0
- package/dist/client/local.js +112 -0
- package/dist/common.d.ts +1318 -0
- package/dist/common.js +569 -0
- package/dist/hooks.d.ts +13 -0
- package/dist/hooks.js +34 -0
- package/dist/hub.d.ts +4 -0
- package/dist/hub.js +75 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/integrations/notes.d.ts +1 -0
- package/dist/integrations/notes.js +104 -0
- package/dist/integrations/tasks.d.ts +1 -0
- package/dist/integrations/tasks.js +157 -0
- package/dist/loop.d.ts +75 -0
- package/dist/loop.js +239 -0
- package/dist/memory.d.ts +107 -0
- package/dist/memory.js +193 -0
- package/dist/node/chat.d.ts +50 -0
- package/dist/node/chat.js +60 -0
- package/dist/node/llama.d.ts +115 -0
- package/dist/node/llama.js +385 -0
- package/dist/node/markdown.d.ts +6 -0
- package/dist/node/markdown.js +285 -0
- package/dist/node/models.d.ts +34 -0
- package/dist/node/models.js +287 -0
- package/dist/node/web.d.ts +1 -0
- package/dist/node/web.js +203 -0
- package/dist/server/api/conversations.d.ts +1 -0
- package/dist/server/api/conversations.js +216 -0
- package/dist/server/api/gateway.d.ts +1 -0
- package/dist/server/api/gateway.js +113 -0
- package/dist/server/api/memories.d.ts +1 -0
- package/dist/server/api/memories.js +129 -0
- package/dist/server/api/models.d.ts +1 -0
- package/dist/server/api/models.js +161 -0
- package/dist/server/api/temporary.d.ts +1 -0
- package/dist/server/api/temporary.js +50 -0
- package/dist/server/api/timers.d.ts +1 -0
- package/dist/server/api/timers.js +27 -0
- package/dist/server/api/voice.d.ts +1 -0
- package/dist/server/api/voice.js +188 -0
- package/dist/server/generation.d.ts +38 -0
- package/dist/server/generation.js +299 -0
- package/dist/server/memory.d.ts +7 -0
- package/dist/server/memory.js +218 -0
- package/dist/server/models.d.ts +13 -0
- package/dist/server/models.js +59 -0
- package/dist/server/preferences.d.ts +3 -0
- package/dist/server/preferences.js +12 -0
- package/dist/server/timers.d.ts +7 -0
- package/dist/server/timers.js +66 -0
- package/dist/speech.d.ts +17 -0
- package/dist/speech.js +69 -0
- package/dist/timers.d.ts +1 -0
- package/dist/timers.js +67 -0
- package/dist/tools.d.ts +70 -0
- package/dist/tools.js +84 -0
- package/lib/ChatMessage.svelte +331 -0
- package/lib/Composer.svelte +234 -0
- package/lib/Conversation.svelte +433 -0
- package/lib/ConversationMenu.svelte +18 -0
- package/lib/ModelActions.svelte +71 -0
- package/lib/ModelOptions.svelte +101 -0
- package/lib/ModelStatus.svelte +49 -0
- package/lib/ToolCall.svelte +171 -0
- package/lib/VoiceMode.svelte +113 -0
- package/lib/actions.ts +21 -0
- package/lib/index.ts +7 -0
- package/lib/live.svelte.ts +113 -0
- package/lib/markdown.ts +40 -0
- package/lib/preferences.ts +13 -0
- package/lib/transition.ts +24 -0
- package/lib/tsconfig.json +14 -0
- package/lib/voice.svelte.ts +476 -0
- package/locales/en.json +316 -0
- package/package.json +139 -5
- package/routes/ai/+layout.svelte +177 -0
- package/routes/ai/+layout.ts +26 -0
- package/routes/ai/+page.svelte +109 -0
- package/routes/ai/TimerAlerts.svelte +51 -0
- package/routes/ai/Timers.svelte +121 -0
- package/routes/ai/[id]/+page.svelte +18 -0
- package/routes/ai/[id]/+page.ts +14 -0
- package/routes/ai/memory/+page.svelte +313 -0
- package/routes/ai/memory/+page.ts +10 -0
- package/routes/ai/memory/MemoryForm.svelte +111 -0
- package/routes/ai/models/+page.svelte +199 -0
- package/routes/ai/models/+page.ts +5 -0
- package/routes/ai/models/AddModel.svelte +240 -0
- package/routes/ai/models/MemoryMeter.svelte +120 -0
- package/routes/ai/models/Roles.svelte +159 -0
- package/routes/ai/models/[id]/+page.svelte +331 -0
- package/routes/ai/models/[id]/+page.ts +6 -0
- package/routes/ai/timers.svelte.ts +94 -0
- package/routes/tsconfig.json +14 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2007 Free Software Foundation, Inc.
|
|
6
|
+
<https://fsf.org/>
|
|
7
|
+
|
|
8
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
9
|
+
license document, but changing it is not allowed.
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates the
|
|
12
|
+
terms and conditions of version 3 of the GNU General Public License,
|
|
13
|
+
supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
## 0. Additional Definitions.
|
|
16
|
+
|
|
17
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the "GNU GPL" refers to version 3 of the
|
|
19
|
+
GNU General Public License.
|
|
20
|
+
|
|
21
|
+
"The Library" refers to a covered work governed by this License, other
|
|
22
|
+
than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An "Application" is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the "Linked
|
|
32
|
+
Version".
|
|
33
|
+
|
|
34
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
## 2. Conveying Modified Versions.
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
- a) under this License, provided that you make a good faith effort
|
|
58
|
+
to ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
+
this License applicable to that copy.
|
|
63
|
+
|
|
64
|
+
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
+
|
|
66
|
+
The object code form of an Application may incorporate material from a
|
|
67
|
+
header file that is part of the Library. You may convey such object
|
|
68
|
+
code under terms of your choice, provided that, if the incorporated
|
|
69
|
+
material is not limited to numerical parameters, data structure
|
|
70
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
+
(ten or fewer lines in length), you do both of the following:
|
|
72
|
+
|
|
73
|
+
- a) Give prominent notice with each copy of the object code that
|
|
74
|
+
the Library is used in it and that the Library and its use are
|
|
75
|
+
covered by this License.
|
|
76
|
+
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
+
license document.
|
|
78
|
+
|
|
79
|
+
## 4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
+
together, effectively do not restrict modification of the portions of
|
|
83
|
+
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
+
debugging such modifications, if you also do each of the following:
|
|
85
|
+
|
|
86
|
+
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
+
the Library is used in it and that the Library and its use are
|
|
88
|
+
covered by this License.
|
|
89
|
+
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
+
license document.
|
|
91
|
+
- c) For a Combined Work that displays copyright notices during
|
|
92
|
+
execution, include the copyright notice for the Library among
|
|
93
|
+
these notices, as well as a reference directing the user to the
|
|
94
|
+
copies of the GNU GPL and this license document.
|
|
95
|
+
- d) Do one of the following:
|
|
96
|
+
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
+
this License, and the Corresponding Application Code in a form
|
|
98
|
+
suitable for, and under terms that permit, the user to
|
|
99
|
+
recombine or relink the Application with a modified version of
|
|
100
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
+
Corresponding Source.
|
|
103
|
+
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
+
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
+
time a copy of the Library already present on the user's
|
|
106
|
+
computer system, and (b) will operate properly with a modified
|
|
107
|
+
version of the Library that is interface-compatible with the
|
|
108
|
+
Linked Version.
|
|
109
|
+
- e) Provide Installation Information, but only if you would
|
|
110
|
+
otherwise be required to provide such information under section 6
|
|
111
|
+
of the GNU GPL, and only to the extent that such information is
|
|
112
|
+
necessary to install and execute a modified version of the
|
|
113
|
+
Combined Work produced by recombining or relinking the Application
|
|
114
|
+
with a modified version of the Linked Version. (If you use option
|
|
115
|
+
4d0, the Installation Information must accompany the Minimal
|
|
116
|
+
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
+
use option 4d1, you must provide the Installation Information in
|
|
118
|
+
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
+
Corresponding Source.)
|
|
120
|
+
|
|
121
|
+
## 5. Combined Libraries.
|
|
122
|
+
|
|
123
|
+
You may place library facilities that are a work based on the Library
|
|
124
|
+
side by side in a single library together with other library
|
|
125
|
+
facilities that are not Applications and are not covered by this
|
|
126
|
+
License, and convey such a combined library under terms of your
|
|
127
|
+
choice, if you do both of the following:
|
|
128
|
+
|
|
129
|
+
- a) Accompany the combined library with a copy of the same work
|
|
130
|
+
based on the Library, uncombined with any other library
|
|
131
|
+
facilities, conveyed under the terms of this License.
|
|
132
|
+
- b) Give prominent notice with the combined library that part of it
|
|
133
|
+
is a work based on the Library, and explaining where to find the
|
|
134
|
+
accompanying uncombined form of the same work.
|
|
135
|
+
|
|
136
|
+
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
+
|
|
138
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
+
versions will be similar in spirit to the present version, but may
|
|
141
|
+
differ in detail to address new problems or concerns.
|
|
142
|
+
|
|
143
|
+
Each version is given a distinguishing version number. If the Library
|
|
144
|
+
as you received it specifies that a certain numbered version of the
|
|
145
|
+
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
+
it, you have the option of following the terms and conditions either
|
|
147
|
+
of that published version or of any later version published by the
|
|
148
|
+
Free Software Foundation. If the Library as you received it does not
|
|
149
|
+
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
+
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
+
published by the Free Software Foundation.
|
|
152
|
+
|
|
153
|
+
If the Library as you received it specifies that a proxy can decide
|
|
154
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
+
permanent authorization for you to choose that version for the
|
|
157
|
+
Library.
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# AI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Axium AI is the official plugin for chatting with language models you run yourself.
|
package/db.json
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../server/schemas/db.json",
|
|
3
|
+
"format": 1,
|
|
4
|
+
"versions": [
|
|
5
|
+
{
|
|
6
|
+
"delta": false,
|
|
7
|
+
"tables": {
|
|
8
|
+
"ai_models": {
|
|
9
|
+
"columns": {
|
|
10
|
+
"id": { "type": "text", "required": true, "primary": true },
|
|
11
|
+
"name": { "type": "text", "required": true },
|
|
12
|
+
"source": { "type": "text", "required": true },
|
|
13
|
+
"size": { "type": "bigint", "required": true, "default": "0" },
|
|
14
|
+
"created": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
15
|
+
"reasoning": { "type": "text" },
|
|
16
|
+
"systemPrompt": { "type": "text" },
|
|
17
|
+
"options": { "type": "jsonb", "required": true, "default": "'{}'::jsonb" },
|
|
18
|
+
"reasoningLevels": { "type": "text[]" },
|
|
19
|
+
"templateReasoning": { "type": "text" },
|
|
20
|
+
"tools": { "type": "boolean" },
|
|
21
|
+
"learned": { "type": "timestamptz" }
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"ai_roles": {
|
|
25
|
+
"columns": {
|
|
26
|
+
"role": {
|
|
27
|
+
"type": "text",
|
|
28
|
+
"required": true,
|
|
29
|
+
"primary": true,
|
|
30
|
+
"check": "role IN ('chat', 'assistant', 'coding', 'stt', 'tts')"
|
|
31
|
+
},
|
|
32
|
+
"modelId": {
|
|
33
|
+
"type": "text",
|
|
34
|
+
"required": true,
|
|
35
|
+
"primary": true,
|
|
36
|
+
"references": "ai_models.id",
|
|
37
|
+
"onDelete": "cascade"
|
|
38
|
+
},
|
|
39
|
+
"position": { "type": "integer", "required": true }
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"ai_conversations": {
|
|
43
|
+
"columns": {
|
|
44
|
+
"id": { "type": "uuid", "required": true, "primary": true, "default": "gen_random_uuid()" },
|
|
45
|
+
"userId": { "type": "uuid", "required": true, "references": "users.id", "onDelete": "cascade" },
|
|
46
|
+
"title": { "type": "text", "required": true, "default": "''" },
|
|
47
|
+
"titleSource": {
|
|
48
|
+
"type": "text",
|
|
49
|
+
"required": true,
|
|
50
|
+
"default": "'none'",
|
|
51
|
+
"check": "\"titleSource\" IN ('none', 'auto', 'user')"
|
|
52
|
+
},
|
|
53
|
+
"model": { "type": "text", "references": "ai_models.id", "onDelete": "set null" },
|
|
54
|
+
"reasoning": { "type": "text", "required": true, "default": "'default'" },
|
|
55
|
+
"created": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
56
|
+
"modified": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
57
|
+
"allowedTools": { "type": "text[]", "required": true, "default": "'{}'" }
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"ai_messages": {
|
|
61
|
+
"columns": {
|
|
62
|
+
"id": { "type": "uuid", "required": true, "primary": true, "default": "gen_random_uuid()" },
|
|
63
|
+
"conversationId": { "type": "uuid", "required": true, "references": "ai_conversations.id", "onDelete": "cascade" },
|
|
64
|
+
"created": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
65
|
+
"role": { "type": "text", "required": true, "check": "role IN ('user', 'assistant', 'tool')" },
|
|
66
|
+
"content": { "type": "text", "required": true, "default": "''" },
|
|
67
|
+
"reasoning": { "type": "text" },
|
|
68
|
+
"toolCalls": { "type": "jsonb" },
|
|
69
|
+
"toolCallId": { "type": "text" },
|
|
70
|
+
"status": {
|
|
71
|
+
"type": "text",
|
|
72
|
+
"required": true,
|
|
73
|
+
"default": "'done'",
|
|
74
|
+
"check": "status IN ('pending', 'done', 'error', 'cancelled', 'awaiting')"
|
|
75
|
+
},
|
|
76
|
+
"error": { "type": "text" },
|
|
77
|
+
"usage": { "type": "jsonb" },
|
|
78
|
+
"voice": { "type": "boolean", "required": true, "default": "false" }
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"ai_memories": {
|
|
82
|
+
"columns": {
|
|
83
|
+
"userId": { "type": "uuid", "required": true, "references": "users.id", "onDelete": "cascade", "primary": true },
|
|
84
|
+
"id": { "type": "text", "required": true, "primary": true },
|
|
85
|
+
"description": { "type": "text", "required": true },
|
|
86
|
+
"content": { "type": "text", "required": true, "default": "''" },
|
|
87
|
+
"source": {
|
|
88
|
+
"type": "text",
|
|
89
|
+
"required": true,
|
|
90
|
+
"default": "'user'",
|
|
91
|
+
"check": "source IN ('user', 'explicit', 'inferred', 'periodic')"
|
|
92
|
+
},
|
|
93
|
+
"conversationId": { "type": "uuid", "references": "ai_conversations.id", "onDelete": "set null" },
|
|
94
|
+
"created": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
95
|
+
"modified": { "type": "timestamptz", "required": true, "default": "now()" }
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
"ai_memory_updates": {
|
|
99
|
+
"columns": {
|
|
100
|
+
"userId": { "type": "uuid", "required": true, "references": "users.id", "onDelete": "cascade", "primary": true },
|
|
101
|
+
"ran": { "type": "timestamptz", "required": true, "default": "now()" },
|
|
102
|
+
"summary": { "type": "text" },
|
|
103
|
+
"changes": { "type": "jsonb", "required": true, "default": "'{}'::jsonb" }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"indexes": {
|
|
108
|
+
"ai_conversations_userId": { "on": "ai_conversations", "columns": ["userId"] },
|
|
109
|
+
"ai_messages_conversationId": { "on": "ai_messages", "columns": ["conversationId"] }
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
],
|
|
113
|
+
"wipe": ["ai_memory_updates", "ai_memories", "ai_messages", "ai_conversations", "ai_roles", "ai_models"]
|
|
114
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { config } from '@axium/client/cli/config';
|
|
2
|
+
import { fetchAPI } from '@axium/client/requests';
|
|
3
|
+
import { configCommand } from '@james-pre/config/cli';
|
|
4
|
+
import { program } from 'commander';
|
|
5
|
+
import * as io from 'ioium/node';
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { styleText } from 'node:util';
|
|
8
|
+
import * as format from 'utilium/format';
|
|
9
|
+
import * as ai from '../common.js';
|
|
10
|
+
import * as llama from '../node/llama.js';
|
|
11
|
+
import * as models from '../node/models.js';
|
|
12
|
+
import { configManager } from './config.js';
|
|
13
|
+
import * as local from './local.js';
|
|
14
|
+
const cli = program.command('ai').helpGroup('Plugins:').description('CLI integration for @axium/ai');
|
|
15
|
+
configCommand(cli, configManager, { defaultType: 'user', sensitive: ['hf_token'] }).description('Manage this device’s AI settings');
|
|
16
|
+
/** The gateway URL and this client's session token */
|
|
17
|
+
function gateway() {
|
|
18
|
+
if (!config.server || !config.token)
|
|
19
|
+
io.exit('Not logged in. Run `axc login` first.', 4);
|
|
20
|
+
return { url: new URL('/api/ai/gateway', config.server).href, token: config.token };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Checks that a model or role can be used. The gateway resolves roles on each request,
|
|
24
|
+
* so a role keeps working when its first model becomes unavailable.
|
|
25
|
+
*/
|
|
26
|
+
async function checkModel(requested) {
|
|
27
|
+
const { models, roles } = await fetchAPI('GET', 'ai/models');
|
|
28
|
+
const usable = models.filter(ai.isUsable);
|
|
29
|
+
const role = ai.Role.safeParse(requested);
|
|
30
|
+
if (usable.some(model => model.id == requested))
|
|
31
|
+
return;
|
|
32
|
+
if (!role.success) {
|
|
33
|
+
io.exit(`Unknown or unavailable model "${requested}". Available: ${[...usable.map(model => model.id), ...ai.roles].join(', ')}`, 2);
|
|
34
|
+
}
|
|
35
|
+
if (!ai.resolveRole(models, roles, role.data))
|
|
36
|
+
io.exit(`No model is available for the ${role.data} role`, 3);
|
|
37
|
+
}
|
|
38
|
+
/** Environment variables that point Anthropic and OpenAI clients at the gateway */
|
|
39
|
+
async function environment(model = 'coding') {
|
|
40
|
+
const { url, token } = gateway();
|
|
41
|
+
await checkModel(model);
|
|
42
|
+
return {
|
|
43
|
+
ANTHROPIC_BASE_URL: url,
|
|
44
|
+
ANTHROPIC_AUTH_TOKEN: token,
|
|
45
|
+
ANTHROPIC_MODEL: model,
|
|
46
|
+
ANTHROPIC_DEFAULT_OPUS_MODEL: model,
|
|
47
|
+
ANTHROPIC_DEFAULT_SONNET_MODEL: model,
|
|
48
|
+
ANTHROPIC_DEFAULT_HAIKU_MODEL: model,
|
|
49
|
+
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
50
|
+
OPENAI_BASE_URL: url + '/v1',
|
|
51
|
+
OPENAI_API_KEY: token,
|
|
52
|
+
OPENAI_MODEL: model,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const shellQuote = (value) => `'${value.replaceAll("'", `'\\''`)}'`;
|
|
56
|
+
/** The device's models, connecting to its llama-server when it runs */
|
|
57
|
+
async function localModels() {
|
|
58
|
+
await local.connect().catch(error => io.warn(io.errorText(error)));
|
|
59
|
+
return (await local.getModels()).models;
|
|
60
|
+
}
|
|
61
|
+
async function serverModels() {
|
|
62
|
+
if (!config.token)
|
|
63
|
+
return;
|
|
64
|
+
return await fetchAPI('GET', 'ai/models').catch(error => {
|
|
65
|
+
io.warn('Could not get the server’s models: ' + io.errorText(error));
|
|
66
|
+
return undefined;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function printModel(model, serves) {
|
|
70
|
+
const status = ai.isUsable(model) ? styleText('green', model.status) : styleText('yellow', model.status);
|
|
71
|
+
const progress = model.progress?.total ? ` ${Math.floor((model.progress.done / model.progress.total) * 100)}%` : '';
|
|
72
|
+
console.log(' ' + styleText('bold', model.name), styleText('dim', model.id), status + progress, styleText('blueBright', format.bytes(model.size)), serves.length ? styleText('cyan', `(${serves.join(', ')})`) : '');
|
|
73
|
+
}
|
|
74
|
+
const cli_models = cli
|
|
75
|
+
.command('models')
|
|
76
|
+
.description('List the models on this device and the server')
|
|
77
|
+
.action(async () => {
|
|
78
|
+
const [device, server] = await Promise.all([localModels(), serverModels()]);
|
|
79
|
+
const deviceRoles = local.getRoles();
|
|
80
|
+
console.log(styleText('whiteBright', 'This device:'));
|
|
81
|
+
for (const model of device)
|
|
82
|
+
printModel(model, ai.roles.filter(role => ai.firstServable(device, deviceRoles[role], role)?.id == model.id));
|
|
83
|
+
if (!device.length)
|
|
84
|
+
console.log(styleText('dim', ' No models. Add one with `axc ai models add <repository>`.'));
|
|
85
|
+
if (!server)
|
|
86
|
+
return;
|
|
87
|
+
console.log(styleText('whiteBright', 'Server:'));
|
|
88
|
+
for (const model of server.models) {
|
|
89
|
+
printModel(model, ai.roles.filter(role => ai.resolveRole(server.models, server.roles, role)?.id == model.id));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
/** Shows a download's progress until it finishes */
|
|
93
|
+
async function followDownload(id, events) {
|
|
94
|
+
const model = await models.get(id);
|
|
95
|
+
if (model?.status != 'downloading')
|
|
96
|
+
return;
|
|
97
|
+
io.start('Downloading ' + model.name);
|
|
98
|
+
for await (const event of events) {
|
|
99
|
+
if (event.model != model.source)
|
|
100
|
+
continue;
|
|
101
|
+
if (event.event == 'download_failed')
|
|
102
|
+
throw new Error('The download failed: ' + JSON.stringify(event.data ?? {}));
|
|
103
|
+
if (event.event == 'download_finished')
|
|
104
|
+
break;
|
|
105
|
+
if (event.event != 'download_progress')
|
|
106
|
+
continue;
|
|
107
|
+
let done = 0, total = 0;
|
|
108
|
+
for (const file of Object.values(event.data)) {
|
|
109
|
+
done += file.done;
|
|
110
|
+
total += file.total;
|
|
111
|
+
}
|
|
112
|
+
if (done < total)
|
|
113
|
+
io.progress(done, total, undefined, `${format.bytes(done)} of ${format.bytes(total)}`);
|
|
114
|
+
}
|
|
115
|
+
io.done();
|
|
116
|
+
}
|
|
117
|
+
cli_models
|
|
118
|
+
.command('add')
|
|
119
|
+
.description('Download a model from Hugging Face to this device')
|
|
120
|
+
.argument('<repository>', 'the Hugging Face repository, e.g. unsloth/gemma-4-E4B-it-GGUF')
|
|
121
|
+
.argument('[quantization]', 'e.g. Q4_K_M; asked for when not given')
|
|
122
|
+
.option('-n, --name <name>', 'the display name')
|
|
123
|
+
.option('-i, --id <id>', 'the model ID, from the name by default')
|
|
124
|
+
.option('--no-wait', 'return once the download starts')
|
|
125
|
+
.action(async (repository, quantization, options) => {
|
|
126
|
+
await local.connect().catch(io.exit);
|
|
127
|
+
io.start('Getting ' + repository);
|
|
128
|
+
const details = await local.details(repository).catch(io.exit);
|
|
129
|
+
io.done();
|
|
130
|
+
const fit = (quant) => ({ device: 'fits in VRAM', host: 'needs RAM', none: styleText('red', 'too large') })[quant.fit];
|
|
131
|
+
quantization ??= await io.select('Quantization', details.quants.map(quant => ({ name: `${quant.tag} (${format.bytes(quant.size)}, ${fit(quant)})`, value: quant.tag })));
|
|
132
|
+
const name = options.name ?? ai.nameFromRepo(details.base ?? details.repo);
|
|
133
|
+
const id = ai.ModelId.parse(options.id ?? ai.modelIdFrom(name));
|
|
134
|
+
const controller = new AbortController();
|
|
135
|
+
const events = await llama.follow(controller.signal).catch(io.exit);
|
|
136
|
+
const model = await local.add({ id, name, repo: details.repo, quant: quantization }).catch(io.exit);
|
|
137
|
+
console.log(`Added ${styleText('bold', model.name)} ${styleText('dim', `(${model.id})`)}`);
|
|
138
|
+
if (options.wait)
|
|
139
|
+
await followDownload(id, events).catch(io.exit);
|
|
140
|
+
controller.abort();
|
|
141
|
+
});
|
|
142
|
+
cli_models
|
|
143
|
+
.command('remove')
|
|
144
|
+
.alias('rm')
|
|
145
|
+
.description('Delete a model from this device')
|
|
146
|
+
.argument('<id>')
|
|
147
|
+
.option('-y, --yes', 'do not ask for confirmation')
|
|
148
|
+
.action(async (id, options) => {
|
|
149
|
+
await local.connect().catch(io.exit);
|
|
150
|
+
if (!options.yes)
|
|
151
|
+
await io.assertYes(`Delete ${id} and its files?`);
|
|
152
|
+
await local.remove(id).catch(io.exit);
|
|
153
|
+
console.log('Deleted', id);
|
|
154
|
+
});
|
|
155
|
+
for (const action of ['load', 'unload']) {
|
|
156
|
+
cli_models
|
|
157
|
+
.command(action)
|
|
158
|
+
.description(`${action == 'load' ? 'Load' : 'Unload'} a model on this device`)
|
|
159
|
+
.argument('<id>')
|
|
160
|
+
.action(async (id) => {
|
|
161
|
+
await local.connect().catch(io.exit);
|
|
162
|
+
if (!(await models.get(id)))
|
|
163
|
+
io.exit(`There is no local model "${id}"`, 2);
|
|
164
|
+
await llama[action](id).catch(io.exit);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
cli_models
|
|
168
|
+
.command('search')
|
|
169
|
+
.description('Search Hugging Face for models to add')
|
|
170
|
+
.argument('<query...>')
|
|
171
|
+
.action(async (query) => {
|
|
172
|
+
const results = await local.search(query.join(' ')).catch(io.exit);
|
|
173
|
+
for (const repo of results) {
|
|
174
|
+
console.log(styleText('bold', repo.id), styleText('dim', `${repo.downloads.toLocaleString()} downloads, ${repo.likes} likes`));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
const cli_roles = cli
|
|
178
|
+
.command('roles')
|
|
179
|
+
.description('Show which model each role uses: the first usable model in this device’s list, then the server’s')
|
|
180
|
+
.action(async () => {
|
|
181
|
+
const [device, server] = await Promise.all([localModels(), serverModels()]);
|
|
182
|
+
const deviceRoles = local.getRoles();
|
|
183
|
+
for (const role of ai.roles) {
|
|
184
|
+
const onDevice = ai.firstServable(device, deviceRoles[role], role);
|
|
185
|
+
const onServer = server && ai.resolveRole(server.models, server.roles, role);
|
|
186
|
+
const using = onDevice
|
|
187
|
+
? `${onDevice.name} ${styleText('dim', '(this device)')}`
|
|
188
|
+
: onServer
|
|
189
|
+
? `${onServer.name} ${styleText('dim', '(server)')}`
|
|
190
|
+
: styleText('yellow', 'none');
|
|
191
|
+
const list = deviceRoles[role].length ? styleText('dim', ` [${deviceRoles[role].join(', ')}]`) : '';
|
|
192
|
+
console.log(styleText('bold', role.padEnd(10)), using + list);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
cli_roles
|
|
196
|
+
.command('set')
|
|
197
|
+
.description('Set the local models a role uses, most preferred first; none clears the list')
|
|
198
|
+
.argument('<role>', ai.roles.join(', '))
|
|
199
|
+
.argument('[models...]')
|
|
200
|
+
.action((role, ids) => {
|
|
201
|
+
const parsed = ai.Role.safeParse(role);
|
|
202
|
+
if (!parsed.success)
|
|
203
|
+
io.exit(`Unknown role "${role}". Roles: ${ai.roles.join(', ')}`, 2);
|
|
204
|
+
local.setRoles({ [parsed.data]: ids });
|
|
205
|
+
});
|
|
206
|
+
cli.command('create-env')
|
|
207
|
+
.description('Print shell exports that point Anthropic and OpenAI clients at the AI gateway, using this client’s session')
|
|
208
|
+
.option('-m, --model <id>', 'a model or role to use instead of the coding role')
|
|
209
|
+
.action(async (options) => {
|
|
210
|
+
for (const [name, value] of Object.entries(await environment(options.model))) {
|
|
211
|
+
console.log(`export ${name}=${shellQuote(value)}`);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
cli.command('claude')
|
|
215
|
+
.description('Run Claude Code against the AI gateway, using this client’s session. Other arguments are passed to Claude Code.')
|
|
216
|
+
.option('-m, --model <id>', 'a model or role to use instead of the coding role')
|
|
217
|
+
.argument('[args...]')
|
|
218
|
+
.allowUnknownOption()
|
|
219
|
+
.action(async (args, options) => {
|
|
220
|
+
const env = { ...process.env, ...(await environment(options.model)) };
|
|
221
|
+
const child = spawn('claude', args, { stdio: 'inherit', env });
|
|
222
|
+
child.on('error', error => io.exit('Could not run Claude Code: ' + io.errorText(error), 5));
|
|
223
|
+
child.on('exit', (code, signal) => process.exit(code ?? (signal ? 1 : 0)));
|
|
224
|
+
});
|