@lowire/loop 0.0.2 → 0.0.3

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/lib/types.d.ts CHANGED
@@ -23,73 +23,79 @@ export type Tool = {
23
23
  description?: string;
24
24
  inputSchema: Schema;
25
25
  };
26
- export type ToolCallPart = {
27
- type: 'tool_call';
28
- name: string;
29
- arguments: any;
30
- id: string;
31
- googleThoughtSignature?: string;
32
- openaiId?: string;
33
- openaiStatus?: 'completed' | 'incomplete' | 'in_progress';
34
- };
35
26
  export type ToolCallback = (params: {
36
27
  name: string;
37
28
  arguments: any;
38
29
  }) => Promise<ToolResult>;
39
- export type Usage = {
40
- input: number;
41
- output: number;
42
- };
43
30
  export type BaseMessage = {
44
- role: 'user' | 'assistant' | 'tool_result';
31
+ role: 'user' | 'assistant';
45
32
  };
33
+ export type Message = UserMessage | AssistantMessage;
46
34
  export type UserMessage = BaseMessage & {
47
35
  role: 'user';
48
36
  content: string;
49
37
  };
50
38
  export type AssistantMessage = BaseMessage & {
51
39
  role: 'assistant';
52
- content: (TextContentPart | ToolCallPart | ThinkingContentPart)[];
40
+ content: (TextContentPart | ToolCallContentPart | ThinkingContentPart)[];
53
41
  openaiId?: string;
54
42
  openaiStatus?: 'completed' | 'incomplete' | 'in_progress';
43
+ toolError?: string;
55
44
  };
56
45
  export type TextContentPart = {
57
46
  type: 'text';
58
47
  text: string;
59
48
  googleThoughtSignature?: string;
60
- };
61
- export type ImageContentPart = {
62
- type: 'image';
63
- data: string;
64
- mimeType: string;
49
+ copilotToolCallId?: string;
65
50
  };
66
51
  export type ThinkingContentPart = {
67
52
  type: 'thinking';
68
53
  thinking: string;
69
54
  signature: string;
70
55
  };
71
- export type ResultContentPart = TextContentPart | ImageContentPart;
56
+ export type ToolCallContentPart = {
57
+ type: 'tool_call';
58
+ name: string;
59
+ arguments: any;
60
+ id: string;
61
+ googleThoughtSignature?: string;
62
+ openaiId?: string;
63
+ openaiStatus?: 'completed' | 'incomplete' | 'in_progress';
64
+ result?: ToolResult;
65
+ };
66
+ export type TextResultPart = {
67
+ type: 'text';
68
+ text: string;
69
+ };
70
+ export type ImageResultPart = {
71
+ type: 'image';
72
+ data: string;
73
+ mimeType: string;
74
+ };
75
+ export type ResultPart = TextResultPart | ImageResultPart;
72
76
  export type ToolResult = {
73
- content: ResultContentPart[];
77
+ _meta?: {
78
+ 'dev.lowire/history'?: {
79
+ category: string;
80
+ content: string;
81
+ }[];
82
+ 'dev.lowire/state'?: Record<string, string>;
83
+ };
84
+ content: ResultPart[];
74
85
  isError?: boolean;
75
86
  };
76
- export type ToolResultMessage = BaseMessage & {
77
- role: 'tool_result';
78
- toolName: string;
79
- toolCallId: string;
80
- result: ToolResult;
81
- };
82
- export type Message = UserMessage | AssistantMessage | ToolResultMessage;
83
87
  export type Conversation = {
84
88
  systemPrompt: string;
85
89
  messages: Message[];
86
90
  tools: Tool[];
87
91
  };
92
+ export type Debug = (category: string) => (...args: any[]) => void;
88
93
  export type CompletionOptions = {
89
94
  model: string;
90
95
  maxTokens?: number;
91
96
  reasoning?: boolean;
92
97
  temperature?: number;
98
+ debug?: Debug;
93
99
  };
94
100
  export interface Provider {
95
101
  name: string;
@@ -98,11 +104,11 @@ export interface Provider {
98
104
  usage: Usage;
99
105
  }>;
100
106
  }
