@soederpop/luca 0.2.2 → 0.2.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.
@@ -1,7 +1,7 @@
1
1
  import { setBuildTimeData, setContainerBuildTimeData } from './index.js';
2
2
 
3
3
  // Auto-generated introspection registry data
4
- // Generated at: 2026-04-06T20:39:08.573Z
4
+ // Generated at: 2026-04-09T05:21:42.356Z
5
5
 
6
6
  setBuildTimeData('features.containerLink', {
7
7
  "id": "features.containerLink",
@@ -1,5 +1,5 @@
1
1
  // Auto-generated Python bridge script
2
- // Generated at: 2026-04-06T20:39:11.189Z
2
+ // Generated at: 2026-04-09T05:21:45.027Z
3
3
  // Source: src/python/bridge.py
4
4
  //
5
5
  // Do not edit manually. Run: luca build-python-bridge
@@ -1,5 +1,5 @@
1
1
  // Auto-generated scaffold and MCP readme content
2
- // Generated at: 2026-04-06T20:39:09.519Z
2
+ // Generated at: 2026-04-09T05:21:43.320Z
3
3
  // Source: docs/scaffolds/*.md, docs/examples/assistant/, and docs/mcp/readme.md
4
4
  //
5
5
  // Do not edit manually. Run: luca build-scaffolds
@@ -0,0 +1,306 @@
1
+ import { describe, it, expect, beforeEach } from 'bun:test'
2
+ import { AGIContainer } from '../src/agi/container.server'
3
+ import type { Assistant } from '../src/agi/features/assistant'
4
+
5
+ /**
6
+ * Helper: create a runtime assistant with no folder, stub the conversation
7
+ * so tests never hit the network, and inject hook functions directly.
8
+ */
9
+ function makeAssistant(hooks: Record<string, (...args: any[]) => any> = {}) {
10
+ const container = new AGIContainer()
11
+ const assistant = container.feature('assistant', {
12
+ systemPrompt: 'You are a test assistant.',
13
+ model: 'gpt-5',
14
+ }) as Assistant
15
+
16
+ // Inject hooks into state (same shape loadHooks() produces)
17
+ assistant.state.set('hooks', hooks)
18
+
19
+ return assistant
20
+ }
21
+
22
+ /** Start + stub so ask() never touches the network */
23
+ async function startWithStub(assistant: Assistant, pattern: string | RegExp = /.*/, response: string = 'stubbed') {
24
+ await assistant.start()
25
+ assistant.conversation.stub(pattern, response)
26
+ return assistant
27
+ }
28
+
29
+ describe('Assistant triggerHook', () => {
30
+ it('returns undefined when no hook exists for the name', async () => {
31
+ const assistant = makeAssistant()
32
+ const result = await assistant.triggerHook('nonexistent', 'arg1')
33
+ expect(result).toBeUndefined()
34
+ })
35
+
36
+ it('calls the hook with (assistant, ...args) and awaits it', async () => {
37
+ const calls: any[][] = []
38
+ const assistant = makeAssistant({
39
+ myHook: async (asst: any, a: string, b: number) => {
40
+ calls.push([asst, a, b])
41
+ return 'hook-result'
42
+ },
43
+ })
44
+
45
+ const result = await assistant.triggerHook('myHook', 'hello', 42)
46
+ expect(result).toBe('hook-result')
47
+ expect(calls).toHaveLength(1)
48
+ expect(calls[0]![0]).toBe(assistant)
49
+ expect(calls[0]![1]).toBe('hello')
50
+ expect(calls[0]![2]).toBe(42)
51
+ })
52
+
53
+ it('emits hookFired before calling the hook', async () => {
54
+ const events: string[] = []
55
+ const assistant = makeAssistant({
56
+ myHook: async () => { events.push('hook-ran') },
57
+ })
58
+ assistant.on('hookFired', (name: string) => { events.push(`hookFired:${name}`) })
59
+
60
+ await assistant.triggerHook('myHook')
61
+ expect(events).toEqual(['hookFired:myHook', 'hook-ran'])
62
+ })
63
+
64
+ it('awaits async hooks to completion before returning', async () => {
65
+ let resolved = false
66
+ const assistant = makeAssistant({
67
+ slowHook: async () => {
68
+ await new Promise(r => setTimeout(r, 50))
69
+ resolved = true
70
+ },
71
+ })
72
+
73
+ await assistant.triggerHook('slowHook')
74
+ expect(resolved).toBe(true)
75
+ })
76
+ })
77
+
78
+ describe('Assistant lifecycle hooks', () => {
79
+ it('beforeStart runs before the assistant is started', async () => {
80
+ const order: string[] = []
81
+ const assistant = makeAssistant({
82
+ beforeStart: async () => { order.push('beforeStart') },
83
+ started: async () => { order.push('started') },
84
+ afterStart: async () => { order.push('afterStart') },
85
+ })
86
+
87
+ await assistant.start()
88
+ expect(order).toEqual(['beforeStart', 'started', 'afterStart'])
89
+ })
90
+
91
+ it('afterStart blocks start() until complete', async () => {
92
+ let afterStartDone = false
93
+ const assistant = makeAssistant({
94
+ afterStart: async () => {
95
+ await new Promise(r => setTimeout(r, 50))
96
+ afterStartDone = true
97
+ },
98
+ })
99
+
100
+ await assistant.start()
101
+ expect(afterStartDone).toBe(true)
102
+ })
103
+
104
+ it('formatSystemPrompt modifies the system prompt before conversation creation', async () => {
105
+ const assistant = makeAssistant({
106
+ formatSystemPrompt: async (_asst: any, prompt: string) => {
107
+ return prompt + '\nExtra instructions.'
108
+ },
109
+ })
110
+
111
+ await assistant.start()
112
+ expect(assistant.systemPrompt).toContain('Extra instructions.')
113
+ })
114
+ })
115
+
116
+ describe('Assistant ask hooks', () => {
117
+ it('beforeAsk fires on every ask() call', async () => {
118
+ let callCount = 0
119
+ const assistant = makeAssistant({
120
+ beforeAsk: async () => { callCount++ },
121
+ })
122
+ await startWithStub(assistant)
123
+
124
+ await assistant.ask('first')
125
+ await assistant.ask('second')
126
+ expect(callCount).toBe(2)
127
+ })
128
+
129
+ it('beforeAsk can rewrite the question via return value', async () => {
130
+ const questionsReceived: string[] = []
131
+ const assistant = makeAssistant({
132
+ beforeAsk: async (_asst: any, question: string) => {
133
+ return question + ' (rewritten)'
134
+ },
135
+ })
136
+ await assistant.start()
137
+
138
+ // Stub that captures the actual question sent to the conversation
139
+ assistant.conversation.on('userMessage', (content: any) => {
140
+ questionsReceived.push(typeof content === 'string' ? content : JSON.stringify(content))
141
+ })
142
+ assistant.conversation.stub(/.*/, 'ok')
143
+
144
+ await assistant.ask('hello')
145
+ expect(questionsReceived[0]).toContain('(rewritten)')
146
+ })
147
+
148
+ it('beforeInitialAsk fires only on the first ask()', async () => {
149
+ let callCount = 0
150
+ const assistant = makeAssistant({
151
+ beforeInitialAsk: async () => { callCount++ },
152
+ })
153
+ await startWithStub(assistant)
154
+
155
+ await assistant.ask('first')
156
+ await assistant.ask('second')
157
+ await assistant.ask('third')
158
+ expect(callCount).toBe(1)
159
+ })
160
+
161
+ it('answered hook fires after the response is produced', async () => {
162
+ let hookResponse: string | undefined
163
+ const assistant = makeAssistant({
164
+ answered: async (_asst: any, result: string) => {
165
+ hookResponse = result
166
+ },
167
+ })
168
+ await startWithStub(assistant, /.*/, 'the answer')
169
+
170
+ await assistant.ask('question')
171
+ expect(hookResponse).toBe('the answer')
172
+ })
173
+
174
+ it('answered hook is awaited before ask() returns', async () => {
175
+ let hookDone = false
176
+ const assistant = makeAssistant({
177
+ answered: async () => {
178
+ await new Promise(r => setTimeout(r, 50))
179
+ hookDone = true
180
+ },
181
+ })
182
+ await startWithStub(assistant)
183
+
184
+ await assistant.ask('question')
185
+ expect(hookDone).toBe(true)
186
+ })
187
+ })
188
+
189
+ describe('Assistant tool hooks', () => {
190
+ it('beforeToolCall hook fires before tool execution', async () => {
191
+ const hookCalls: any[] = []
192
+ const assistant = makeAssistant({
193
+ beforeToolCall: async (_asst: any, ctx: any) => {
194
+ hookCalls.push({ name: ctx.name, args: { ...ctx.args } })
195
+ },
196
+ })
197
+ await assistant.start()
198
+
199
+ // Add a simple tool and stub a response that triggers it
200
+ assistant.addTool('echo', {
201
+ description: 'Echo tool',
202
+ parameters: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'] },
203
+ handler: async (args: any) => args.text,
204
+ })
205
+
206
+ // Directly invoke the toolExecutor to test hooks without needing model responses
207
+ const handler = async (args: any) => args.text
208
+ await assistant.conversation.toolExecutor!('echo', { text: 'hello' }, handler)
209
+
210
+ expect(hookCalls).toHaveLength(1)
211
+ expect(hookCalls[0]!.name).toBe('echo')
212
+ expect(hookCalls[0]!.args.text).toBe('hello')
213
+ })
214
+
215
+ it('afterToolCall hook fires after tool execution with result', async () => {
216
+ const hookCalls: any[] = []
217
+ const assistant = makeAssistant({
218
+ afterToolCall: async (_asst: any, ctx: any) => {
219
+ hookCalls.push({ name: ctx.name, result: ctx.result })
220
+ },
221
+ })
222
+ await assistant.start()
223
+
224
+ const handler = async (args: any) => args.text
225
+ await assistant.conversation.toolExecutor!('echo', { text: 'world' }, handler)
226
+
227
+ expect(hookCalls).toHaveLength(1)
228
+ expect(hookCalls[0]!.name).toBe('echo')
229
+ expect(hookCalls[0]!.result).toBe('world')
230
+ })
231
+
232
+ it('beforeToolCall can modify args via ctx mutation', async () => {
233
+ const assistant = makeAssistant({
234
+ beforeToolCall: async (_asst: any, ctx: any) => {
235
+ ctx.args.text = 'intercepted'
236
+ },
237
+ })
238
+ await assistant.start()
239
+
240
+ let receivedArgs: any
241
+ const handler = async (args: any) => { receivedArgs = args; return 'ok' }
242
+ await assistant.conversation.toolExecutor!('echo', { text: 'original' }, handler)
243
+
244
+ expect(receivedArgs.text).toBe('intercepted')
245
+ })
246
+
247
+ it('beforeToolCall can skip execution via ctx.skip', async () => {
248
+ const assistant = makeAssistant({
249
+ beforeToolCall: async (_asst: any, ctx: any) => {
250
+ ctx.skip = true
251
+ ctx.result = 'skipped-by-hook'
252
+ },
253
+ })
254
+ await assistant.start()
255
+
256
+ let handlerCalled = false
257
+ const handler = async () => { handlerCalled = true; return 'should not run' }
258
+ const result = await assistant.conversation.toolExecutor!('echo', { text: 'hi' }, handler)
259
+
260
+ expect(handlerCalled).toBe(false)
261
+ expect(result).toBe('skipped-by-hook')
262
+ })
263
+
264
+ it('afterToolCall can modify the result via ctx mutation', async () => {
265
+ const assistant = makeAssistant({
266
+ afterToolCall: async (_asst: any, ctx: any) => {
267
+ ctx.result = 'modified-result'
268
+ },
269
+ })
270
+ await assistant.start()
271
+
272
+ const handler = async () => 'original-result'
273
+ const result = await assistant.conversation.toolExecutor!('echo', {}, handler)
274
+
275
+ expect(result).toBe('modified-result')
276
+ })
277
+ })
278
+
279
+ describe('Assistant forwarded event hooks', () => {
280
+ it('turnStart hook fires before the turnStart event on the assistant bus', async () => {
281
+ const order: string[] = []
282
+ const assistant = makeAssistant({
283
+ turnStart: async () => { order.push('hook') },
284
+ })
285
+ await startWithStub(assistant)
286
+
287
+ assistant.on('turnStart', () => { order.push('event') })
288
+
289
+ await assistant.ask('go')
290
+ expect(order[0]).toBe('hook')
291
+ expect(order[1]).toBe('event')
292
+ })
293
+
294
+ it('response hook fires with the final response text', async () => {
295
+ let hookText: string | undefined
296
+ const assistant = makeAssistant({
297
+ response: async (_asst: any, text: string) => {
298
+ hookText = text
299
+ },
300
+ })
301
+ await startWithStub(assistant, /.*/, 'final answer')
302
+
303
+ await assistant.ask('question')
304
+ expect(hookText).toBe('final answer')
305
+ })
306
+ })
@@ -58,7 +58,7 @@ describe('Assistant', () => {
58
58
  model: 'qwen/qwen3-8b',
59
59
  })
60
60
 
61
- // bindHooksToEvents emits 'hookFired' with the event name each time a hook runs
61
+ // triggerHook emits 'hookFired' with the hook name each time a hook runs
62
62
  const fired: string[] = []
63
63
  assistant.on('hookFired', (eventName: string) => { fired.push(eventName) })
64
64