@koi-language/koi 1.0.6 → 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.
Files changed (113) hide show
  1. package/README.md +4 -125
  2. package/examples/.build/agent-dialogue.ts +138 -0
  3. package/examples/.build/agent-dialogue.ts.map +1 -0
  4. package/examples/.build/chess.ts +77 -0
  5. package/examples/.build/chess.ts.map +1 -0
  6. package/examples/.build/delegation-test.ts +140 -0
  7. package/examples/.build/delegation-test.ts.map +1 -0
  8. package/examples/.build/dialog-demo.ts +77 -0
  9. package/examples/.build/dialog-demo.ts.map +1 -0
  10. package/examples/.build/hello-world.ts +77 -0
  11. package/examples/.build/hello-world.ts.map +1 -0
  12. package/examples/.build/lover-dialog-demo.ts +77 -0
  13. package/examples/.build/lover-dialog-demo.ts.map +1 -0
  14. package/examples/.build/package.json +3 -0
  15. package/examples/.build/registry-interactive-demo.ts +202 -0
  16. package/examples/.build/registry-interactive-demo.ts.map +1 -0
  17. package/examples/.build/registry-playbook-demo.ts +201 -0
  18. package/examples/.build/registry-playbook-demo.ts.map +1 -0
  19. package/examples/.build/tic-tac-toe.ts +77 -0
  20. package/examples/.build/tic-tac-toe.ts.map +1 -0
  21. package/examples/actions-demo.koi +8 -9
  22. package/examples/activists-dialogue.koi +75 -0
  23. package/examples/agent-dialogue.koi +66 -0
  24. package/examples/chess.koi +19 -0
  25. package/examples/counter.koi +20 -69
  26. package/examples/delegation-test.koi +16 -18
  27. package/examples/dialog-demo.koi +20 -0
  28. package/examples/hello-world.koi +7 -43
  29. package/examples/mcp-stdio-demo.koi +29 -0
  30. package/examples/memory-test.koi +49 -0
  31. package/examples/mobile-mcp-demo.koi +32 -0
  32. package/examples/multi-event-handler-test.koi +16 -18
  33. package/examples/pipeline.koi +15 -17
  34. package/examples/prompt-demo.koi +20 -0
  35. package/examples/{registry-playbook-email-compositor.koi → registry-interactive-demo.koi} +27 -27
  36. package/examples/registry-playbook-demo.koi +28 -28
  37. package/examples/skill-import-test.koi +7 -9
  38. package/examples/skills/.build/math-operations.ts +1656 -0
  39. package/examples/skills/.build/math-operations.ts.map +1 -0
  40. package/examples/skills/.build/package.json +3 -0
  41. package/examples/skills/.build/string-operations.ts +1643 -0
  42. package/examples/skills/.build/string-operations.ts.map +1 -0
  43. package/examples/skills/advanced/.build/index.ts +3223 -0
  44. package/examples/skills/advanced/.build/index.ts.map +1 -0
  45. package/examples/skills/advanced/.build/package.json +3 -0
  46. package/examples/skills/advanced/index.koi +3 -5
  47. package/examples/skills/math-operations.koi +1 -3
  48. package/examples/skills/string-operations.koi +1 -3
  49. package/examples/tic-tac-toe.koi +19 -0
  50. package/examples/utils/echo-mcp-server.js +141 -0
  51. package/examples/web-delegation-demo.koi +15 -17
  52. package/package.json +2 -1
  53. package/src/cli/koi.js +30 -41
  54. package/src/compiler/build-optimizer.js +204 -289
  55. package/src/compiler/cache-manager.js +1 -1
  56. package/src/compiler/import-resolver.js +5 -9
  57. package/src/compiler/parser.js +6072 -3476
  58. package/src/compiler/transpiler.js +346 -38
  59. package/src/grammar/koi.pegjs +302 -62
  60. package/src/runtime/actions/{format.js → call-llm.js} +37 -44
  61. package/src/runtime/actions/call-mcp.js +97 -0
  62. package/src/runtime/actions/if.js +179 -0
  63. package/src/runtime/actions/print.js +3 -1
  64. package/src/runtime/actions/prompt-user.js +75 -0
  65. package/src/runtime/actions/repeat.js +147 -0
  66. package/src/runtime/actions/shell.js +185 -0
  67. package/src/runtime/actions/while.js +205 -0
  68. package/src/runtime/agent.js +592 -178
  69. package/src/runtime/cli-display.js +26 -0
  70. package/src/runtime/cli-input.js +421 -0
  71. package/src/runtime/cli-logger.js +2 -5
  72. package/src/runtime/cli-markdown.js +61 -0
  73. package/src/runtime/cli-select.js +106 -0
  74. package/src/runtime/incremental-json-parser.js +27 -17
  75. package/src/runtime/index.js +1 -0
  76. package/src/runtime/llm-provider.js +1083 -572
  77. package/src/runtime/mcp-registry.js +141 -0
  78. package/src/runtime/mcp-stdio-client.js +334 -0
  79. package/src/runtime/planner.js +1 -1
  80. package/src/runtime/playbook-session.js +259 -0
  81. package/src/runtime/registry-backends/keyv-sqlite.js +1 -1
  82. package/src/runtime/registry-backends/local.js +1 -1
  83. package/src/runtime/router.js +22 -26
  84. package/src/runtime/runtime.js +7 -1
  85. package/examples/cache-test.koi +0 -29
  86. package/examples/calculator.koi +0 -61
  87. package/examples/clear-registry.js +0 -33
  88. package/examples/clear-registry.koi +0 -30
  89. package/examples/code-introspection-test.koi +0 -149
  90. package/examples/directory-import-test.koi +0 -84
  91. package/examples/hello-world-claude.koi +0 -52
  92. package/examples/hello.koi +0 -24
  93. package/examples/mcp-example.koi +0 -70
  94. package/examples/new-import-test.koi +0 -89
  95. package/examples/registry-demo.koi +0 -184
  96. package/examples/registry-playbook-email-compositor-2.koi +0 -140
  97. package/examples/sentiment.koi +0 -90
  98. package/examples/simple.koi +0 -48
  99. package/examples/task-chaining-demo.koi +0 -244
  100. package/examples/test-await.koi +0 -22
  101. package/examples/test-crypto-sha256.koi +0 -196
  102. package/examples/test-delegation.koi +0 -41
  103. package/examples/test-multi-team-routing.koi +0 -258
  104. package/examples/test-no-handler.koi +0 -35
  105. package/examples/test-npm-import.koi +0 -67
  106. package/examples/test-parse.koi +0 -10
  107. package/examples/test-peers-with-team.koi +0 -59
  108. package/examples/test-permissions-fail.koi +0 -20
  109. package/examples/test-permissions.koi +0 -36
  110. package/examples/test-simple-registry.koi +0 -31
  111. package/examples/test-typescript-import.koi +0 -64
  112. package/examples/test-uses-team-syntax.koi +0 -25
  113. 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
- // Complete action object found!
91
- const actionJSON = this.buffer.substring(objectStart, i + 1);
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 = i + 1;
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 incomplete, wait for more data
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 ${objectStart}: ${e.message}`);
110
+ console.error(`[IncrementalParser] ⚠️ Failed to parse object at ${this.pendingObjectStart}: ${e.message}`);
105
111
  }
106
112
  }
107
- objectStart = -1;
113
+
114
+ this.pendingObjectEnd = -1;
115
+ this.pendingObjectStart = -1;
108
116
  }
109
- } else if (char === ']' && depth === 0) {
110
- // End of actions array
111
- if (process.env.KOI_DEBUG_LLM) {
112
- console.error(`[IncrementalParser] 🏁 End of actions array reached`);
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
 
@@ -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 = {