107
+ export type Usage = {
108
+ input: number;
109
+ output: number;
110
+ };
101
111
  export type ReplayCache = Record<string, {
102
112
  result: AssistantMessage;
103
113
  usage: Usage;
104
114
  }>;
105
- export type ReplayCaches = {
106
- before: ReplayCache;
107
- after: ReplayCache;
108
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowire/loop",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Small agentic loop",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,14 +15,6 @@
15
15
  },
16
16
  "license": "Apache-2.0",
17
17
  "scripts": {
18
- "build": "tsc --project tsconfig.src.json",
19
- "lint": "eslint . && tsc --project tsconfig.src.json --noEmit && tsc --project tsconfig.tests.json --noEmit",
20
- "lint-fix": "eslint . --fix",
21
- "watch": "tsc --project tsconfig.src.json --watch",
22
- "clean": "rm -rf ./lib",
23
- "test": "playwright test",
24
- "ctest": "playwright test --project=copilot",
25
- "deploy": "npm run clean && npm run build && npm publish"
26
18
  },
27
19
  "exports": {
28
20
  ".": {
@@ -30,30 +22,5 @@
30
22
  "require": "./index.js",
31
23
  "types": "./index.d.ts"
32
24
  }
33
- },
34
- "devDependencies": {
35
- "@anthropic-ai/sdk": "^0.70.0",
36
- "@eslint/eslintrc": "^3.2.0",
37
- "@eslint/js": "^9.19.0",
38
- "@google/generative-ai": "^0.24.1",
39
- "@modelcontextprotocol/sdk": "^1.17.5",
40
- "@playwright/test": "^1.56.1",
41
- "@stylistic/eslint-plugin": "^3.0.1",
42
- "@types/colors": "^1.1.3",
43
- "@types/debug": "^4.1.12",
44
- "@types/dotenv": "^6.1.1",
45
- "@types/glob": "^8.1.0",
46
- "@types/node": "^22.13.10",
47
- "@typescript-eslint/eslint-plugin": "^8.26.1",
48
- "@typescript-eslint/parser": "^8.26.1",
49
- "@typescript-eslint/utils": "^8.26.1",
50
- "colors": "^1.4.0",
51
- "debug": "^4.4.3",
52
- "dotenv": "^17.2.3",
53
- "eslint": "^9.19.0",
54
- "eslint-plugin-import": "^2.31.0",
55
- "eslint-plugin-notice": "^1.0.0",
56
- "openai": "^6.9.1",
57
- "typescript": "^5.8.2"
58
25
  }
59
26
  }
