-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add reusable multi-turn agent kit #299
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
5 commits
Select commit
Hold shift + click to select a range
6f12048
feat: add reusable multi-turn agent kit
0674f85
fix: keep release notes in package order
4228e27
feat: bridge turn kit to agent assistant context
f51104a
fix: isolate conversation keys and order timestamps
8877720
fix: fail closed on turn isolation dependencies
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
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,18 @@ | ||
| # turn-kit multi-turn agent | ||
|
|
||
| Minimal runnable Relay channel agent demonstrating | ||
| `@agentworkforce/turn-kit`. | ||
|
|
||
| The persona explicitly enables workspace memory. The handler derives a stable | ||
| conversation id from the Relay channel + thread (or the peer identity for a | ||
| direct message), receives chronological history, sends one direct-model reply | ||
| to the originating channel or peer, requires a Relay delivery receipt, and only | ||
| then saves the turn. Direct messages fail closed when their peer identity is | ||
| unavailable, so separate senders cannot share workspace-scoped history. | ||
|
|
||
| ```bash | ||
| agentworkforce deploy ./examples/turn-agent/persona.ts --mode cloud | ||
| ``` | ||
|
|
||
| Real agents can add deterministic `defineTurnContext()` providers and interim | ||
| `acknowledge()` messages without changing that lifecycle. |
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,113 @@ | ||
| import { | ||
| defineAgent, | ||
| isRelaycastMessageEvent, | ||
| type AgentEvent | ||
| } from '@agentworkforce/runtime'; | ||
| import { | ||
| conversationKey, | ||
| createTurnRunner | ||
| } from '@agentworkforce/turn-kit'; | ||
|
|
||
| const turns = createTurnRunner({ | ||
| namespace: 'turn-agent-example', | ||
| memory: { | ||
| query: 'recent turn-agent-example conversation', | ||
| limit: 8, | ||
| ttlSeconds: 30 * 24 * 60 * 60, | ||
| assistantLabel: 'turn-agent-example' | ||
| } | ||
| }); | ||
|
|
||
| export default defineAgent({ | ||
| handler: async (ctx, rawEvent) => { | ||
| const event = rawEvent as unknown as AgentEvent; | ||
| if (!isRelaycastMessageEvent(event) || !event.channel) return; | ||
| const expanded = await event.expand('full'); | ||
| const input = messageText(expanded.data); | ||
| if (!input) return; | ||
| const directMessage = isDirectMessageChannel(event.channel); | ||
| const peer = directMessage ? relayPeer(event, expanded.data) : undefined; | ||
| if (directMessage && !peer) { | ||
| ctx.log('warn', 'turn-agent.direct-peer-unavailable', { channel: event.channel }); | ||
| return; | ||
| } | ||
|
|
||
| await turns.run(ctx, { | ||
| conversation: { | ||
| transport: 'relay', | ||
| id: directMessage | ||
| ? conversationKey(event.channel, peer) | ||
| : conversationKey(event.channel, event.threadId) | ||
| }, | ||
| input, | ||
| respond: async ({ history }) => | ||
| ctx.llm.complete( | ||
| [ | ||
| 'Answer clearly in at most four short lines.', | ||
| history.length | ||
| ? `Conversation so far (oldest first):\n${history.map((entry) => entry.content).join('\n')}` | ||
| : '', | ||
| `User: ${input}` | ||
| ].filter(Boolean).join('\n\n'), | ||
| { maxTokens: 300 } | ||
| ), | ||
| deliver: (reply) => | ||
| directMessage | ||
| ? ctx.relay.dm(peer!, reply) | ||
| : ctx.relay.post(event.channel!, reply), | ||
| confirmDelivery: (receipt) => receipt.ok | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| function isDirectMessageChannel(channel: string): boolean { | ||
| return channel === 'dm' || channel.startsWith('dm:'); | ||
| } | ||
|
|
||
| function relayPeer(event: AgentEvent, data: unknown): string | undefined { | ||
| const eventSummary = record((event as { summary?: unknown }).summary); | ||
| const full = record(data); | ||
| const candidates = [ | ||
| record(eventSummary?.actor), | ||
| record(full?.from), | ||
| record(full?.actor), | ||
| record(record(full?.message)?.from), | ||
| record(full?.message) | ||
| ]; | ||
| for (const candidate of candidates) { | ||
| const identity = | ||
| text(candidate?.id) ?? | ||
| text(candidate?.agentId) ?? | ||
| text(candidate?.agent_id) ?? | ||
| text(candidate?.name) ?? | ||
| text(candidate?.displayName); | ||
| if (identity) return identity; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function messageText(value: unknown): string { | ||
| const recordValue = record(value); | ||
| if (!recordValue) return ''; | ||
| const nested = | ||
| recordValue.message && typeof recordValue.message === 'object' && !Array.isArray(recordValue.message) | ||
| ? recordValue.message as Record<string, unknown> | ||
| : {}; | ||
| return ( | ||
| typeof recordValue.text === 'string' | ||
| ? recordValue.text | ||
| : typeof nested.text === 'string' | ||
| ? nested.text | ||
| : '' | ||
| ).trim(); | ||
| } | ||
|
|
||
| function record(value: unknown): Record<string, unknown> | undefined { | ||
| return value && typeof value === 'object' && !Array.isArray(value) | ||
| ? value as Record<string, unknown> | ||
| : undefined; | ||
| } | ||
|
|
||
| function text(value: unknown): string | undefined { | ||
| return typeof value === 'string' && value.trim() ? value.trim() : undefined; | ||
| } | ||
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,18 @@ | ||
| import { defineTurnPersona } from '@agentworkforce/turn-kit'; | ||
|
|
||
| export default defineTurnPersona({ | ||
| id: 'turn-agent-example', | ||
| intent: 'relay-orchestrator', | ||
| tags: ['discovery'], | ||
| description: 'Minimal multi-turn Relay chat agent built with turn-kit.', | ||
| cloud: true, | ||
| sandbox: false, | ||
| useSubscription: true, | ||
| harness: 'claude', | ||
| model: 'claude-haiku-4-5-20251001', | ||
| systemPrompt: 'Answer clearly and briefly, using the supplied conversation history.', | ||
| harnessSettings: { reasoning: 'low', timeoutSeconds: 300 }, | ||
| memory: { enabled: true, scopes: ['workspace'], ttlDays: 30 }, | ||
| relay: { inbox: ['@self'] }, | ||
| onEvent: './agent.ts' | ||
| }); |
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,26 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to `@agentworkforce/turn-kit` will be documented in this | ||
| file. | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Added | ||
|
|
||
| - Add a transport-neutral turn runner with chronological conversation memory, | ||
| deterministic context providers, best-effort interim acknowledgements, | ||
| delivery confirmation, and post-delivery persistence. | ||
| - Add standalone conversation identity, recall, and persistence helpers for | ||
| handlers that need only part of the lifecycle. | ||
| - Add a pass-through persona factory that requires durable memory and expose | ||
| the bundled kit version in runtime logs. | ||
| - Add a provider-agnostic confirmed-action helper that constructs success | ||
| output only after the caller's receipt predicate passes. | ||
| - Add the optional `@agentworkforce/turn-kit/assistant` bridge so Workforce | ||
| history and deterministic context blocks can use | ||
| `@agent-assistant/turn-context` for canonical identity/context assembly and | ||
| harness projection. | ||
| - Align deterministic context blocks with the Agent Assistant prepared-context | ||
| shape (`id`, `label`, `content`, source/category metadata). | ||
| - Encode conversation-key components independently to prevent delimiter | ||
| collisions, and sort timestamped history by parsed instants. |
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.
Uh oh!
There was an error while loading. Please reload this page.