wirejs-resources 0.1.150-llm → 0.1.152-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.
@@ -1,13 +1,7 @@
1
1
  import { Resource } from '../resource.js';
2
2
  export declare class Secret extends Resource {
3
+ #private;
3
4
  constructor(scope: Resource | string, id: string);
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;
5
+ read(): Promise<any>;
6
+ write(data: any): Promise<void>;
13
7
  }
@@ -1,38 +1,28 @@
1
- import * as crypto from 'crypto';
1
+ import crypto from 'crypto';
2
2
  import { Resource } from '../resource.js';
3
- import { KeyValueStore } from './key-value-store.js';
3
+ import { FileService } from '../services/file.js';
4
4
  import { overrides } from '../overrides.js';
5
- let secrets;
5
+ const FILENAME = 'secret';
6
6
  export class Secret extends Resource {
7
+ #fileService;
8
+ #initPromise;
7
9
  constructor(scope, id) {
8
10
  super(scope, id);
9
- secrets = new (overrides.KeyValueStore || KeyValueStore)('wirejs', 'secrets');
11
+ this.#fileService = new (overrides.FileService || FileService)(this, 'files');
10
12
  }
11
- async read() {
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 });
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;
31
19
  }
32
- async *scan() {
33
- return secrets?.scan();
20
+ async read() {
21
+ await this.#initialize();
22
+ return JSON.parse(await this.#fileService.read(FILENAME));
34
23
  }
35
- isAlreadyExistsError(error) {
36
- return secrets?.isAlreadyExistsError(error);
24
+ async write(data) {
25
+ await this.#initialize();
26
+ await this.#fileService.write(FILENAME, JSON.stringify(data));
37
27
  }
38
28
  }
@@ -10,15 +10,25 @@ export type ToolCall = {
10
10
  arguments: Record<string, any>;
11
11
  };
12
12
  };
13
- export type LLMMessage = {
14
- /** Standard user or assistant message */
15
- role: 'user' | 'assistant';
13
+ /** User message in conversation */
14
+ export type UserMessage = {
15
+ /** Always 'user' for user messages */
16
+ role: 'user';
16
17
  /** Text content of the message */
17
18
  content: string;
18
- /** Tool calls requested by the assistant (only for assistant messages) */
19
+ };
20
+ /** Assistant message type - what LLM methods actually return */
21
+ export type AssistantMessage = {
22
+ /** Always 'assistant' for LLM responses */
23
+ role: 'assistant';
24
+ /** Text content of the message */
25
+ content: string;
26
+ /** Tool calls requested by the assistant */
19
27
  tool_calls?: ToolCall[];
20
- } | {
21
- /** Tool execution result message */
28
+ };
29
+ /** Tool execution result message */
30
+ export type ToolMessage = {
31
+ /** Always 'tool' for tool execution results */
22
32
  role: 'tool';
23
33
  /** Result content from the tool execution */
24
34
  content: string;
@@ -27,6 +37,8 @@ export type LLMMessage = {
27
37
  /** ID linking this result to the original tool call */
28
38
  tool_call_id: string;
29
39
  };
40
+ /** Union of all possible message types in conversation history */
41
+ export type LLMMessage = UserMessage | AssistantMessage | ToolMessage;
30
42
  export type LLMChunk = {
31
43
  created_at: string;
32
44
  message: LLMMessage;
@@ -88,6 +100,6 @@ export declare class LLM extends Resource {
88
100
  private checkOllamaAvailable;
89
101
  private checkModelExists;
90
102
  private createStreamedString;
91
- continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }: ContinueConversationOptions): Promise<LLMMessage>;
103
+ continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }: ContinueConversationOptions): Promise<AssistantMessage>;
92
104
  }
93
105
  export {};
@@ -40,11 +40,9 @@ export class LLM extends Resource {
40
40
  tool_calls = chunk.message.tool_calls;
41
41
  }
42
42
  }
43
- // Ensure we have a valid role, default to assistant if unexpected
44
- const finalRole = (role === 'user' || role === 'assistant') ? role : 'assistant';
45
- // Build the result message
43
+ // Build the assistant result message
46
44
  const result = {
47
- role: finalRole,
45
+ role: 'assistant',
48
46
  content
49
47
  };
50
48
  if (tool_calls) {
@@ -161,7 +159,18 @@ export class LLM extends Resource {
161
159
  }
162
160
  else {
163
161
  const chunk = await response.json();
164
- return chunk.message;
162
+ // Ensure we return an assistant message
163
+ const message = chunk.message;
164
+ if (message.role === 'assistant') {
165
+ return message;
166
+ }
167
+ else {
168
+ // Fallback: create assistant message
169
+ return {
170
+ role: 'assistant',
171
+ content: message.content
172
+ };
173
+ }
165
174
  }
166
175
  }
167
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.150-llm",
3
+ "version": "0.1.152-llm",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1 +0,0 @@
1
- export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -1,68 +0,0 @@
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 +0,0 @@
1
- export declare function apiTree(INTERNAL_API_URL: string, path?: string[]): () => void;
@@ -1,68 +0,0 @@
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 +0,0 @@
1
- export declare function prebuildApi(): Promise<void>;
@@ -1,27 +0,0 @@
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
- }