wirejs-resources 0.1.149-llm → 0.1.150-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,33 +1,93 @@
1
1
  import { Resource } from '../resource.js';
2
+ export type ToolCall = {
3
+ /** Unique identifier for this tool call */
4
+ id?: string;
5
+ /** Function to be called */
6
+ function: {
7
+ /** Name of the function to call */
8
+ name: string;
9
+ /** Arguments to pass to the function (JSON object) */
10
+ arguments: Record<string, any>;
11
+ };
12
+ };
2
13
  export type LLMMessage = {
3
- role: 'assistant' | 'user';
14
+ /** Standard user or assistant message */
15
+ role: 'user' | 'assistant';
16
+ /** Text content of the message */
17
+ content: string;
18
+ /** Tool calls requested by the assistant (only for assistant messages) */
19
+ tool_calls?: ToolCall[];
20
+ } | {
21
+ /** Tool execution result message */
22
+ role: 'tool';
23
+ /** Result content from the tool execution */
4
24
  content: string;
25
+ /** Name of the tool that was executed */
26
+ tool_name: string;
27
+ /** ID linking this result to the original tool call */
28
+ tool_call_id: string;
5
29
  };
6
30
  export type LLMChunk = {
7
31
  created_at: string;
8
32
  message: LLMMessage;
9
33
  done: boolean;
10
34
  };
35
+ type ToolParameterProperty = {
36
+ /** The property's data type */
37
+ type: string;
38
+ /** Human-readable description of this property */
39
+ description?: string;
40
+ /** Allowed values for this property (for constrained inputs) */
41
+ enum?: string[] | number[];
42
+ /** Schema for array elements when this property's type is 'array' */
43
+ items?: {
44
+ type: string;
45
+ description?: string;
46
+ };
47
+ /** Minimum value for numeric properties */
48
+ minimum?: number;
49
+ /** Maximum value for numeric properties */
50
+ maximum?: number;
51
+ };
52
+ export type ToolDefinition = {
53
+ /** The name of the tool function */
54
+ name: string;
55
+ /** Human-readable description of what the tool does */
56
+ description: string;
57
+ /** JSON Schema definition for the tool's parameters - defines named function parameters */
58
+ parameters: {
59
+ /** Always 'object' - functions must have named parameters */
60
+ type: 'object';
61
+ /** Named function parameters and their schemas */
62
+ properties: Record<string, ToolParameterProperty>;
63
+ /** Array of parameter names that must be provided */
64
+ required?: string[];
65
+ };
66
+ };
11
67
  export type ContinueConversationOptions = {
12
68
  history: LLMMessage[];
13
69
  onChunk?: (chunk: LLMChunk) => void | Promise<void>;
14
70
  timeoutSeconds?: number;
15
71
  systemPrompt?: string;
16
72
  models?: string[];
73
+ tools?: ToolDefinition[];
17
74
  targetContextSize?: number;
18
75
  };
19
76
  export declare class LLM extends Resource {
20
77
  models: string[];
21
78
  systemPrompt: string | undefined;
22
79
  targetContextSize: number;
80
+ tools: ToolDefinition[];
23
81
  constructor(scope: Resource | string, id: string, options: {
24
82
  models: string[];
25
83
  systemPrompt?: string;
26
84
  targetContextSize?: number;
85
+ tools?: ToolDefinition[];
27
86
  });
28
87
  private stream;
29
88
  private checkOllamaAvailable;
30
89
  private checkModelExists;
31
90
  private createStreamedString;
32
- continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, }: ContinueConversationOptions): Promise<LLMMessage>;
91
+ continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }: ContinueConversationOptions): Promise<LLMMessage>;
33
92
  }
93
+ export {};
@@ -3,11 +3,13 @@ export class LLM extends Resource {
3
3
  models;
4
4
  systemPrompt;
5
5
  targetContextSize;
6
+ tools;
6
7
  constructor(scope, id, options) {
7
8
  super(scope, id);
8
9
  this.models = options.models;
9
10
  this.systemPrompt = options.systemPrompt;
10
11
  this.targetContextSize = options.targetContextSize ?? 16384;
12
+ this.tools = options.tools ?? [];
11
13
  }
12
14
  async stream(response, onChunk) {
13
15
  if (!response.ok || !response.body) {
@@ -17,6 +19,7 @@ export class LLM extends Resource {
17
19
  const decoder = new TextDecoder('utf-8');
18
20
  let role = 'assistant';
19
21
  let content = '';
22
+ let tool_calls;
20
23
  while (true) {
21
24
  const { value, done } = await reader.read();
22
25
  if (done)
@@ -32,8 +35,22 @@ export class LLM extends Resource {
32
35
  }
33
36
  role = chunk.message.role;
34
37
  content += chunk.message.content;
38
+ // Capture tool calls if present in the chunk
39
+ if ('tool_calls' in chunk.message && chunk.message.tool_calls) {
40
+ tool_calls = chunk.message.tool_calls;
41
+ }
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
46
+ const result = {
47
+ role: finalRole,
48
+ content
49
+ };
50
+ if (tool_calls) {
51
+ result.tool_calls = tool_calls;
35
52
  }
36
- return { role, content };
53
+ return result;
37
54
  }
38
55
  async checkOllamaAvailable() {
39
56
  try {
@@ -79,7 +96,7 @@ export class LLM extends Resource {
79
96
  content: message
80
97
  };
81
98
  }
82
- async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, }) {
99
+ async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }) {
83
100
  const ollamaAvailable = await this.checkOllamaAvailable();
84
101
  if (!ollamaAvailable) {
85
102
  return this.createStreamedString('Ollama is not running locally. Please install and start Ollama:\n\n' +
@@ -102,6 +119,16 @@ export class LLM extends Resource {
102
119
  }, timeoutSeconds * 1000);
103
120
  }
104
121
  const finalSystemPrompt = systemPrompt ?? this.systemPrompt;
122
+ const finalTools = tools ?? this.tools;
123
+ // Transform our ToolDefinition format to Ollama's expected format
124
+ const ollamaTools = finalTools.map(tool => ({
125
+ type: "function",
126
+ function: {
127
+ name: tool.name,
128
+ description: tool.description,
129
+ parameters: tool.parameters
130
+ }
131
+ }));
105
132
  try {
106
133
  const response = await fetch('http://localhost:11434/api/chat', {
107
134
  method: 'POST',
@@ -118,6 +145,7 @@ export class LLM extends Resource {
118
145
  ...history
119
146
  ],
120
147
  stream,
148
+ ...(ollamaTools.length > 0 ? { tools: ollamaTools } : {}),
121
149
  options: {
122
150
  num_ctx: targetContextSize ?? this.targetContextSize
123
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.149-llm",
3
+ "version": "0.1.150-llm",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",