mirror of
https://github.com/openai/codex.git
synced 2026-05-02 04:11:39 +03:00
python-sdk: split stream and control examples (2026-03-16)
- make 03_turn_stream_events a clean streaming example with curated event output - add 14_turn_controls as a separate steer and interrupt demo with concise summaries - update the notebook and runtime-backed example assertions to match the new example shapes Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -509,33 +509,58 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Cell 10: async stream + steer + interrupt (best effort)\n",
|
||||
"# Cell 10: async turn controls (best effort steer + interrupt)\n",
|
||||
"import asyncio\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"async def async_stream_demo():\n",
|
||||
" async with AsyncCodex() as codex:\n",
|
||||
" thread = await codex.thread_start(model='gpt-5.4', config={'model_reasoning_effort': 'high'})\n",
|
||||
" turn = await thread.turn(TextInput('Count from 1 to 200 with commas, then one summary sentence.'))\n",
|
||||
" steer_turn = await thread.turn(TextInput('Count from 1 to 40 with commas, then one summary sentence.'))\n",
|
||||
"\n",
|
||||
" steer_result = 'sent'\n",
|
||||
" try:\n",
|
||||
" _ = await turn.steer(TextInput('Keep it brief and stop after 20 numbers.'))\n",
|
||||
" print('steer: sent')\n",
|
||||
" _ = await steer_turn.steer(TextInput('Keep it brief and stop after 10 numbers.'))\n",
|
||||
" except Exception as e:\n",
|
||||
" print('steer: skipped', type(e).__name__)\n",
|
||||
" steer_result = f'skipped {type(e).__name__}'\n",
|
||||
"\n",
|
||||
" steer_event_count = 0\n",
|
||||
" steer_completed_status = 'unknown'\n",
|
||||
" steer_completed_turn = None\n",
|
||||
" async for event in steer_turn.stream():\n",
|
||||
" steer_event_count += 1\n",
|
||||
" if event.method == 'turn/completed':\n",
|
||||
" steer_completed_turn = event.payload.turn\n",
|
||||
" steer_completed_status = getattr(event.payload.turn.status, 'value', str(event.payload.turn.status))\n",
|
||||
"\n",
|
||||
" steer_preview = assistant_text_from_turn(steer_completed_turn).strip() or '[no assistant text]'\n",
|
||||
"\n",
|
||||
" interrupt_turn = await thread.turn(TextInput('Count from 1 to 200 with commas, then one summary sentence.'))\n",
|
||||
" interrupt_result = 'sent'\n",
|
||||
" try:\n",
|
||||
" _ = await turn.interrupt()\n",
|
||||
" print('interrupt: sent')\n",
|
||||
" _ = await interrupt_turn.interrupt()\n",
|
||||
" except Exception as e:\n",
|
||||
" print('interrupt: skipped', type(e).__name__)\n",
|
||||
" interrupt_result = f'skipped {type(e).__name__}'\n",
|
||||
"\n",
|
||||
" event_count = 0\n",
|
||||
" async for event in turn.stream():\n",
|
||||
" event_count += 1\n",
|
||||
" print(event.method, event.payload)\n",
|
||||
" interrupt_event_count = 0\n",
|
||||
" interrupt_completed_status = 'unknown'\n",
|
||||
" interrupt_completed_turn = None\n",
|
||||
" async for event in interrupt_turn.stream():\n",
|
||||
" interrupt_event_count += 1\n",
|
||||
" if event.method == 'turn/completed':\n",
|
||||
" interrupt_completed_turn = event.payload.turn\n",
|
||||
" interrupt_completed_status = getattr(event.payload.turn.status, 'value', str(event.payload.turn.status))\n",
|
||||
"\n",
|
||||
" print('events.count:', event_count)\n",
|
||||
" interrupt_preview = assistant_text_from_turn(interrupt_completed_turn).strip() or '[no assistant text]'\n",
|
||||
"\n",
|
||||
" print('steer.result:', steer_result)\n",
|
||||
" print('steer.final.status:', steer_completed_status)\n",
|
||||
" print('steer.events.count:', steer_event_count)\n",
|
||||
" print('steer.assistant.preview:', steer_preview)\n",
|
||||
" print('interrupt.result:', interrupt_result)\n",
|
||||
" print('interrupt.final.status:', interrupt_completed_status)\n",
|
||||
" print('interrupt.events.count:', interrupt_event_count)\n",
|
||||
" print('interrupt.assistant.preview:', interrupt_preview)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"await async_stream_demo()\n"
|
||||
|
||||
Reference in New Issue
Block a user