Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/agents/src/harness/think/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2917,8 +2917,12 @@ export class ThinkHarness<TOOLS extends ToolSet = ToolSet>
generateMessageId: () => messageId,

// The participant who started the turn runs its client tools.
messageMetadata: ({ part }) =>
part.type === "start" && owner !== undefined ? { owner } : undefined
messageMetadata: ({ part }) => {
const own =
part.type === "start" && owner !== undefined ? { owner } : undefined;
const host = config.messageMetadata?.({ part });
return host === undefined ? own : { ...host, ...own };
Comment thread
mattzcarey marked this conversation as resolved.
Resolved
}
});
return {
hideReasoning: config.sendReasoning === false,
Expand Down
8 changes: 8 additions & 0 deletions packages/agents/src/harness/think/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ export type ThinkTurnConfig = {
readonly callSettings?: ThinkCallSettings;
/** @internal Whether reasoning reaches the stream; default true. */
readonly sendReasoning?: boolean;
/**
* @internal Metadata for the reply from the model's stream parts, as
* the AI SDK's `toUIMessageStream({ messageMetadata })` takes it. It is
* merged onto the reply's metadata.
*/
readonly messageMetadata?: (options: {
readonly part: { readonly type: string };
}) => Record<string, unknown> | undefined;
/**
* @internal The AI SDK's structured output spec (`Output.object(...)`),
* passed to `streamText`. The last model call's parsed output is the
Expand Down
25 changes: 23 additions & 2 deletions packages/think/src/harness/think.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ import {
loadThinkExtensions,
ThinkExtensionHooks
} from "../extensions/wiring";
import type { ExtensionConfig, OnStartDegradation } from "../turn-types";
import type {
ExtensionConfig,
MessageMetadataCallback,
OnStartDegradation
} from "../turn-types";
import { ThinkSkills } from "../skills-runtime";
import { declaredOverrides } from "../overrides";
import type { SkillScriptRunner, SkillSource } from "agents/skills";
Expand Down Expand Up @@ -421,6 +425,12 @@ export class Think<
/** How a messenger thread's burst of messages is answered, as on Think. */
messengerConcurrency: MessengerConcurrency = DEFAULT_MESSENGER_CONCURRENCY;

/**
* Metadata for every assistant message, as on Think. A turn's
* `beforeTurn` may return its own `messageMetadata` instead.
*/
messageMetadata?: MessageMetadataCallback;
Comment thread
mattzcarey marked this conversation as resolved.
Resolved

readonly harness: ThinkHarness;
readonly #chat: ThinkChat;
/** The messengers from `getMessengers()`, as Chat SDK Channels. */
Expand Down Expand Up @@ -3925,6 +3935,7 @@ export class Think<
// As in Think: recorded once the turn is prepared, so a continuation
// never extends a turn whose preparation threw.
if (first) this.#lastTurnChannel = this.#turnChannel.channelId;
const writer = config.messageMetadata ?? this.messageMetadata;
const sentSystem =
final && typeof system === "string"
? [system, thinkFinalAnswerInstruction(final.name)]
Expand Down Expand Up @@ -3955,7 +3966,17 @@ export class Think<
// The rest of Think's call settings, as `streamText` takes them.
...(Object.keys(callSettings).length > 0 && { callSettings }),
...(config.output !== undefined && { output: config.output }),
sendReasoning: config.sendReasoning ?? this.sendReasoning
sendReasoning: config.sendReasoning ?? this.sendReasoning,
// As in Think: the turn's writer, else the agent's, told whether the
// turn continues a reply.
...(writer && {
messageMetadata: ({ part }: { readonly part: unknown }) =>
writer({
// SAFETY: the parts are the AI SDK's stream parts.
part: part as Parameters<MessageMetadataCallback>[0]["part"],
continuation: turn.continuation
Comment thread
mattzcarey marked this conversation as resolved.
Resolved
})
})
} as ThinkTurnConfig;
}

Expand Down