@koi-language/koi 1.0.5 → 1.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/README.md +4 -125
- package/examples/.build/agent-dialogue.ts +138 -0
- package/examples/.build/agent-dialogue.ts.map +1 -0
- package/examples/.build/chess.ts +77 -0
- package/examples/.build/chess.ts.map +1 -0
- package/examples/.build/delegation-test.ts +140 -0
- package/examples/.build/delegation-test.ts.map +1 -0
- package/examples/.build/dialog-demo.ts +77 -0
- package/examples/.build/dialog-demo.ts.map +1 -0
- package/examples/.build/hello-world.ts +77 -0
- package/examples/.build/hello-world.ts.map +1 -0
- package/examples/.build/lover-dialog-demo.ts +77 -0
- package/examples/.build/lover-dialog-demo.ts.map +1 -0
- package/examples/.build/package.json +3 -0
- package/examples/.build/registry-interactive-demo.ts +202 -0
- package/examples/.build/registry-interactive-demo.ts.map +1 -0
- package/examples/.build/registry-playbook-demo.ts +201 -0
- package/examples/.build/registry-playbook-demo.ts.map +1 -0
- package/examples/.build/tic-tac-toe.ts +77 -0
- package/examples/.build/tic-tac-toe.ts.map +1 -0
- package/examples/actions-demo.koi +8 -9
- package/examples/activists-dialogue.koi +75 -0
- package/examples/agent-dialogue.koi +66 -0
- package/examples/chess.koi +19 -0
- package/examples/counter.koi +20 -69
- package/examples/delegation-test.koi +16 -18
- package/examples/dialog-demo.koi +20 -0
- package/examples/hello-world.koi +7 -43
- package/examples/mcp-stdio-demo.koi +29 -0
- package/examples/memory-test.koi +49 -0
- package/examples/mobile-mcp-demo.koi +32 -0
- package/examples/multi-event-handler-test.koi +16 -18
- package/examples/pipeline.koi +15 -17
- package/examples/prompt-demo.koi +20 -0
- package/examples/{registry-playbook-email-compositor.koi → registry-interactive-demo.koi} +27 -27
- package/examples/registry-playbook-demo.koi +28 -28
- package/examples/skill-import-test.koi +7 -9
- package/examples/skills/.build/math-operations.ts +1656 -0
- package/examples/skills/.build/math-operations.ts.map +1 -0
- package/examples/skills/.build/package.json +3 -0
- package/examples/skills/.build/string-operations.ts +1643 -0
- package/examples/skills/.build/string-operations.ts.map +1 -0
- package/examples/skills/advanced/.build/index.ts +3223 -0
- package/examples/skills/advanced/.build/index.ts.map +1 -0
- package/examples/skills/advanced/.build/package.json +3 -0
- package/examples/skills/advanced/index.koi +3 -5
- package/examples/skills/math-operations.koi +1 -3
- package/examples/skills/string-operations.koi +1 -3
- package/examples/tic-tac-toe.koi +19 -0
- package/examples/utils/echo-mcp-server.js +141 -0
- package/examples/web-delegation-demo.koi +15 -17
- package/package.json +2 -1
- package/src/cli/koi.js +30 -41
- package/src/compiler/build-optimizer.js +204 -289
- package/src/compiler/cache-manager.js +1 -1
- package/src/compiler/import-resolver.js +5 -9
- package/src/compiler/parser.js +6072 -3476
- package/src/compiler/transpiler.js +346 -38
- package/src/grammar/koi.pegjs +302 -62
- package/src/runtime/actions/{format.js → call-llm.js} +37 -44
- package/src/runtime/actions/call-mcp.js +97 -0
- package/src/runtime/actions/if.js +179 -0
- package/src/runtime/actions/print.js +3 -1
- package/src/runtime/actions/prompt-user.js +75 -0
- package/src/runtime/actions/repeat.js +147 -0
- package/src/runtime/actions/shell.js +185 -0
- package/src/runtime/actions/while.js +205 -0
- package/src/runtime/agent.js +592 -178
- package/src/runtime/cli-display.js +26 -0
- package/src/runtime/cli-input.js +421 -0
- package/src/runtime/cli-logger.js +2 -5
- package/src/runtime/cli-markdown.js +61 -0
- package/src/runtime/cli-select.js +106 -0
- package/src/runtime/incremental-json-parser.js +51 -17
- package/src/runtime/index.js +1 -0
- package/src/runtime/llm-provider.js +1083 -572
- package/src/runtime/mcp-registry.js +141 -0
- package/src/runtime/mcp-stdio-client.js +334 -0
- package/src/runtime/planner.js +1 -1
- package/src/runtime/playbook-session.js +259 -0
- package/src/runtime/registry-backends/keyv-sqlite.js +1 -1
- package/src/runtime/registry-backends/local.js +1 -1
- package/src/runtime/router.js +22 -26
- package/src/runtime/runtime.js +7 -1
- package/examples/cache-test.koi +0 -29
- package/examples/calculator.koi +0 -61
- package/examples/clear-registry.js +0 -33
- package/examples/clear-registry.koi +0 -30
- package/examples/code-introspection-test.koi +0 -149
- package/examples/directory-import-test.koi +0 -84
- package/examples/hello-world-claude.koi +0 -52
- package/examples/hello.koi +0 -24
- package/examples/mcp-example.koi +0 -70
- package/examples/new-import-test.koi +0 -89
- package/examples/registry-demo.koi +0 -184
- package/examples/registry-playbook-email-compositor-2.koi +0 -140
- package/examples/sentiment.koi +0 -90
- package/examples/simple.koi +0 -48
- package/examples/task-chaining-demo.koi +0 -244
- package/examples/test-await.koi +0 -22
- package/examples/test-crypto-sha256.koi +0 -196
- package/examples/test-delegation.koi +0 -41
- package/examples/test-multi-team-routing.koi +0 -258
- package/examples/test-no-handler.koi +0 -35
- package/examples/test-npm-import.koi +0 -67
- package/examples/test-parse.koi +0 -10
- package/examples/test-peers-with-team.koi +0 -59
- package/examples/test-permissions-fail.koi +0 -20
- package/examples/test-permissions.koi +0 -36
- package/examples/test-simple-registry.koi +0 -31
- package/examples/test-typescript-import.koi +0 -64
- package/examples/test-uses-team-syntax.koi +0 -25
- package/examples/test-uses-team.koi +0 -31
|
@@ -11,6 +11,8 @@ export class IncrementalJSONParser {
|
|
|
11
11
|
this.actionsStartIndex = -1; // Position where "actions":[ was found
|
|
12
12
|
this.lastParsedIndex = 0; // Last position we successfully parsed up to
|
|
13
13
|
this.parsedActions = 0; // Count of actions we've parsed
|
|
14
|
+
this.pendingObjectEnd = -1; // Position where a potential complete object ended (} char)
|
|
15
|
+
this.pendingObjectStart = -1; // Start position of the pending object
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
/**
|
|
@@ -36,11 +38,6 @@ export class IncrementalJSONParser {
|
|
|
36
38
|
}
|
|
37
39
|
} else {
|
|
38
40
|
// Haven't found actions array yet, wait for more data
|
|
39
|
-
if (process.env.KOI_DEBUG_LLM && this.buffer.length > 50) {
|
|
40
|
-
// Show first 100 chars of buffer to debug what LLM is generating
|
|
41
|
-
const preview = this.buffer.substring(0, 100).replace(/\n/g, '\\n');
|
|
42
|
-
console.error(`[IncrementalParser] ⏳ Waiting for "actions" array (buffer: ${this.buffer.length} chars, preview: "${preview}...")`);
|
|
43
|
-
}
|
|
44
41
|
return actions;
|
|
45
42
|
}
|
|
46
43
|
}
|
|
@@ -87,31 +84,44 @@ export class IncrementalJSONParser {
|
|
|
87
84
|
} else if (char === '}') {
|
|
88
85
|
depth--;
|
|
89
86
|
if (depth === 0 && objectStart !== -1) {
|
|
90
|
-
//
|
|
91
|
-
|
|
87
|
+
// Potential complete object - save position but wait for delimiter
|
|
88
|
+
this.pendingObjectEnd = i;
|
|
89
|
+
this.pendingObjectStart = objectStart;
|
|
90
|
+
objectStart = -1;
|
|
91
|
+
}
|
|
92
|
+
} else if ((char === ',' || char === ']') && depth === 0) {
|
|
93
|
+
// Delimiter found - if we have a pending object, parse it now
|
|
94
|
+
if (this.pendingObjectEnd !== -1) {
|
|
95
|
+
const actionJSON = this.buffer.substring(this.pendingObjectStart, this.pendingObjectEnd + 1);
|
|
96
|
+
|
|
92
97
|
try {
|
|
93
98
|
const action = JSON.parse(actionJSON);
|
|
99
|
+
|
|
94
100
|
actions.push(action);
|
|
95
101
|
this.parsedActions++;
|
|
96
|
-
this.lastParsedIndex =
|
|
102
|
+
this.lastParsedIndex = this.pendingObjectEnd + 1;
|
|
97
103
|
|
|
98
104
|
if (process.env.KOI_DEBUG_LLM) {
|
|
99
|
-
console.error(`[IncrementalParser] ✅ Parsed action #${this.parsedActions}: ${action.intent || action.type || 'unknown'}`);
|
|
105
|
+
console.error(`[IncrementalParser] ✅ Parsed action #${this.parsedActions}: ${action.intent || action.type || 'unknown'}${action.id ? ` (id: ${action.id})` : ''}`);
|
|
100
106
|
}
|
|
101
107
|
} catch (e) {
|
|
102
|
-
// Failed to parse - might be
|
|
108
|
+
// Failed to parse - might be malformed JSON
|
|
103
109
|
if (process.env.KOI_DEBUG_LLM) {
|
|
104
|
-
console.error(`[IncrementalParser] ⚠️ Failed to parse object at ${
|
|
110
|
+
console.error(`[IncrementalParser] ⚠️ Failed to parse object at ${this.pendingObjectStart}: ${e.message}`);
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
|
-
|
|
113
|
+
|
|
114
|
+
this.pendingObjectEnd = -1;
|
|
115
|
+
this.pendingObjectStart = -1;
|
|
108
116
|
}
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
if (
|
|
112
|
-
|
|
117
|
+
|
|
118
|
+
// If this is the end of the array, stop parsing
|
|
119
|
+
if (char === ']') {
|
|
120
|
+
if (process.env.KOI_DEBUG_LLM) {
|
|
121
|
+
console.error(`[IncrementalParser] 🏁 End of actions array reached`);
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
113
124
|
}
|
|
114
|
-
break;
|
|
115
125
|
}
|
|
116
126
|
}
|
|
117
127
|
|
|
@@ -136,6 +146,30 @@ export class IncrementalJSONParser {
|
|
|
136
146
|
actions.push(...objects);
|
|
137
147
|
}
|
|
138
148
|
|
|
149
|
+
// If no "actions" array was found, the LLM returned raw JSON
|
|
150
|
+
// Wrap it in a return action so it can be executed properly
|
|
151
|
+
if (this.actionsStartIndex === -1 && this.buffer.length > 0) {
|
|
152
|
+
try {
|
|
153
|
+
const rawJSON = JSON.parse(this.buffer.trim());
|
|
154
|
+
// Check if it's not already an action (has actionType or intent)
|
|
155
|
+
if (!rawJSON.actionType && !rawJSON.actions) {
|
|
156
|
+
if (process.env.KOI_DEBUG_LLM) {
|
|
157
|
+
console.error(`[IncrementalParser] ⚠️ No "actions" array found - wrapping raw JSON in return action`);
|
|
158
|
+
}
|
|
159
|
+
return [{
|
|
160
|
+
actionType: 'direct',
|
|
161
|
+
intent: 'return',
|
|
162
|
+
data: rawJSON
|
|
163
|
+
}];
|
|
164
|
+
}
|
|
165
|
+
} catch (e) {
|
|
166
|
+
// Not valid JSON, return whatever objects we found
|
|
167
|
+
if (process.env.KOI_DEBUG_LLM) {
|
|
168
|
+
console.error(`[IncrementalParser] ⚠️ Failed to parse raw response as JSON: ${e.message}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
139
173
|
return actions;
|
|
140
174
|
}
|
|
141
175
|
|
package/src/runtime/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { MCPClient, mcpClient } from './mcp-client.js';
|
|
|
7
7
|
export { Planner, PlanningAgent } from './planner.js';
|
|
8
8
|
export { SkillSelector, skillSelector } from './skill-selector.js';
|
|
9
9
|
export { registry, getRegistry } from './registry.js';
|
|
10
|
+
export { mcpRegistry } from './mcp-registry.js';
|
|
10
11
|
|
|
11
12
|
// Global registry for skill functions (for tool calling)
|
|
12
13
|
export const SkillRegistry = {
|