serial-created.mjs 823 B

123456789101112131415161718192021222324
  1. /** Awaited creation context shared by TypeScript and Python SDK snapshots. */
  2. import { randomUUID } from 'node:crypto'
  3. import { setImmediate } from 'node:timers/promises'
  4. export const name = 'serial-created-fixture'
  5. export const inject = ['agents']
  6. /** Install ordered creation listeners whose context must precede the first turn. */
  7. export function apply(ctx) {
  8. const ready = new WeakSet()
  9. ctx.on('agent/created', async ({ agent }) => {
  10. await setImmediate()
  11. agent.inject({
  12. id: randomUUID(),
  13. role: 'user',
  14. content: [{ type: 'text', text: 'Serial agent creation completed.' }],
  15. source: { kind: 'plugin', plugin: name },
  16. })
  17. ready.add(agent)
  18. })
  19. ctx.on('agent/created', ({ agent }) => {
  20. if (!ready.has(agent)) throw new Error('creation listeners ran out of order')
  21. })
  22. }