refactor(core): mechanism-neutral context entry rendering (#34945)
This commit is contained in:
parent
7843f8fb38
commit
dbaa53329c
2 changed files with 8 additions and 17 deletions
|
|
@ -33,22 +33,20 @@ const renderValue = (value: Schema.Json) => (typeof value === "string" ? value :
|
||||||
const renderBlock = (key: Key, value: Schema.Json) =>
|
const renderBlock = (key: Key, value: Schema.Json) =>
|
||||||
[`<context key="${key}">`, renderValue(value), "</context>"].join("\n")
|
[`<context key="${key}">`, renderValue(value), "</context>"].join("\n")
|
||||||
|
|
||||||
|
// Rendering stays mechanism-neutral: the model sees session context, not how
|
||||||
|
// it was attached. Only chronological updates and removals carry narration.
|
||||||
const source = (entry: Info) =>
|
const source = (entry: Info) =>
|
||||||
SystemContext.make({
|
SystemContext.make({
|
||||||
key: SystemContext.Key.make(`api/${entry.key}`),
|
key: SystemContext.Key.make(`api/${entry.key}`),
|
||||||
codec: Schema.toCodecJson(Schema.Json),
|
codec: Schema.toCodecJson(Schema.Json),
|
||||||
load: Effect.succeed(entry.value),
|
load: Effect.succeed(entry.value),
|
||||||
baseline: (value) =>
|
baseline: (value) => renderBlock(entry.key, value),
|
||||||
[
|
|
||||||
`An API client attached the following context to this session under "${entry.key}":`,
|
|
||||||
renderBlock(entry.key, value),
|
|
||||||
].join("\n"),
|
|
||||||
update: (_previous, value) =>
|
update: (_previous, value) =>
|
||||||
[
|
[
|
||||||
`The attached context "${entry.key}" changed. This value supersedes the previous one:`,
|
`The context under "${entry.key}" changed and supersedes the previous value:`,
|
||||||
renderBlock(entry.key, value),
|
renderBlock(entry.key, value),
|
||||||
].join("\n"),
|
].join("\n"),
|
||||||
removed: () => `The attached context "${entry.key}" was removed. Disregard it.`,
|
removed: () => `The context under "${entry.key}" no longer applies. Disregard it.`,
|
||||||
})
|
})
|
||||||
|
|
||||||
const layer = Layer.effect(
|
const layer = Layer.effect(
|
||||||
|
|
|
||||||
|
|
@ -1108,14 +1108,7 @@ describe("SessionRunnerLLM", () => {
|
||||||
// String values render verbatim inside the tagged block at baseline.
|
// String values render verbatim inside the tagged block at baseline.
|
||||||
expect(requests[0]?.system.map((part) => part.text)).toEqual([
|
expect(requests[0]?.system.map((part) => part.text)).toEqual([
|
||||||
defaultSystem,
|
defaultSystem,
|
||||||
[
|
["Initial context", "", '<context key="deploy-target">', "production", "</context>"].join("\n"),
|
||||||
"Initial context",
|
|
||||||
"",
|
|
||||||
'An API client attached the following context to this session under "deploy-target":',
|
|
||||||
'<context key="deploy-target">',
|
|
||||||
"production",
|
|
||||||
"</context>",
|
|
||||||
].join("\n"),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
// Non-string JSON pretty-prints; the change narrates as a System update.
|
// Non-string JSON pretty-prints; the change narrates as a System update.
|
||||||
|
|
@ -1128,7 +1121,7 @@ describe("SessionRunnerLLM", () => {
|
||||||
{
|
{
|
||||||
type: "text",
|
type: "text",
|
||||||
text: [
|
text: [
|
||||||
'The attached context "deploy-target" changed. This value supersedes the previous one:',
|
'The context under "deploy-target" changed and supersedes the previous value:',
|
||||||
'<context key="deploy-target">',
|
'<context key="deploy-target">',
|
||||||
"{",
|
"{",
|
||||||
' "region": "us-east-1"',
|
' "region": "us-east-1"',
|
||||||
|
|
@ -1146,7 +1139,7 @@ describe("SessionRunnerLLM", () => {
|
||||||
|
|
||||||
expect(requests[2]?.messages.map((message) => message.role)).toEqual(["user", "system", "user", "system", "user"])
|
expect(requests[2]?.messages.map((message) => message.role)).toEqual(["user", "system", "user", "system", "user"])
|
||||||
expect(requests[2]?.messages.at(-2)?.content).toEqual([
|
expect(requests[2]?.messages.at(-2)?.content).toEqual([
|
||||||
{ type: "text", text: 'The attached context "deploy-target" was removed. Disregard it.' },
|
{ type: "text", text: 'The context under "deploy-target" no longer applies. Disregard it.' },
|
||||||
])
|
])
|
||||||
expect(yield* contextEntries.list(sessionID)).toEqual([])
|
expect(yield* contextEntries.list(sessionID)).toEqual([])
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue