wirejs-resources 0.1.147-llm → 0.1.149-llm
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/hosting/client.d.ts +1 -0
- package/dist/hosting/client.js +68 -0
- package/dist/internal/client.d.ts +1 -0
- package/dist/internal/client.js +68 -0
- package/dist/resources/secret.d.ts +9 -3
- package/dist/resources/secret.js +28 -18
- package/dist/services/llm.d.ts +6 -1
- package/dist/services/llm.js +10 -4
- package/dist/setup/index.d.ts +1 -0
- package/dist/setup/index.js +27 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
async function callApi(INTERNAL_API_URL, method, ...args) {
|
|
2
|
+
function isNode() {
|
|
3
|
+
return typeof args[0]?.cookies?.getAll === 'function';
|
|
4
|
+
}
|
|
5
|
+
function apiUrl() {
|
|
6
|
+
if (isNode()) {
|
|
7
|
+
return INTERNAL_API_URL;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return "/api";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
let cookieHeader = {};
|
|
14
|
+
if (isNode()) {
|
|
15
|
+
const context = args[0];
|
|
16
|
+
const cookies = context.cookies.getAll();
|
|
17
|
+
cookieHeader = typeof cookies === 'object'
|
|
18
|
+
? {
|
|
19
|
+
Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
|
|
20
|
+
}
|
|
21
|
+
: {};
|
|
22
|
+
}
|
|
23
|
+
const response = await fetch(apiUrl(), {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
...cookieHeader
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify([{ method, args: [...args] }]),
|
|
30
|
+
});
|
|
31
|
+
const body = await response.json();
|
|
32
|
+
if (isNode()) {
|
|
33
|
+
const context = args[0];
|
|
34
|
+
for (const c of response.headers.getSetCookie()) {
|
|
35
|
+
const parts = c.split(';').map(p => p.trim());
|
|
36
|
+
const flags = parts.slice(1);
|
|
37
|
+
const [name, value] = parts[0].split('=').map(decodeURIComponent);
|
|
38
|
+
const httpOnly = flags.includes('HttpOnly');
|
|
39
|
+
const secure = flags.includes('Secure');
|
|
40
|
+
const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
|
|
41
|
+
context.cookies.set({
|
|
42
|
+
name,
|
|
43
|
+
value,
|
|
44
|
+
httpOnly,
|
|
45
|
+
secure,
|
|
46
|
+
maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const error = body[0].error;
|
|
51
|
+
if (error) {
|
|
52
|
+
throw new Error(error);
|
|
53
|
+
}
|
|
54
|
+
const value = body[0].data;
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
;
|
|
58
|
+
export function apiTree(INTERNAL_API_URL, path = []) {
|
|
59
|
+
return new Proxy(function () { }, {
|
|
60
|
+
apply(_target, _thisArg, args) {
|
|
61
|
+
return callApi(INTERNAL_API_URL, path, ...args);
|
|
62
|
+
},
|
|
63
|
+
get(_target, prop) {
|
|
64
|
+
return apiTree(INTERNAL_API_URL, [...path, prop]);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
async function callApi(INTERNAL_API_URL, method, ...args) {
|
|
2
|
+
function isNode() {
|
|
3
|
+
return typeof args[0]?.cookies?.getAll === 'function';
|
|
4
|
+
}
|
|
5
|
+
function apiUrl() {
|
|
6
|
+
if (isNode()) {
|
|
7
|
+
return INTERNAL_API_URL;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return "/api";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
let cookieHeader = {};
|
|
14
|
+
if (isNode()) {
|
|
15
|
+
const context = args[0];
|
|
16
|
+
const cookies = context.cookies.getAll();
|
|
17
|
+
cookieHeader = typeof cookies === 'object'
|
|
18
|
+
? {
|
|
19
|
+
Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
|
|
20
|
+
}
|
|
21
|
+
: {};
|
|
22
|
+
}
|
|
23
|
+
const response = await fetch(apiUrl(), {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
...cookieHeader
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify([{ method, args: [...args] }]),
|
|
30
|
+
});
|
|
31
|
+
const body = await response.json();
|
|
32
|
+
if (isNode()) {
|
|
33
|
+
const context = args[0];
|
|
34
|
+
for (const c of response.headers.getSetCookie()) {
|
|
35
|
+
const parts = c.split(';').map(p => p.trim());
|
|
36
|
+
const flags = parts.slice(1);
|
|
37
|
+
const [name, value] = parts[0].split('=').map(decodeURIComponent);
|
|
38
|
+
const httpOnly = flags.includes('HttpOnly');
|
|
39
|
+
const secure = flags.includes('Secure');
|
|
40
|
+
const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
|
|
41
|
+
context.cookies.set({
|
|
42
|
+
name,
|
|
43
|
+
value,
|
|
44
|
+
httpOnly,
|
|
45
|
+
secure,
|
|
46
|
+
maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const error = body[0].error;
|
|
51
|
+
if (error) {
|
|
52
|
+
throw new Error(error);
|
|
53
|
+
}
|
|
54
|
+
const value = body[0].data;
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
;
|
|
58
|
+
export function apiTree(INTERNAL_API_URL, path = []) {
|
|
59
|
+
return new Proxy(function () { }, {
|
|
60
|
+
apply(_target, _thisArg, args) {
|
|
61
|
+
return callApi(INTERNAL_API_URL, path, ...args);
|
|
62
|
+
},
|
|
63
|
+
get(_target, prop) {
|
|
64
|
+
return apiTree(INTERNAL_API_URL, [...path, prop]);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { Resource } from '../resource.js';
|
|
2
2
|
export declare class Secret extends Resource {
|
|
3
|
-
#private;
|
|
4
3
|
constructor(scope: Resource | string, id: string);
|
|
5
|
-
read(): Promise<
|
|
6
|
-
write(
|
|
4
|
+
read(): Promise<string>;
|
|
5
|
+
write(value: string, options?: {
|
|
6
|
+
onlyIfNotExists?: boolean;
|
|
7
|
+
}): Promise<void | undefined>;
|
|
8
|
+
scan(): AsyncGenerator<never, AsyncGenerator<{
|
|
9
|
+
key: string;
|
|
10
|
+
value: string;
|
|
11
|
+
}, any, any> | undefined, unknown>;
|
|
12
|
+
isAlreadyExistsError(error: any): boolean | undefined;
|
|
7
13
|
}
|
package/dist/resources/secret.js
CHANGED
|
@@ -1,28 +1,38 @@
|
|
|
1
|
-
import crypto from 'crypto';
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
2
|
import { Resource } from '../resource.js';
|
|
3
|
-
import {
|
|
3
|
+
import { KeyValueStore } from './key-value-store.js';
|
|
4
4
|
import { overrides } from '../overrides.js';
|
|
5
|
-
|
|
5
|
+
let secrets;
|
|
6
6
|
export class Secret extends Resource {
|
|
7
|
-
#fileService;
|
|
8
|
-
#initPromise;
|
|
9
7
|
constructor(scope, id) {
|
|
10
8
|
super(scope, id);
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
#initialize() {
|
|
14
|
-
this.#initPromise = this.#initPromise || this.#fileService.write(FILENAME, JSON.stringify(crypto.randomBytes(64).toString('base64url')), { onlyIfNotExists: true }).catch(error => {
|
|
15
|
-
if (!this.#fileService.isAlreadyExistsError(error))
|
|
16
|
-
throw error;
|
|
17
|
-
});
|
|
18
|
-
return this.#initPromise;
|
|
9
|
+
secrets = new (overrides.KeyValueStore || KeyValueStore)('wirejs', 'secrets');
|
|
19
10
|
}
|
|
20
11
|
async read() {
|
|
21
|
-
await this
|
|
22
|
-
|
|
12
|
+
const existingValue = await secrets?.get(this.absoluteId);
|
|
13
|
+
if (!existingValue) {
|
|
14
|
+
try {
|
|
15
|
+
const initValue = crypto.randomBytes(64).toString('base64url');
|
|
16
|
+
await this.write(initValue, { onlyIfNotExists: true });
|
|
17
|
+
return initValue;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (!this.isAlreadyExistsError(error))
|
|
21
|
+
throw error;
|
|
22
|
+
return this.read();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return existingValue;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async write(value, options) {
|
|
30
|
+
return secrets?.set(this.absoluteId, value, { onlyIfNotExists: options?.onlyIfNotExists });
|
|
31
|
+
}
|
|
32
|
+
async *scan() {
|
|
33
|
+
return secrets?.scan();
|
|
23
34
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
await this.#fileService.write(FILENAME, JSON.stringify(data));
|
|
35
|
+
isAlreadyExistsError(error) {
|
|
36
|
+
return secrets?.isAlreadyExistsError(error);
|
|
27
37
|
}
|
|
28
38
|
}
|
package/dist/services/llm.d.ts
CHANGED
|
@@ -12,17 +12,22 @@ export type ContinueConversationOptions = {
|
|
|
12
12
|
history: LLMMessage[];
|
|
13
13
|
onChunk?: (chunk: LLMChunk) => void | Promise<void>;
|
|
14
14
|
timeoutSeconds?: number;
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
models?: string[];
|
|
17
|
+
targetContextSize?: number;
|
|
15
18
|
};
|
|
16
19
|
export declare class LLM extends Resource {
|
|
17
20
|
models: string[];
|
|
18
21
|
systemPrompt: string | undefined;
|
|
22
|
+
targetContextSize: number;
|
|
19
23
|
constructor(scope: Resource | string, id: string, options: {
|
|
20
24
|
models: string[];
|
|
21
25
|
systemPrompt?: string;
|
|
26
|
+
targetContextSize?: number;
|
|
22
27
|
});
|
|
23
28
|
private stream;
|
|
24
29
|
private checkOllamaAvailable;
|
|
25
30
|
private checkModelExists;
|
|
26
31
|
private createStreamedString;
|
|
27
|
-
continueConversation({ history, onChunk, timeoutSeconds }: ContinueConversationOptions): Promise<LLMMessage>;
|
|
32
|
+
continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, }: ContinueConversationOptions): Promise<LLMMessage>;
|
|
28
33
|
}
|
package/dist/services/llm.js
CHANGED
|
@@ -2,10 +2,12 @@ import { Resource } from '../resource.js';
|
|
|
2
2
|
export class LLM extends Resource {
|
|
3
3
|
models;
|
|
4
4
|
systemPrompt;
|
|
5
|
+
targetContextSize;
|
|
5
6
|
constructor(scope, id, options) {
|
|
6
7
|
super(scope, id);
|
|
7
8
|
this.models = options.models;
|
|
8
9
|
this.systemPrompt = options.systemPrompt;
|
|
10
|
+
this.targetContextSize = options.targetContextSize ?? 16384;
|
|
9
11
|
}
|
|
10
12
|
async stream(response, onChunk) {
|
|
11
13
|
if (!response.ok || !response.body) {
|
|
@@ -77,7 +79,7 @@ export class LLM extends Resource {
|
|
|
77
79
|
content: message
|
|
78
80
|
};
|
|
79
81
|
}
|
|
80
|
-
async continueConversation({ history, onChunk, timeoutSeconds }) {
|
|
82
|
+
async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, }) {
|
|
81
83
|
const ollamaAvailable = await this.checkOllamaAvailable();
|
|
82
84
|
if (!ollamaAvailable) {
|
|
83
85
|
return this.createStreamedString('Ollama is not running locally. Please install and start Ollama:\n\n' +
|
|
@@ -87,7 +89,7 @@ export class LLM extends Resource {
|
|
|
87
89
|
'Models to try installing: ' + this.models.join(', '), onChunk);
|
|
88
90
|
}
|
|
89
91
|
// models should be in priority order. so, first one that works is the one we want.
|
|
90
|
-
for (const model of this.models) {
|
|
92
|
+
for (const model of models ?? this.models) {
|
|
91
93
|
const modelExists = await this.checkModelExists(model);
|
|
92
94
|
if (!modelExists)
|
|
93
95
|
continue;
|
|
@@ -99,6 +101,7 @@ export class LLM extends Resource {
|
|
|
99
101
|
controller.abort();
|
|
100
102
|
}, timeoutSeconds * 1000);
|
|
101
103
|
}
|
|
104
|
+
const finalSystemPrompt = systemPrompt ?? this.systemPrompt;
|
|
102
105
|
try {
|
|
103
106
|
const response = await fetch('http://localhost:11434/api/chat', {
|
|
104
107
|
method: 'POST',
|
|
@@ -106,15 +109,18 @@ export class LLM extends Resource {
|
|
|
106
109
|
body: JSON.stringify({
|
|
107
110
|
model: model,
|
|
108
111
|
messages: [
|
|
109
|
-
...(
|
|
112
|
+
...(finalSystemPrompt ? [
|
|
110
113
|
{
|
|
111
114
|
role: 'system',
|
|
112
|
-
content:
|
|
115
|
+
content: finalSystemPrompt
|
|
113
116
|
}
|
|
114
117
|
] : []),
|
|
115
118
|
...history
|
|
116
119
|
],
|
|
117
120
|
stream,
|
|
121
|
+
options: {
|
|
122
|
+
num_ctx: targetContextSize ?? this.targetContextSize
|
|
123
|
+
}
|
|
118
124
|
}),
|
|
119
125
|
signal: controller.signal
|
|
120
126
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function prebuildApi(): Promise<void>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import process from 'process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export async function prebuildApi() {
|
|
5
|
+
const CWD = process.cwd();
|
|
6
|
+
let API_URL = '/api';
|
|
7
|
+
const indexModule = await import(path.join(CWD, 'index.js'));
|
|
8
|
+
try {
|
|
9
|
+
const backendConfigModule = await import(path.join(CWD, 'config.js'));
|
|
10
|
+
const backendConfig = backendConfigModule.default;
|
|
11
|
+
console.log("backend config found", backendConfig);
|
|
12
|
+
if (backendConfig.apiUrl) {
|
|
13
|
+
API_URL = backendConfig.apiUrl;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
console.log("No backend API config found.");
|
|
18
|
+
}
|
|
19
|
+
const apiCode = Object.keys(indexModule)
|
|
20
|
+
.map(k => `export const ${k} = apiTree(INTERNAL_API_URL, ${JSON.stringify([k])});`)
|
|
21
|
+
.join('\n');
|
|
22
|
+
const baseClient = [
|
|
23
|
+
`import { apiTree } from "wirejs-resources/hosting/client.js";`,
|
|
24
|
+
`const INTERNAL_API_URL = ${JSON.stringify(API_URL)};`,
|
|
25
|
+
].join('\n');
|
|
26
|
+
await fs.promises.writeFile(path.join(CWD, 'index.client.js'), [baseClient, apiCode].join('\n\n'));
|
|
27
|
+
}
|