@@ -1,174 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Microsoft Corporation.
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.OpenAICompletions = void 0;
19
- class OpenAICompletions {
20
- name = 'openai';
21
- _endpoint;
22
- async endpoint() {
23
- if (!this._endpoint)
24
- this._endpoint = await this.connect();
25
- return this._endpoint;
26
- }
27
- async connect() {
28
- return {
29
- baseUrl: 'https://api.openai.com/v1',
30
- apiKey: process.env.OPENAI_API_KEY,
31
- headers: {}
32
- };
33
- }
34
- async complete(conversation, options) {
35
- // Convert generic messages to OpenAI format
36
- const systemMessage = {
37
- role: 'system',
38
- content: systemPrompt(conversation.systemPrompt)
39
- };
40
- const openaiMessages = [systemMessage, ...conversation.messages.map(toOpenAIMessage)];
41
- const openaiTools = conversation.tools.map(t => toOpenAITool(t, options));
42
- const endpoint = await this.endpoint();
43
- const response = await create({
44
- model: options.model,
45
- max_tokens: options.maxTokens,
46
- temperature: options.temperature,
47
- messages: openaiMessages,
48
- tools: openaiTools,
49
- tool_choice: conversation.tools.length > 0 ? 'auto' : undefined,
50
- reasoning_effort: options.reasoning ? 'medium' : undefined,
51
- }, endpoint);
52
- const result = { role: 'assistant', content: [] };
53
- const message = response.choices[0].message;
54
- if (message.content)
55
- result.content.push({ type: 'text', text: message.content });
56
- result.content.push(...(message.tool_calls || []).map(toToolCall));
57
- const usage = {
58
- input: response.usage?.prompt_tokens ?? 0,
59
- output: response.usage?.completion_tokens ?? 0,
60
- };
61
- return { result, usage };
62
- }
63
- }
64
- exports.OpenAICompletions = OpenAICompletions;
65
- async function create(body, endpoint) {
66
- const headers = {
67
- 'Content-Type': 'application/json',
68
- 'Authorization': `Bearer ${endpoint.apiKey}`,
69
- 'Copilot-Vision-Request': 'true',
70
- ...endpoint.headers
71
- };
72
- const response = await fetch(`${endpoint.baseUrl}/chat/completions`, {
73
- method: 'POST',
74
- headers,
75
- body: JSON.stringify(body)
76
- });
77
- if (!response.ok)
78
- throw new Error(`API error: ${response.status} ${response.statusText} ${await response.text()}`);
79
- return await response.json();
80
- }
81
- function toOpenAIResultContentPart(part) {
82
- if (part.type === 'text') {
83
- return {
84
- type: 'text',
85
- text: part.text,
86
- };
87
- }
88
- if (part.type === 'image') {
89
- return {
90
- type: 'image_url',
91
- image_url: {
92
- url: `data:${part.mimeType};base64,${part.data}`,
93
- },
94
- };
95
- }
96
- throw new Error(`Cannot convert content part of type ${part.type} to text content part`);
97
- }
98
- function toOpenAIMessage(message) {
99
- if (message.role === 'user') {
100
- return {
101
- role: 'user',
102
- content: message.content
103
- };
104
- }
105
- if (message.role === 'assistant') {
106
- const assistantMessage = {
107
- role: 'assistant'
108
- };
109
- const textParts = message.content.filter(part => part.type === 'text');
110
- const toolCallParts = message.content.filter(part => part.type === 'tool_call');
111
- if (textParts.length === 1)
112
- assistantMessage.content = textParts[0].text;
113
- else
114
- assistantMessage.content = textParts;
115
- const toolCalls = [];
116
- for (const toolCall of toolCallParts) {
117
- toolCalls.push({
118
- id: toolCall.id,
119
- type: 'function',
120
- function: {
121
- name: toolCall.name,
122
- arguments: JSON.stringify(toolCall.arguments)
123
- }
124
- });
125
- }
126
- if (toolCalls.length > 0)
127
- assistantMessage.tool_calls = toolCalls;
128
- return assistantMessage;
129
- }
130
- if (message.role === 'tool_result') {
131
- return {
132
- role: 'tool',
133
- tool_call_id: message.toolCallId,
134
- content: message.result.content.map(toOpenAIResultContentPart),
135
- };
136
- }
137
- throw new Error(`Unsupported message role: ${message.role}`);
138
- }
139
- function toOpenAITool(tool, options) {
140
- const parameters = { ...tool.inputSchema };
141
- if (options.injectIntent) {
142
- parameters.properties = {
143
- _intent: { type: 'string', description: 'Describe the intent of this tool call' },
144
- ...parameters.properties || {},
145
- };
146
- }
147
- return {
148
- type: 'function',
149
- function: {
150
- name: tool.name,
151
- description: tool.description,
152
- parameters,
153
- },
154
- };
155
- }
156
- function toToolCall(toolCall) {
157
- return {
158
- type: 'tool_call',
159
- name: toolCall.type === 'function' ? toolCall.function.name : toolCall.custom.name,
160
- arguments: JSON.parse(toolCall.type === 'function' ? toolCall.function.arguments : toolCall.custom.input),
161
- id: toolCall.id,
162
- };
163
- }
164
- const systemPrompt = (prompt) => `
165
- ### System instructions
166
-
167
- ${prompt}
168
-
169
- ### Tool calling instructions
170
- - Your reply MUST be a tool call and nothing but the tool call.
171
- - NEVER respond with text content, only tool calls.
172
- - Do NOT describe your plan, do NOT explain what you are doing, do NOT describe what you see, call tools.
173
- - Provide thoughts in the '_intent' property of the tool calls instead.
174
- `;