Repository navigation
feat: messages to the heavens #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f07c26e
Init messages to the heavens
gewenyu99 9782f7a
Tweaks for simplicity
gewenyu99 e4f5761
error handling duh
gewenyu99 98ecfbc
Matt's comments
gewenyu99 31ff2f7
tests
gewenyu99 ca6c9d2
Clean up the super messy flow for figuring out skillid
gewenyu99 d16d9f8
Merge branch 'main' into messages-to-the-heavens
gewenyu99 14ee794
fix: use of temp folders for windows (#417)
gewenyu99 6b73213
Merge branch 'main' into messages-to-the-heavens
gewenyu99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| import { TaskStreamPush } from '../task-stream-push'; | ||
| import { StreamEvent } from '../types'; | ||
| import type { TaskStreamDestination, TaskStreamUpdate } from '../types'; | ||
| import type { WizardStore, TaskItem } from '../../../ui/tui/store'; | ||
| import { RunPhase } from '../../wizard-session'; | ||
|
|
||
| // Mocks and stuff | ||
|
|
||
| type Listener = () => void; | ||
|
|
||
| function createMockStore(overrides: Partial<MockStoreState> = {}) { | ||
| const listeners: Listener[] = []; | ||
| const state: MockStoreState = { | ||
| runPhase: RunPhase.Idle, | ||
| skillId: 'test-skill', | ||
| tasks: [], | ||
| eventPlan: [], | ||
| ...overrides, | ||
| }; | ||
|
|
||
| const store = { | ||
| get session() { | ||
| return { runPhase: state.runPhase, skillId: state.skillId }; | ||
| }, | ||
| get tasks() { | ||
| return state.tasks; | ||
| }, | ||
| get eventPlan() { | ||
| return state.eventPlan; | ||
| }, | ||
| subscribe(cb: Listener) { | ||
| listeners.push(cb); | ||
| return () => { | ||
| const i = listeners.indexOf(cb); | ||
| if (i >= 0) listeners.splice(i, 1); | ||
| }; | ||
| }, | ||
| // mock setter and getter | ||
| _emit() { | ||
| for (const cb of listeners) cb(); | ||
| }, | ||
| _set(patch: Partial<MockStoreState>) { | ||
| Object.assign(state, patch); | ||
| }, | ||
| _listenerCount() { | ||
| return listeners.length; | ||
| }, | ||
| }; | ||
|
|
||
| return store as typeof store & WizardStore; | ||
| } | ||
|
|
||
| interface MockStoreState { | ||
| runPhase: RunPhase; | ||
| skillId: string | null; | ||
| tasks: TaskItem[]; | ||
| eventPlan: unknown[]; | ||
| } | ||
|
|
||
| function createMockDestination( | ||
| name = 'test', | ||
| ): TaskStreamDestination & { calls: Array<[StreamEvent, TaskStreamUpdate]> } { | ||
| const calls: Array<[StreamEvent, TaskStreamUpdate]> = []; | ||
| return { | ||
| name, | ||
| calls, | ||
| send: jest.fn((event: StreamEvent, payload: TaskStreamUpdate) => { | ||
| calls.push([event, payload]); | ||
| return Promise.resolve(); | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| function createPush( | ||
| store: ReturnType<typeof createMockStore>, | ||
| dest?: ReturnType<typeof createMockDestination>, | ||
| ) { | ||
| const d = dest ?? createMockDestination(); | ||
| const push = new TaskStreamPush({ | ||
| store, | ||
| workflowId: 'test-workflow', | ||
| destinations: [d], | ||
| }); | ||
| return { push, dest: d }; | ||
| } | ||
|
|
||
| describe('TaskStreamPush', () => { | ||
| describe('Coorect order of events', () => { | ||
| it('first push sends CREATE', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
|
|
||
| expect(dest.calls).toHaveLength(1); | ||
| expect(dest.calls[0][0]).toBe(StreamEvent.Create); | ||
| }); | ||
|
|
||
| it('subsequent pushes send UPDATE', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
| await push.push(); | ||
| await push.push(); | ||
|
|
||
| expect(dest.calls.map(([ev]) => ev)).toEqual([ | ||
| StreamEvent.Create, | ||
| StreamEvent.Update, | ||
| StreamEvent.Update, | ||
| ]); | ||
| }); | ||
|
|
||
| it('sends COMPLETE when runPhase is completed', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); // CREATE | ||
| store._set({ runPhase: RunPhase.Completed }); | ||
| await push.push(); | ||
|
|
||
| expect(dest.calls.map(([ev]) => ev)).toEqual([ | ||
| StreamEvent.Create, | ||
| StreamEvent.Complete, | ||
| ]); | ||
| }); | ||
|
|
||
| it('sends ERROR when runPhase is error', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); // CREATE | ||
| store._set({ runPhase: RunPhase.Error }); | ||
| await push.push(); | ||
|
|
||
| expect(dest.calls[1][0]).toBe(StreamEvent.Error); | ||
| }); | ||
|
|
||
| it('first push with terminal phase sends CREATE then COMPLETE on next', async () => { | ||
| const store = createMockStore({ runPhase: RunPhase.Completed }); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
| await push.push(); | ||
|
|
||
| expect(dest.calls.map(([ev]) => ev)).toEqual([ | ||
| StreamEvent.Create, | ||
| StreamEvent.Complete, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('payload contents', () => { | ||
| it('session_id is correctly formed', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
|
|
||
| const payload = dest.calls[0][1]; | ||
| expect(payload.workflow_id).toBe('test-workflow'); | ||
| expect(payload.skill_id).toBe('test-skill'); | ||
| expect(payload.session_id).toContain('test-workflow-test-skill-'); | ||
| }); | ||
|
|
||
| it('includes eventPlan when non-empty', async () => { | ||
| const plan = [{ name: 'signup', description: 'User signs up' }]; | ||
| const store = createMockStore({ eventPlan: plan }); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
|
|
||
| expect(dest.calls[0][1].event_plan).toEqual(plan); | ||
| }); | ||
|
|
||
| it('omits eventPlan when empty', async () => { | ||
| const store = createMockStore({ eventPlan: [] }); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
|
|
||
| expect(dest.calls[0][1].event_plan).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('destinations are independent', () => { | ||
| it('one destination failing does not break others', async () => { | ||
| const store = createMockStore(); | ||
| const good = createMockDestination('good'); | ||
| const bad: TaskStreamDestination = { | ||
| name: 'bad', | ||
| send: jest.fn(() => { | ||
| return Promise.reject(new Error('network down')); | ||
| }), | ||
| }; | ||
| const push = new TaskStreamPush({ | ||
| store, | ||
| workflowId: 'w', | ||
| destinations: [bad, good], | ||
| }); | ||
|
|
||
| // This should still happily complete and not block the wizard | ||
| await push.push(); | ||
|
|
||
| expect(bad.send).toHaveBeenCalledTimes(1); | ||
| expect(good.calls).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('push resolves even when all destinations fail', async () => { | ||
| const store = createMockStore(); | ||
| const bad: TaskStreamDestination = { | ||
| name: 'bad', | ||
| send: jest.fn(() => { | ||
| return Promise.reject(new Error('fail')); | ||
| }), | ||
| }; | ||
| const push = new TaskStreamPush({ | ||
| store, | ||
| workflowId: 'w', | ||
| destinations: [bad], | ||
| }); | ||
|
|
||
| await expect(push.push()).resolves.toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('dispose', () => { | ||
| it('sends a final push', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.dispose(); | ||
|
|
||
| expect(dest.calls).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('sends COMPLETE when runPhase is completed at dispose time', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
| store._set({ runPhase: RunPhase.Completed }); | ||
| await push.dispose(); | ||
|
|
||
| expect(dest.calls[1][0]).toBe(StreamEvent.Complete); | ||
| }); | ||
|
|
||
| it('sends ERROR when runPhase is error at dispose time', async () => { | ||
| const store = createMockStore(); | ||
| const { push, dest } = createPush(store); | ||
|
|
||
| await push.push(); | ||
| store._set({ runPhase: RunPhase.Error }); | ||
| await push.dispose(); | ||
|
|
||
| expect(dest.calls[1][0]).toBe(StreamEvent.Error); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { appendFileSync } from 'node:fs'; | ||
| import type { | ||
| TaskStreamDestination, | ||
| TaskStreamUpdate, | ||
| StreamEvent, | ||
| } from '../types'; | ||
| import { WIZARD_TASK_STREAM_LOG } from '../../../utils/paths'; | ||
|
|
||
| export class FileDestination implements TaskStreamDestination { | ||
| readonly name = 'file'; | ||
|
|
||
| send(event: StreamEvent, payload: TaskStreamUpdate): Promise<void> { | ||
| try { | ||
| appendFileSync( | ||
| WIZARD_TASK_STREAM_LOG, | ||
| `[${event}] ${JSON.stringify(payload, null, 2)}\n`, | ||
| ); | ||
| } catch { | ||
| // Non-critical | ||
| } | ||
| return Promise.resolve(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { | ||
| TaskStreamDestination, | ||
| TaskStreamUpdate, | ||
| StreamEvent, | ||
| } from '../types'; | ||
|
|
||
| export class PostHogDestination implements TaskStreamDestination { | ||
| readonly name = 'posthog'; | ||
|
|
||
| send(_event: StreamEvent, _payload: TaskStreamUpdate): Promise<void> { | ||
| // TODO: implement when the PostHog API surface is defined. | ||
| return Promise.resolve(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wire this up to send to where ever in the heavens (posthog app)