spfunctions/agent-world-awareness
GitHub: spfunctions/agent-world-awareness
通过一行代码将 30,000+ 预测市场的校准概率数据注入 AI 智能体系统提示词,让大语言模型获得实时世界认知和不确定性量化。
Stars: 1 | Forks: 0
# 智能体世界感知
[](https://www.npmjs.com/package/agent-world-awareness)
[](LICENSE)
让任何 AI 代理**感知世界**只需一行代码。缓存的约 800 tokens 快照
包含 30,000+ 预测市场数据 — 可直接注入系统提示词。零配置,
零依赖,无需 API 密钥。
```
import { getWorldContext } from 'agent-world-awareness'
const context = await getWorldContext()
// markdown ready to drop into any LLM system prompt
```
## 为什么选择预测市场?
LLM 不知道当下发生了什么。网络搜索返回的是叙事和
观点;你需要权衡数百条相互矛盾的标题。**预测市场返回的是由真金白银支持的校准概率**——
这是关于不确定未来事件最接近真相的存在。
此包为您的代理提供:
- **正在发生什么** — 政权摘要、市场推动因素、塑造叙事的关健事件
- **确定性如何** — 不确定性指数 (0-100)、动量、地缘政治风险、活跃度
- **正在改变什么** — 自代理上次调用以来的增量变化(低成本轮询)
一次 `getWorldContext()` 调用返回约 800 tokens 的结构化 markdown,
已缓存 15 分钟,因此多个代理/工具共享一次网络调用。
## 安装
```
npm install agent-world-awareness
```
零运行时依赖。ESM 和 CJS,完全支持 TypeScript 类型。
## 使用方法
### 一行代码注入系统提示词
```
import OpenAI from 'openai'
import { getWorldContext } from 'agent-world-awareness'
const openai = new OpenAI()
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'system',
content: `You are a market intelligence assistant.
## 当前世界状态(预测市场,约15分钟延迟)
${await getWorldContext()}
Use this context to ground every claim in real-money probabilities.`,
},
{ role: 'user', content: "What's the highest geopolitical risk right now?" },
],
})
```
### LangChain / Vercel AI / CrewAI
相同的上下文可作为任何代理框架中的"系统上下文提供器"。有关更丰富的每工具集成
(多个工具、工具调用等),请参阅下面的姐妹包。
### 使用增量低成本轮询
```
import { getWorldChanges, refreshContext } from 'agent-world-awareness'
// Every minute: poll for changes, only refetch full context if there were changes
setInterval(async () => {
const delta = await getWorldChanges('1m')
if (delta.changes.length > 0) {
console.log('World changed:', delta.changes)
await refreshContext()
}
}, 60_000)
```
### 仅获取四个信号
```
import { getWorldSignals } from 'agent-world-awareness'
const s = await getWorldSignals()
// {
// uncertainty: 22, // 0-100
// geopolitical: 0, // 0-100
// momentum: -0.08, // -1 to +1
// activity: 99, // 0-100
// }
```
## API
### `getWorldContext(): Promise`
获取 `/api/agent/world?format=markdown` 并将结果作为字符串返回。
在内存中缓存 15 分钟。
### `getWorldSignals(): Promise`
获取 `/api/public/index` 并返回四个数值信号。在
内存中缓存 15 分钟(与 context 缓存分开)。
### `getWorldChanges(since?): Promise`
获取自 `since` 以来的增量变化。不缓存 — 始终获取最新数据。
```
interface WorldDelta {
from: string // ISO-8601 start
to: string // ISO-8601 end
changes: string[] // one-line change descriptions
markdown: string // pretty markdown rendering
}
```
`since` 接受相对时长(`'30m'`、`'1h'`、``'6h'`、`'24h'`)或
ISO-8601 时间戳。服务器默认为 `'1h'`。
### `refreshContext(): Promise`
强制刷新 context 缓存。返回新值。
### `isStale(maxAgeMs?): boolean`
如果缓存的 context 超过 `maxAgeMs`(默认为配置的
TTL,即 15 分钟),则返回 true。
### `setCacheTTL(ms): void`
覆盖 context 和 signals 的缓存 TTL。传入 `0` 可禁用缓存。
### `clearCache(): void`
清除两个缓存。
## 错误
所有 fetcher 在非 2xx 响应时抛出 `Error("SimpleFunctions API error for ")`,
`getWorldSignals` 在响应缺少预期字段时抛出 `"...malformed index payload"`。
## 姐妹包
想要与您的特定代理框架进行更丰富的集成?
| 技术栈 | 包 |
|-------|---------|
| Vercel AI SDK | [`vercel-ai-prediction-markets`](https://github.com/spfunctions/vercel-ai-prediction-markets) |
| LangChain / LangGraph | [`langchain-prediction-markets`](https://github.com/spfunctions/langchain-prediction-markets) |
| OpenAI Agents SDK / 函数调用 | [`openai-agents-prediction-markets`](https://github.com/spfunctions/openai-agents-prediction-markets) |
| CrewAI (Python) | [`crewai-prediction-markets`](https://github.com/spfunctions/crewai-prediction-markets) |
| MCP / Claude / Cursor | [`simplefunctions-cli`](https://github.com/spfunctions/simplefunctions-cli) |
| 仅一个数字或标签 | [`prediction-market-uncertainty`](https://github.com/spfunctions/prediction-market-uncertainty), [`prediction-market-regime`](https://github.com/spfunctions/prediction-market-regime) |
## 测试
```
npm test
```
14 个测试,全部使用 `fetch` 模拟 — 无需网络。
## 许可证
MIT — 由 [SimpleFunctions](https://simplefunctions.dev) 构建。
标签:AI代理, Apex, CJS, CMS安全, ESM, JavaScript, LLM增强, MITM代理, npm包, Token优化, TypeScript, 上下文增强, 世界状态, 人工智能, 动态上下文, 安全插件, 实时数据, 市场情报, 数据可视化, 数据获取, 暗色界面, 机器学习, 用户模式Hook绕过, 系统提示注入, 缓存, 自动化攻击, 预测市场